user_center_page.dart 5.6 KB

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