profile.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:chicken_farm/stores/auth_store.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. class ProfilePage extends ConsumerWidget {
  5. const ProfilePage({super.key});
  6. @override
  7. Widget build(BuildContext context, WidgetRef ref) {
  8. final authState = ref.watch(authStoreProvider);
  9. final authStore = ref.read(authStoreProvider.notifier);
  10. return Scaffold(
  11. body: Padding(
  12. padding: const EdgeInsets.all(16.0),
  13. child: Column(
  14. crossAxisAlignment: CrossAxisAlignment.start,
  15. children: [
  16. const SizedBox(height: 20),
  17. if (authState.state == AuthState.authenticated &&
  18. authState.user != null) ...[
  19. Card(
  20. child: Padding(
  21. padding: const EdgeInsets.all(16.0),
  22. child: Column(
  23. crossAxisAlignment: CrossAxisAlignment.start,
  24. children: [
  25. ListTile(
  26. leading: const CircleAvatar(
  27. radius: 30,
  28. child: Icon(Icons.person, size: 30),
  29. ),
  30. title: Text(
  31. authState.user!.nickName ?? '未设置昵称',
  32. style: const TextStyle(
  33. fontSize: 20,
  34. fontWeight: FontWeight.bold,
  35. ),
  36. ),
  37. subtitle: Text('用户名: ${authState.user!.userName}'),
  38. ),
  39. const Divider(),
  40. const Text(
  41. '基本信息',
  42. style: TextStyle(fontWeight: FontWeight.bold),
  43. ),
  44. const SizedBox(height: 10),
  45. _buildInfoRow(
  46. '手机号码',
  47. authState.user!.phonenumber ?? '未填写',
  48. ),
  49. _buildInfoRow('邮箱地址', authState.user!.email ?? '未填写'),
  50. _buildInfoRow('部门', authState.user!.orgName ?? '未分配'),
  51. _buildInfoRow(
  52. '角色',
  53. (authState.user!.roles
  54. ?.map((r) => r.roleName)
  55. .join(', ')) ??
  56. '未分配',
  57. ),
  58. ],
  59. ),
  60. ),
  61. ),
  62. const SizedBox(height: 20),
  63. SizedBox(
  64. width: double.infinity,
  65. child: ElevatedButton.icon(
  66. onPressed: () => authStore.logout(),
  67. icon: const Icon(Icons.logout),
  68. label: const Text('退出登录'),
  69. style: ElevatedButton.styleFrom(
  70. padding: const EdgeInsets.symmetric(
  71. horizontal: 30,
  72. vertical: 15,
  73. ),
  74. textStyle: const TextStyle(fontSize: 16),
  75. ),
  76. ),
  77. ),
  78. ] else ...[
  79. const Center(child: Text('加载中...')),
  80. ],
  81. ],
  82. ),
  83. ),
  84. );
  85. }
  86. Widget _buildInfoRow(String label, String value) {
  87. return Padding(
  88. padding: const EdgeInsets.symmetric(vertical: 4.0),
  89. child: Row(
  90. children: [
  91. SizedBox(
  92. width: 80,
  93. child: Text(
  94. '$label:',
  95. style: const TextStyle(fontWeight: FontWeight.w500),
  96. ),
  97. ),
  98. Expanded(child: Text(value)),
  99. ],
  100. ),
  101. );
  102. }
  103. }