profile.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'dart:io';
  2. import 'package:chicken_farm/core/config/app_config.dart';
  3. import 'package:chicken_farm/stores/auth_store.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_riverpod/flutter_riverpod.dart';
  6. import 'package:chicken_farm/pages/home/_profile/user_info_card.dart';
  7. import 'package:chicken_farm/pages/home/_profile/upload_data_button.dart';
  8. import 'package:chicken_farm/pages/home/_profile/clear_cache_button.dart';
  9. import 'package:chicken_farm/pages/home/_profile/config_button.dart';
  10. import 'package:chicken_farm/pages/home/_profile/dict_config_button.dart';
  11. import 'package:chicken_farm/pages/home/_profile/power_config_button.dart';
  12. import 'package:chicken_farm/pages/home/_profile/win_card_reader_button.dart';
  13. // import 'package:chicken_farm/pages/home/_profile/rfid_config_button.dart';
  14. // import 'package:chicken_farm/pages/home/_profile/scanner_light_button.dart';
  15. import 'package:chicken_farm/pages/home/_profile/logout_button.dart';
  16. import 'package:chicken_farm/pages/home/_profile/export_data_button.dart';
  17. final tapCountProvider = StateProvider<int>((ref) => 0);
  18. final lastTapTimeProvider = StateProvider<DateTime>((ref) => DateTime.now());
  19. class ProfilePage extends ConsumerWidget {
  20. const ProfilePage({super.key});
  21. @override
  22. Widget build(BuildContext context, WidgetRef ref) {
  23. final authState = ref.watch(authStoreProvider);
  24. final tapCount = ref.watch(tapCountProvider);
  25. // 判断是否应该显示字典维护按钮 (连续点击5次后显示)
  26. final showDictConfig = tapCount >= 5;
  27. void handleUserCardTap() {
  28. final now = DateTime.now();
  29. final previousTapTime = ref.read(lastTapTimeProvider);
  30. final tapCountNotifier = ref.read(tapCountProvider.notifier);
  31. final lastTapTimeNotifier = ref.read(lastTapTimeProvider.notifier);
  32. // 如果距离上次点击超过1秒,则重置计数
  33. if (now.difference(previousTapTime).inMilliseconds > 1000) {
  34. tapCountNotifier.state = 1;
  35. } else {
  36. tapCountNotifier.state = tapCount + 1;
  37. // 如果达到5次点击,显示提示
  38. if (tapCountNotifier.state == 5) {
  39. ScaffoldMessenger.of(context).showSnackBar(
  40. const SnackBar(
  41. content: Text('已解锁字典维护功能'),
  42. duration: Duration(seconds: 2),
  43. ),
  44. );
  45. }
  46. }
  47. lastTapTimeNotifier.state = now;
  48. }
  49. return Scaffold(
  50. key: super.key,
  51. body: Padding(
  52. padding: const EdgeInsets.all(20),
  53. child: Center(
  54. // 添加居中组件
  55. child: SingleChildScrollView(
  56. child: SizedBox(
  57. width: Platform.isAndroid
  58. ? MediaQuery.of(context).size.width *
  59. 0.8 // 使用屏幕宽度的80%
  60. : MediaQuery.of(context).size.width > 350
  61. ? 350 // 限制最大宽度
  62. : MediaQuery.of(context).size.width,
  63. child: Column(
  64. crossAxisAlignment: CrossAxisAlignment.center,
  65. mainAxisAlignment: MainAxisAlignment.center,
  66. children: [
  67. if (authState.state == AuthState.authenticated) ...[
  68. if (AppConfig.isOffline) ...[
  69. GestureDetector(
  70. onTap: handleUserCardTap,
  71. child: UserInfoCard(user: authState.user!),
  72. ),
  73. const SizedBox(height: 20),
  74. if (showDictConfig) ...[
  75. const DictConfigButton(),
  76. const SizedBox(height: 20),
  77. ],
  78. if (AppConfig.isPda) ...[
  79. const PowerConfigButton(),
  80. const SizedBox(height: 20),
  81. ],
  82. const ClearCacheButton(),
  83. const SizedBox(height: 20),
  84. const ExportDataButton(),
  85. const SizedBox(height: 20),
  86. ] else ...[
  87. UserInfoCard(user: authState.user!),
  88. const SizedBox(height: 20),
  89. if (AppConfig.isPda) ...[
  90. const PowerConfigButton(),
  91. const SizedBox(height: 20),
  92. const UploadDataButton(),
  93. const SizedBox(height: 20),
  94. ],
  95. const ClearCacheButton(),
  96. const SizedBox(height: 20),
  97. const ConfigButton(),
  98. const SizedBox(height: 20),
  99. if (!AppConfig.isPda &&
  100. !AppConfig.isOffline &&
  101. Platform.isWindows) ...[
  102. const WinCardReaderButton(),
  103. const SizedBox(height: 20),
  104. ],
  105. // const RfidConfigButton(),
  106. // const SizedBox(height: 20),
  107. // const ScannerLightButton(),
  108. // const SizedBox(height: 20),
  109. const LogoutButton(),
  110. const SizedBox(height: 20),
  111. ],
  112. Text(
  113. _buildVersionText(),
  114. textAlign: TextAlign.center,
  115. style: TextStyle(fontSize: 12, color: Colors.grey),
  116. ),
  117. ],
  118. ],
  119. ),
  120. ),
  121. ),
  122. ),
  123. ),
  124. );
  125. }
  126. String _buildVersionText() {
  127. final deviceType = AppConfig.isPda ? "PDA" : "APP";
  128. final mode = AppConfig.isOffline ? "_OFFLINE" : "";
  129. final deviceInfo = Platform.isAndroid ? "$deviceType$mode" : "WIN$mode";
  130. return 'ChickenFarm $deviceInfo Powered by IWB ©2026 \nVersion: ${AppConfig.appVersion}';
  131. }
  132. }