user_center_page.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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: Stack(
  85. children: [
  86. Row(
  87. children: [
  88. buildUserAvatar(user?.avatar ?? ''),
  89. Column(
  90. crossAxisAlignment: CrossAxisAlignment.start,
  91. mainAxisSize: MainAxisSize.min,
  92. children: [
  93. Text(
  94. user?.nickname ?? '',
  95. style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  96. ),
  97. const SizedBox(height: 14),
  98. Text(
  99. user?.username ?? '',
  100. style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  101. ),
  102. ],
  103. ),
  104. ],
  105. ),
  106. Positioned(
  107. right: 0,
  108. child: buildDeptText(user?.dept?.name ?? '')),
  109. ],
  110. ),
  111. ),
  112. );
  113. });
  114. }
  115. Widget buildUserAvatar(String imageUrl) {
  116. return Container(
  117. width: 72,
  118. height: 72,
  119. margin: const EdgeInsets.symmetric(vertical: 24, horizontal: 12),
  120. clipBehavior: Clip.hardEdge,
  121. decoration: const BoxDecoration(
  122. color: Color(0xFF7085A1),
  123. borderRadius: BorderRadius.all(Radius.circular(200)),
  124. border: Border.fromBorderSide(BorderSide(color: Color(0xFF7085A1), width: 1, strokeAlign: 1.0)),
  125. ),
  126. child: CachedNetworkImage(
  127. fit: BoxFit.cover,
  128. imageUrl: imageUrl,
  129. placeholder: (_, __) => const Center(child: CircularProgressIndicator()),
  130. errorWidget: (context, url, error) => const Center(child: Icon(Icons.error, color: Colors.grey)),
  131. ),
  132. );
  133. }
  134. Widget buildDeptText(String text) {
  135. return Padding(
  136. padding: const EdgeInsets.only(top: 12, right: 12),
  137. child: Text(
  138. text,
  139. style: const TextStyle(
  140. color: Color(0xFF333333),
  141. fontSize: 12,
  142. fontWeight: FontWeight.w500,
  143. ),
  144. ),
  145. );
  146. }
  147. Widget buildList() {
  148. return Container(
  149. margin: const EdgeInsets.symmetric(horizontal: 12),
  150. clipBehavior: Clip.hardEdge,
  151. decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10))),
  152. child: Column(
  153. children: [
  154. CardItemWidget(
  155. '账号管理',
  156. onTap: startAccountManage,
  157. leading: Image.asset(imgUserCenterAccount, height: 16),
  158. trailing: Image.asset(imgItemArrowRight, height: 16),
  159. bottomLine: true,
  160. ),
  161. CardItemWidget(
  162. '设置',
  163. onTap: startSetting,
  164. leading: Image.asset(imgUserCenterSetting, height: 16),
  165. ),
  166. ],
  167. ),
  168. );
  169. }
  170. @override
  171. bool get wantKeepAlive => true;
  172. }