import 'package:chicken_farm/core/utils/storage.dart'; import 'package:chicken_farm/core/utils/toast.dart'; import 'package:chicken_farm/stores/auth_store.dart'; import 'package:chicken_farm/stores/config_store.dart'; import 'package:chicken_farm/stores/dict_stroe.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; class ProfilePage extends ConsumerWidget { const ProfilePage({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final authState = ref.watch(authStoreProvider); final authStore = ref.read(authStoreProvider.notifier); return Scaffold( body: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const SizedBox(height: 20), if (authState.state == AuthState.authenticated && authState.user != null) ...[ Expanded( child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ListTile( leading: const CircleAvatar( radius: 30, child: Icon(Icons.person, size: 30), ), title: Text( authState.user!.nickName ?? '未设置昵称', style: const TextStyle( fontSize: 20, fontWeight: FontWeight.bold, ), ), subtitle: Text( '用户名: ${authState.user!.userName}', ), ), const Divider(), const Text( '基本信息', style: TextStyle(fontWeight: FontWeight.bold), ), const SizedBox(height: 10), _buildInfoRow( '手机号码', authState.user!.phonenumber ?? '未填写', ), _buildInfoRow( '邮箱地址', authState.user!.email ?? '未填写', ), _buildInfoRow( '部门', authState.user!.orgName ?? '未分配', ), _buildInfoRow( '角色', (authState.user!.roles ?.map((r) => r.roleName) .join(', ')) ?? '未分配', ), ], ), ), ), const SizedBox(height: 20), SizedBox( width: double.infinity, child: ElevatedButton.icon( onPressed: () { ToastUtil.confirm('确定要清除所有缓存吗?', () async { await StorageUtils.removeWithPrefix("vb_"); ConfigStore().clearAll(); DictStore().clearAll(); if (context.mounted) { ToastUtil.success('缓存已清除'); } }); }, icon: const Icon(Icons.cleaning_services), label: const Text('清除缓存'), style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric( horizontal: 30, vertical: 15, ), textStyle: const TextStyle(fontSize: 16), ), ), ), ], ), ), ), SizedBox( width: double.infinity, child: ElevatedButton.icon( onPressed: () { ToastUtil.confirm('确定要退出登录吗?', () => authStore.logout()); }, icon: const Icon(Icons.logout), label: const Text('退出登录'), style: ElevatedButton.styleFrom( // 保持背景色不变,只改变文字颜色为红色 foregroundColor: Colors.red, padding: const EdgeInsets.symmetric( horizontal: 30, vertical: 15, ), textStyle: const TextStyle(fontSize: 16), ), ), ), ] else ...[ const Center(child: Text('加载中...')), ], ], ), ), ); } Widget _buildInfoRow(String label, String value) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4.0), child: Row( children: [ SizedBox( width: 80, child: Text( '$label:', style: const TextStyle(fontWeight: FontWeight.w500), ), ), Expanded(child: Text(value)), ], ), ); } }