profile.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import 'package:chicken_farm/core/utils/storage.dart';
  2. import 'package:chicken_farm/core/utils/toast.dart';
  3. import 'package:chicken_farm/stores/auth_store.dart';
  4. import 'package:chicken_farm/stores/config_store.dart';
  5. import 'package:chicken_farm/stores/dict_stroe.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter_riverpod/flutter_riverpod.dart';
  8. class ProfilePage extends ConsumerWidget {
  9. const ProfilePage({super.key});
  10. @override
  11. Widget build(BuildContext context, WidgetRef ref) {
  12. final authState = ref.watch(authStoreProvider);
  13. final authStore = ref.read(authStoreProvider.notifier);
  14. return Scaffold(
  15. body: Padding(
  16. padding: const EdgeInsets.all(16.0),
  17. child: Column(
  18. crossAxisAlignment: CrossAxisAlignment.start,
  19. children: [
  20. const SizedBox(height: 20),
  21. if (authState.state == AuthState.authenticated &&
  22. authState.user != null) ...[
  23. Expanded(
  24. child: SingleChildScrollView(
  25. child: Column(
  26. crossAxisAlignment: CrossAxisAlignment.start,
  27. children: [
  28. Card(
  29. child: Padding(
  30. padding: const EdgeInsets.all(16.0),
  31. child: Column(
  32. crossAxisAlignment: CrossAxisAlignment.start,
  33. children: [
  34. ListTile(
  35. leading: const CircleAvatar(
  36. radius: 30,
  37. child: Icon(Icons.person, size: 30),
  38. ),
  39. title: Text(
  40. authState.user!.nickName ?? '未设置昵称',
  41. style: const TextStyle(
  42. fontSize: 20,
  43. fontWeight: FontWeight.bold,
  44. ),
  45. ),
  46. subtitle: Text(
  47. '用户名: ${authState.user!.userName}',
  48. ),
  49. ),
  50. const Divider(),
  51. const Text(
  52. '基本信息',
  53. style: TextStyle(fontWeight: FontWeight.bold),
  54. ),
  55. const SizedBox(height: 10),
  56. _buildInfoRow(
  57. '手机号码',
  58. authState.user!.phonenumber ?? '未填写',
  59. ),
  60. _buildInfoRow(
  61. '邮箱地址',
  62. authState.user!.email ?? '未填写',
  63. ),
  64. _buildInfoRow(
  65. '部门',
  66. authState.user!.orgName ?? '未分配',
  67. ),
  68. _buildInfoRow(
  69. '角色',
  70. (authState.user!.roles
  71. ?.map((r) => r.roleName)
  72. .join(', ')) ??
  73. '未分配',
  74. ),
  75. ],
  76. ),
  77. ),
  78. ),
  79. const SizedBox(height: 20),
  80. SizedBox(
  81. width: double.infinity,
  82. child: ElevatedButton.icon(
  83. onPressed: () {
  84. ToastUtil.confirm('确定要清除所有缓存吗?', () async {
  85. await StorageUtils.removeWithPrefix("vb_");
  86. ConfigStore().clearAll();
  87. DictStore().clearAll();
  88. if (context.mounted) {
  89. ToastUtil.success('缓存已清除');
  90. }
  91. });
  92. },
  93. icon: const Icon(Icons.cleaning_services),
  94. label: const Text('清除缓存'),
  95. style: ElevatedButton.styleFrom(
  96. padding: const EdgeInsets.symmetric(
  97. horizontal: 30,
  98. vertical: 15,
  99. ),
  100. textStyle: const TextStyle(fontSize: 16),
  101. ),
  102. ),
  103. ),
  104. ],
  105. ),
  106. ),
  107. ),
  108. SizedBox(
  109. width: double.infinity,
  110. child: ElevatedButton.icon(
  111. onPressed: () {
  112. ToastUtil.confirm('确定要退出登录吗?', () => authStore.logout());
  113. },
  114. icon: const Icon(Icons.logout),
  115. label: const Text('退出登录'),
  116. style: ElevatedButton.styleFrom(
  117. // 保持背景色不变,只改变文字颜色为红色
  118. foregroundColor: Colors.red,
  119. padding: const EdgeInsets.symmetric(
  120. horizontal: 30,
  121. vertical: 15,
  122. ),
  123. textStyle: const TextStyle(fontSize: 16),
  124. ),
  125. ),
  126. ),
  127. ] else ...[
  128. const Center(child: Text('加载中...')),
  129. ],
  130. ],
  131. ),
  132. ),
  133. );
  134. }
  135. Widget _buildInfoRow(String label, String value) {
  136. return Padding(
  137. padding: const EdgeInsets.symmetric(vertical: 4.0),
  138. child: Row(
  139. children: [
  140. SizedBox(
  141. width: 80,
  142. child: Text(
  143. '$label:',
  144. style: const TextStyle(fontWeight: FontWeight.w500),
  145. ),
  146. ),
  147. Expanded(child: Text(value)),
  148. ],
  149. ),
  150. );
  151. }
  152. }