|
|
@@ -1,3 +1,5 @@
|
|
|
+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';
|
|
|
@@ -5,6 +7,8 @@ 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});
|
|
|
@@ -80,6 +84,49 @@ class ProfilePage extends ConsumerWidget {
|
|
|
),
|
|
|
),
|
|
|
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(
|
|
|
@@ -104,28 +151,71 @@ class ProfilePage extends ConsumerWidget {
|
|
|
),
|
|
|
),
|
|
|
),
|
|
|
+ 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),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
],
|
|
|
),
|
|
|
),
|
|
|
),
|
|
|
- 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,
|
|
|
+ // 网络状态感知的退出登录按钮
|
|
|
+ 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),
|
|
|
+ ),
|
|
|
),
|
|
|
- textStyle: const TextStyle(fontSize: 16),
|
|
|
- ),
|
|
|
- ),
|
|
|
+ );
|
|
|
+ },
|
|
|
),
|
|
|
] else ...[
|
|
|
const Center(child: Text('加载中...')),
|