user_center_page.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:easy_refresh/easy_refresh.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:lszlgl/base/base_lifecycle_state.dart';
  5. import 'package:lszlgl/base/base_state.dart';
  6. import 'package:lszlgl/model/rsp/user_rsp.dart';
  7. import 'package:lszlgl/service/user_service.dart';
  8. import 'package:lszlgl/widget/card_item.dart';
  9. import '../../network/my_api.dart';
  10. /// 用户中心
  11. class UserCenterPage extends StatefulWidget {
  12. const UserCenterPage({Key? key}) : super(key: key);
  13. @override
  14. State<UserCenterPage> createState() => _UserCenterPageState();
  15. }
  16. class _UserCenterPageState extends BaseLifecycleState<UserCenterPage> with AutomaticKeepAliveClientMixin {
  17. final userNotify = null.notifier<UserRsp?>();
  18. void startAccountManage() {
  19. MyRouter.startAccountManage();
  20. }
  21. void startSetting() {
  22. MyRouter.startSetting();
  23. }
  24. Future<void> onRefresh() async {
  25. try {
  26. var value = await MyApi.get().userProfile();
  27. if (value.data != null) userNotify.value = value.data;
  28. } catch (err) {}
  29. }
  30. @override
  31. void onInit() {
  32. userNotify.value = UserService.get().getUser();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. super.build(context);
  37. return myScaffold(child: buildBody());
  38. }
  39. Widget buildBody() {
  40. return Column(
  41. children: [
  42. myAppBar(
  43. title: '用户中心',
  44. autoLeading: false,
  45. naviBarColor: Colors.white,
  46. ),
  47. Expanded(
  48. child: EasyRefresh(
  49. onRefresh: onRefresh,
  50. child: CustomScrollView(
  51. slivers: [
  52. SliverToBoxAdapter(
  53. child: Column(
  54. children: [
  55. buildUserInfo(),
  56. const SizedBox(height: 24),
  57. buildList(),
  58. ],
  59. ),
  60. )
  61. ],
  62. ),
  63. ),
  64. ),
  65. ],
  66. );
  67. }
  68. Widget buildUserInfo() {
  69. return userNotify.builder((user) {
  70. return GestureDetector(
  71. onTap: startAccountManage,
  72. child: Container(
  73. margin: const EdgeInsets.symmetric(horizontal: 12),
  74. clipBehavior: Clip.hardEdge,
  75. decoration: BoxDecoration(
  76. borderRadius: const BorderRadius.all(Radius.circular(12)),
  77. boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
  78. image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
  79. ),
  80. child: Row(
  81. crossAxisAlignment: CrossAxisAlignment.start,
  82. children: [
  83. Expanded(
  84. child: Row(
  85. children: [
  86. buildUserAvatar(user?.avatar ?? ''),
  87. Column(
  88. crossAxisAlignment: CrossAxisAlignment.start,
  89. mainAxisSize: MainAxisSize.min,
  90. children: [
  91. Text(
  92. user?.nickname ?? '',
  93. style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  94. ),
  95. const SizedBox(height: 14),
  96. Text(
  97. user?.username ?? '',
  98. style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  99. ),
  100. ],
  101. ),
  102. ],
  103. ),
  104. ),
  105. buildDeptText(user?.dept?.name ?? ''),
  106. ],
  107. ),
  108. ),
  109. );
  110. });
  111. }
  112. Widget buildUserAvatar(String imageUrl) {
  113. return Container(
  114. width: 72,
  115. height: 72,
  116. margin: const EdgeInsets.symmetric(vertical: 24, horizontal: 12),
  117. clipBehavior: Clip.hardEdge,
  118. decoration: const BoxDecoration(
  119. color: Color(0xFF7085A1),
  120. borderRadius: BorderRadius.all(Radius.circular(200)),
  121. border: Border.fromBorderSide(BorderSide(color: Color(0xFF7085A1), width: 1, strokeAlign: 1.0)),
  122. ),
  123. child: CachedNetworkImage(
  124. fit: BoxFit.cover,
  125. imageUrl: imageUrl,
  126. placeholder: (_, __) => const Center(child: CircularProgressIndicator()),
  127. errorWidget: (context, url, error) => const Center(child: Icon(Icons.error, color: Colors.grey)),
  128. ),
  129. );
  130. }
  131. Widget buildDeptText(String text) {
  132. return Padding(
  133. padding: const EdgeInsets.only(top: 12, right: 12),
  134. child: Text(
  135. text,
  136. style: const TextStyle(
  137. color: Color(0xFF333333),
  138. fontSize: 12,
  139. fontWeight: FontWeight.w500,
  140. ),
  141. ),
  142. );
  143. }
  144. Widget buildList() {
  145. return Container(
  146. margin: const EdgeInsets.symmetric(horizontal: 12),
  147. clipBehavior: Clip.hardEdge,
  148. decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10))),
  149. child: Column(
  150. children: [
  151. CardItemWidget(
  152. '账号管理',
  153. onTap: startAccountManage,
  154. leading: Image.asset(imgUserCenterAccount, height: 16),
  155. trailing: Image.asset(imgItemArrowRight, height: 16),
  156. bottomLine: true,
  157. ),
  158. CardItemWidget(
  159. '设置',
  160. onTap: startSetting,
  161. leading: Image.asset(imgUserCenterSetting, height: 16),
  162. ),
  163. ],
  164. ),
  165. );
  166. }
  167. @override
  168. bool get wantKeepAlive => true;
  169. }