user_center_page.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/config/colors.dart';
  6. import 'package:lszlgl/model/rsp/user_rsp.dart';
  7. import 'package:lszlgl/service/user_service.dart';
  8. import 'package:lszlgl/utils/sp_utils.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>
  18. with AutomaticKeepAliveClientMixin {
  19. final userNotify = null.notifier<UserRsp?>();
  20. void startSetting() {
  21. MyRouter.startSetting();
  22. }
  23. Future<void> onRefresh() async {
  24. try {
  25. var value = await MyApi.get().userProfile();
  26. if (value.data != null) userNotify.value = value.data;
  27. await UserService.get().saveUser(value.data);
  28. } catch (e) {
  29. logger.e(e);
  30. }
  31. }
  32. @override
  33. void onInit() {
  34. userNotify.value = UserService.get().getUser();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. super.build(context);
  39. return myScaffold(child: buildBody());
  40. }
  41. Widget buildBody() {
  42. return Column(
  43. children: [
  44. myAppBar(
  45. title: '用户中心',
  46. autoLeading: false,
  47. naviBarColor: Colors.white,
  48. ),
  49. Expanded(
  50. child: EasyRefresh(
  51. onRefresh: onRefresh,
  52. child: CustomScrollView(
  53. slivers: [
  54. SliverToBoxAdapter(
  55. child: Column(
  56. children: [
  57. buildUserInfo(),
  58. const SizedBox(height: 28),
  59. buildQuit(),
  60. ],
  61. ),
  62. )
  63. ],
  64. ),
  65. ),
  66. ),
  67. ],
  68. );
  69. }
  70. Widget buildUserInfo() {
  71. return userNotify.builder((user) {
  72. return Stack(
  73. alignment: Alignment.bottomCenter,
  74. children: [
  75. Container(
  76. margin: const EdgeInsets.symmetric(horizontal: 12),
  77. padding: const EdgeInsets.fromLTRB(20, 30, 20, 156),
  78. clipBehavior: Clip.hardEdge,
  79. decoration: BoxDecoration(
  80. borderRadius: const BorderRadius.all(Radius.circular(12)),
  81. boxShadow: [
  82. BoxShadow(
  83. color: Colors.black.withOpacity(0.1),
  84. offset: const Offset(0, 5),
  85. blurRadius: 4)
  86. ],
  87. image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
  88. ),
  89. child: Row(
  90. crossAxisAlignment: CrossAxisAlignment.start,
  91. children: [
  92. Expanded(
  93. child: Column(
  94. crossAxisAlignment: CrossAxisAlignment.start,
  95. mainAxisSize: MainAxisSize.min,
  96. children: [
  97. Text(
  98. user?.nickname ?? '',
  99. style: const TextStyle(
  100. fontSize: 18, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  101. ),
  102. const SizedBox(height: 4),
  103. Text(
  104. user?.username ?? '',
  105. style: const TextStyle(
  106. fontSize: 18, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  107. ),
  108. const SizedBox(height: 6),
  109. buildDeptText(user?.dept?.name ?? ''),
  110. ],
  111. ),
  112. ),
  113. buildUserAvatar(user?.avatar ?? ''),
  114. ],
  115. ),
  116. ),
  117. buildBottomStack(),
  118. ],
  119. );
  120. });
  121. }
  122. Widget buildBottomStack() {
  123. return Container(
  124. padding: const EdgeInsets.only(bottom: 16),
  125. alignment: Alignment.bottomCenter,
  126. height: 140,
  127. decoration: const BoxDecoration(
  128. image: DecorationImage(image: AssetImage(imgUserCenterTopBgIm), fit: BoxFit.fill)),
  129. child: Row(
  130. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  131. mainAxisSize: MainAxisSize.max,
  132. children: [
  133. GestureDetector(
  134. onTap: () {
  135. MyRouter.startChangePwd();
  136. },
  137. child: Column(
  138. mainAxisSize: MainAxisSize.min,
  139. children: [
  140. Image.asset(imgUserCenterChangePwdIm, width: 50, height: 50),
  141. const SizedBox(height: 8),
  142. const Text('修改密码')
  143. ],
  144. ),
  145. ),
  146. Container(
  147. width: 1,
  148. height: 40,
  149. color: MyColor.c_background,
  150. ),
  151. GestureDetector(
  152. onTap: startSetting,
  153. child: Column(
  154. mainAxisSize: MainAxisSize.min,
  155. children: [
  156. Image.asset(imgUserCenterSettingIm, width: 50, height: 50),
  157. const SizedBox(height: 8),
  158. const Text('设置')
  159. ],
  160. ),
  161. ),
  162. ],
  163. ),
  164. );
  165. }
  166. Widget buildUserAvatar(String imageUrl) {
  167. return Container(
  168. width: 72,
  169. height: 72,
  170. clipBehavior: Clip.hardEdge,
  171. decoration: const BoxDecoration(
  172. color: Color(0xFF7085A1),
  173. borderRadius: BorderRadius.all(Radius.circular(200)),
  174. border: Border.fromBorderSide(BorderSide(color: Colors.white, width: 1, strokeAlign: 1.0)),
  175. ),
  176. child: CachedNetworkImage(
  177. fit: BoxFit.cover,
  178. imageUrl: imageUrl,
  179. placeholder: (_, __) => const Center(child: CircularProgressIndicator()),
  180. errorWidget: (context, url, error) =>
  181. const Center(child: Icon(Icons.error, color: Colors.grey)),
  182. ),
  183. );
  184. }
  185. Widget buildDeptText(String text) {
  186. return Row(
  187. children: [
  188. if (text.isNotEmpty)
  189. Padding(
  190. padding: const EdgeInsets.only(top: 2.0),
  191. child: Image.asset(imgUserCenterNameIm, width: 12, height: 12),
  192. ),
  193. const SizedBox(width: 4),
  194. Expanded(
  195. child: Text(
  196. text,
  197. maxLines: 2,
  198. style: const TextStyle(
  199. color: Color(0xFF6A7891),
  200. fontSize: 12,
  201. fontWeight: FontWeight.w500,
  202. ),
  203. ),
  204. ),
  205. ],
  206. );
  207. }
  208. Widget buildQuit() {
  209. return Container(
  210. color: Colors.white,
  211. width: MediaQuery.of(context).size.width,
  212. child: TextButton(
  213. onPressed: () {
  214. SPUtils.getInstance().remove('accountpwd');
  215. UserService.get().logout();
  216. },
  217. child: const Text('退出登录',style: TextStyle(color: Color(0xFF333333),fontSize: 15)),
  218. ),
  219. );
  220. }
  221. @override
  222. bool get wantKeepAlive => true;
  223. }