user_center_page.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/network/api.dart';
  8. import 'package:lszlgl/service/user_service.dart';
  9. import 'package:lszlgl/widget/card_item.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 Api().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(title: '用户中心', autoLeading: false),
  43. Expanded(
  44. child: EasyRefresh(
  45. onRefresh: onRefresh,
  46. child: CustomScrollView(
  47. slivers: [
  48. SliverToBoxAdapter(
  49. child: Column(
  50. children: [
  51. buildUserInfo(),
  52. const SizedBox(height: 30),
  53. buildList(),
  54. ],
  55. ),
  56. )
  57. ],
  58. ),
  59. ),
  60. ),
  61. ],
  62. );
  63. }
  64. Widget buildUserInfo() {
  65. return userNotify.builder((user) {
  66. return GestureDetector(
  67. onTap: startAccountManage,
  68. child: Container(
  69. margin: const EdgeInsets.symmetric(horizontal: 12),
  70. padding: const EdgeInsets.only(left: 8, right: 14, top: 14, bottom: 26),
  71. clipBehavior: Clip.hardEdge,
  72. decoration: BoxDecoration(
  73. borderRadius: const BorderRadius.all(Radius.circular(12)),
  74. boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)],
  75. image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill),
  76. ),
  77. child: Column(
  78. children: [
  79. buildUserText(user?.dept?.name ?? ''),
  80. Row(
  81. children: [
  82. buildUserAvatar(user?.avatar ?? ''),
  83. const SizedBox(width: 12),
  84. Column(
  85. crossAxisAlignment: CrossAxisAlignment.start,
  86. mainAxisSize: MainAxisSize.min,
  87. children: [
  88. Text(
  89. user?.nickname ?? '',
  90. style: const TextStyle(fontSize: 17, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  91. ),
  92. const SizedBox(height: 14),
  93. Text(
  94. user?.username ?? '',
  95. style: const TextStyle(fontSize: 17, color: Color(0xFF333333), fontWeight: FontWeight.w500),
  96. ),
  97. ],
  98. ),
  99. ],
  100. ),
  101. ],
  102. ),
  103. ),
  104. );
  105. });
  106. }
  107. Widget buildUserAvatar(String imageUrl) {
  108. return Container(
  109. width: 75,
  110. height: 75,
  111. clipBehavior: Clip.hardEdge,
  112. decoration: const BoxDecoration(
  113. color: Color(0xFF7085A1),
  114. borderRadius: BorderRadius.all(Radius.circular(200)),
  115. border: Border.fromBorderSide(BorderSide(color: Color(0xFF7085A1), width: 1, strokeAlign: 1.0)),
  116. ),
  117. child: CachedNetworkImage(
  118. fit: BoxFit.cover,
  119. imageUrl: imageUrl,
  120. placeholder: (_, __) => const Center(child: CircularProgressIndicator()),
  121. errorWidget: (context, url, error) => const Center(child: Icon(Icons.error)),
  122. ),
  123. );
  124. }
  125. Widget buildUserText(String text) {
  126. return Align(
  127. alignment: Alignment.topRight,
  128. child: Text(
  129. text,
  130. style: const TextStyle(
  131. color: Color(0xFF333333),
  132. fontSize: 13,
  133. fontWeight: FontWeight.w500,
  134. ),
  135. ),
  136. );
  137. }
  138. Widget buildList() {
  139. return Container(
  140. margin: const EdgeInsets.symmetric(horizontal: 14),
  141. clipBehavior: Clip.hardEdge,
  142. decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10))),
  143. child: Column(
  144. children: [
  145. CardItemWidget(
  146. '账号管理',
  147. onTap: startAccountManage,
  148. leading: Image.asset(imgUserCenterAccount, height: 16),
  149. trailing: Image.asset(imgItemArrowRight, height: 16),
  150. bottomLine: true,
  151. ),
  152. CardItemWidget(
  153. '设置',
  154. onTap: startSetting,
  155. leading: Image.asset(imgUserCenterSetting, height: 16),
  156. ),
  157. ],
  158. ),
  159. );
  160. }
  161. @override
  162. bool get wantKeepAlive => true;
  163. }