| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import 'package:chicken_farm/core/services/offline_storage_service.dart';
- import 'package:chicken_farm/core/services/sync_service.dart';
- 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';
- import 'package:chicken_farm/core/services/connectivity_service.dart';
- import 'package:chicken_farm/pages/account/config_dialog.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),
- // 显示待上传数据数量的按钮
- FutureBuilder<List<OfflineOperation>>(
- future: OfflineStorageService().getPendingOperations(),
- builder: (context, snapshot) {
- if (snapshot.connectionState ==
- ConnectionState.done &&
- snapshot.hasData) {
- final pendingCount = snapshot.data!.length;
- return SizedBox(
- width: double.infinity,
- child: ElevatedButton.icon(
- onPressed: pendingCount > 0
- ? () async {
- ToastUtil.show(
- '正在上传 $pendingCount 条数据...',
- );
- final syncService = SyncService();
- await syncService
- .syncPendingOperations();
- ToastUtil.success('数据上传完成');
- // 刷新按钮状态
- (context as Element).markNeedsBuild();
- }
- : null, // 禁用按钮
- icon: const Icon(Icons.upload),
- label: Text('上传数据 ($pendingCount条待上传)'),
- style: ElevatedButton.styleFrom(
- padding: const EdgeInsets.symmetric(
- horizontal: 30,
- vertical: 15,
- ),
- textStyle: const TextStyle(fontSize: 16),
- ),
- ),
- );
- }
- return const SizedBox(
- width: double.infinity,
- child: Center(child: CircularProgressIndicator()),
- );
- },
- ),
- 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),
- ),
- ),
- ),
- const SizedBox(height: 20),
- SizedBox(
- width: double.infinity,
- child: ElevatedButton.icon(
- onPressed: () async {
- final result = await showDialog(
- context: context,
- builder: (context) => const ConfigDialog(),
- );
- // 如果配置发生了变化,显示提示
- if (result == true && context.mounted) {
- ToastUtil.success('配置已保存');
- }
- },
- icon: const Icon(Icons.settings),
- label: const Text('服务配置'),
- style: ElevatedButton.styleFrom(
- padding: const EdgeInsets.symmetric(
- horizontal: 30,
- vertical: 15,
- ),
- textStyle: const TextStyle(fontSize: 16),
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- // 网络状态感知的退出登录按钮
- Consumer(
- builder: (context, ref, child) {
- final isConnectedAsync = ref.watch(isConnectedProvider);
- return SizedBox(
- width: double.infinity,
- child: ElevatedButton.icon(
- onPressed: isConnectedAsync.when(
- data: (isConnected) {
- // 在线状态下允许退出登录,离线状态下禁用
- return isConnected
- ? () {
- ToastUtil.confirm(
- '确定要退出登录吗?',
- () => authStore.logout(),
- );
- }
- : null;
- },
- loading: () => null,
- error: (_, _) => null,
- ),
- 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)),
- ],
- ),
- );
- }
- }
|