From 5947fa1f53ad9497745de9bf157332d4fbc55998 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Sat, 7 Nov 2026 12:30:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(device):=20=E6=96=B0=E5=A2=9E=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=9C=A8=E7=BA=BF=E7=8A=B6=E6=80=81=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E5=B9=B6=E7=BB=9F=E4=B8=80=E4=BE=9D=E8=B5=96=E5=88=86=E7=BB=84?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/src/main/AndroidManifest.xml | 1 - lib/features/device/domain/device_models.dart | 12 ++ .../presentation/device_manage_page.dart | 3 + .../presentation/device_online_badge.dart | 48 +++++++ .../device/presentation/device_switcher.dart | 127 +++++++++++++----- .../profile/presentation/profile_tab.dart | 8 +- lib/l10n/app_localizations.dart | 6 + lib/l10n/app_localizations_en.dart | 3 + lib/l10n/app_localizations_zh.dart | 3 + lib/l10n/intl_en.arb | 1 + lib/l10n/intl_zh.arb | 4 + pubspec.yaml | 109 ++++++++------- 12 files changed, 243 insertions(+), 82 deletions(-) create mode 100644 lib/features/device/presentation/device_online_badge.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 695c3a7..56bfcec 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -50,7 +50,6 @@ android:name=".MainActivity" android:exported="true" android:launchMode="singleTask" - android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" diff --git a/lib/features/device/domain/device_models.dart b/lib/features/device/domain/device_models.dart index bffef96..ed13e83 100644 --- a/lib/features/device/domain/device_models.dart +++ b/lib/features/device/domain/device_models.dart @@ -13,6 +13,8 @@ class DeviceBrief { this.snMobile, this.status, this.activateTime, + this.online, + this.lastHeartbeatTime, }); final String serialno; @@ -22,6 +24,14 @@ class DeviceBrief { final int? status; final String? activateTime; + /// 在线状态(1 在线 / 0 离线),后台按 Redis 心跳 TTL 判定。 + final int? online; + + /// 最后心跳时间。 + final String? lastHeartbeatTime; + + bool get isOnline => online == 1; + /// 展示名称:优先自定义备注名,缺省回退 SN。 String get displayName => snName?.isNotEmpty == true ? snName! : serialno; @@ -33,6 +43,8 @@ class DeviceBrief { snMobile: json['snMobile'] as String?, status: _toInt(json['status']), activateTime: json['activateTime']?.toString(), + online: _toInt(json['online']), + lastHeartbeatTime: json['lastHeartbeatTime']?.toString(), ); } diff --git a/lib/features/device/presentation/device_manage_page.dart b/lib/features/device/presentation/device_manage_page.dart index 6a643ea..b004d19 100644 --- a/lib/features/device/presentation/device_manage_page.dart +++ b/lib/features/device/presentation/device_manage_page.dart @@ -10,6 +10,7 @@ import '../../../l10n/app_localizations.dart'; import '../data/device_providers.dart'; import '../domain/device_models.dart'; import 'device_manage_controller.dart'; +import 'device_online_badge.dart'; /// 设备管理页(我的 → 绑定设备 → 管理)。 /// @@ -285,6 +286,8 @@ class _DeviceRow extends StatelessWidget { ), ), ), + const SizedBox(width: 6), + DeviceOnlineBadge(online: device.isOnline), if (isCurrent) ...[ const SizedBox(width: 6), Container( diff --git a/lib/features/device/presentation/device_online_badge.dart b/lib/features/device/presentation/device_online_badge.dart new file mode 100644 index 0000000..dbbe1bf --- /dev/null +++ b/lib/features/device/presentation/device_online_badge.dart @@ -0,0 +1,48 @@ +import 'package:cupertino_ui/cupertino_ui.dart'; +import 'package:ttstd_family_care/app/theme/dark_mode.dart'; + +import '../../../core/widgets/feature_ui.dart'; +import '../../../l10n/app_localizations.dart'; + +/// 设备在线状态徽标:状态圆点 +「在线 / 离线」。 +class DeviceOnlineBadge extends StatelessWidget { + const DeviceOnlineBadge({ + super.key, + required this.online, + this.fontSize = 11, + }); + + final bool online; + final double fontSize; + + @override + Widget build(BuildContext context) { + final l10n = AppLocalizations.of(context); + return Row( + mainAxisSize: MainAxisSize.min, + children: [ + Container( + width: 6, + height: 6, + decoration: BoxDecoration( + color: online + ? AppColors.green + : DarkMode.color(context, AppColors.track), + shape: BoxShape.circle, + ), + ), + const SizedBox(width: 4), + Text( + online ? l10n.deviceOnline : l10n.deviceOffline, + style: TextStyle( + fontSize: fontSize, + fontWeight: FontWeight.w600, + color: online + ? AppColors.green + : DarkMode.color(context, AppColors.sub), + ), + ), + ], + ); + } +} diff --git a/lib/features/device/presentation/device_switcher.dart b/lib/features/device/presentation/device_switcher.dart index 1f09cfa..2a5eef8 100644 --- a/lib/features/device/presentation/device_switcher.dart +++ b/lib/features/device/presentation/device_switcher.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:cupertino_ui/cupertino_ui.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; @@ -7,6 +9,7 @@ import '../../../core/widgets/feature_ui.dart'; import '../../../l10n/app_localizations.dart'; import '../data/device_providers.dart'; import '../domain/device_models.dart'; +import 'device_online_badge.dart'; /// 切换面板中「设备管理」入口的返回值标记。 const _manageDevicesValue = '__device_manage__'; @@ -30,10 +33,36 @@ Future showDeviceSwitcher(BuildContext context, WidgetRef ref) async { ref.read(selectedDeviceSnOverrideProvider.notifier).select(selected); } -/// 首页导航栏的当前设备入口:显示设备名,点击切换。 -class DeviceSwitcherButton extends ConsumerWidget { +/// 首页导航栏的当前设备入口:显示设备名与在线状态,点击切换。 +/// +/// 设备端每 30 秒上报一次心跳,后台按 Redis TTL(90s) 判定在线; +/// 此处在页面存活期间每 30 秒刷新一次设备列表,保证状态与设备近似实时。 +class DeviceSwitcherButton extends ConsumerStatefulWidget { const DeviceSwitcherButton({super.key}); + @override + ConsumerState createState() => + _DeviceSwitcherButtonState(); +} + +class _DeviceSwitcherButtonState extends ConsumerState { + Timer? _timer; + + @override + void initState() { + super.initState(); + _timer = Timer.periodic( + const Duration(seconds: 30), + (_) => ref.invalidate(myDevicesProvider), + ); + } + + @override + void dispose() { + _timer?.cancel(); + super.dispose(); + } + /// 无备注名时 SN 可能很长,导航栏只展示尾部便于区分。 String _shortLabel(DeviceBrief device) { if (device.snName?.isNotEmpty == true) return device.snName!; @@ -42,9 +71,10 @@ class DeviceSwitcherButton extends ConsumerWidget { } @override - Widget build(BuildContext context, WidgetRef ref) { + Widget build(BuildContext context) { final l10n = AppLocalizations.of(context); final device = ref.watch(currentDeviceProvider); + final online = device?.isOnline == true; return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () => showDeviceSwitcher(context, ref), @@ -60,8 +90,10 @@ class DeviceSwitcherButton extends ConsumerWidget { Container( width: 7, height: 7, - decoration: const BoxDecoration( - color: AppColors.green, + decoration: BoxDecoration( + color: online + ? AppColors.green + : DarkMode.color(context, AppColors.track), shape: BoxShape.circle, ), ), @@ -129,15 +161,28 @@ class CurrentDeviceBar extends ConsumerWidget { TextStyle(fontSize: 11.5, color: DarkMode.color(context, AppColors.sub)), ), const SizedBox(height: 2), - Text( - device?.displayName ?? l10n.homeNoDevice, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle( - fontSize: 14.5, - fontWeight: FontWeight.w600, - color: DarkMode.color(context, AppColors.ink), - ), + Row( + children: [ + Flexible( + child: Text( + device?.displayName ?? l10n.homeNoDevice, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 14.5, + fontWeight: FontWeight.w600, + color: DarkMode.color(context, AppColors.ink), + ), + ), + ), + if (device != null) ...[ + const SizedBox(width: 6), + DeviceOnlineBadge( + online: device.isOnline, + fontSize: 10.5, + ), + ], + ], ), ], ), @@ -186,15 +231,24 @@ class DeviceContextTitle extends ConsumerWidget { ), if (device != null) ...[ const SizedBox(height: 1), - Text( - device.displayName, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle( - fontSize: 10, - fontWeight: FontWeight.w400, - color: DarkMode.color(context, AppColors.sub), - ), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Flexible( + child: Text( + device.displayName, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 10, + fontWeight: FontWeight.w400, + color: DarkMode.color(context, AppColors.sub), + ), + ), + ), + const SizedBox(width: 5), + DeviceOnlineBadge(online: device.isOnline, fontSize: 10), + ], ), ], ], @@ -303,15 +357,24 @@ class _DeviceSwitcherSheet extends ConsumerWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( - device.displayName, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle( - fontSize: 14.5, - fontWeight: FontWeight.w600, - color: DarkMode.color(context, AppColors.ink), - ), + Row( + children: [ + Flexible( + child: Text( + device.displayName, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 14.5, + fontWeight: FontWeight.w600, + color: DarkMode.color( + context, AppColors.ink), + ), + ), + ), + const SizedBox(width: 6), + DeviceOnlineBadge(online: device.isOnline), + ], ), const SizedBox(height: 2), Text( diff --git a/lib/features/profile/presentation/profile_tab.dart b/lib/features/profile/presentation/profile_tab.dart index 81e5958..999ecb1 100644 --- a/lib/features/profile/presentation/profile_tab.dart +++ b/lib/features/profile/presentation/profile_tab.dart @@ -209,10 +209,12 @@ class ProfileTab extends ConsumerWidget { } Widget _deviceCard(BuildContext context, DeviceBrief device, AppLocalizations l10n) { + final status = device.isOnline ? l10n.deviceOnline : l10n.deviceOffline; final sub = device.snModel?.isNotEmpty == true - ? '${l10n.deviceOnline} · ${device.snModel}' - : l10n.deviceOnline; - return _deviceRow(context, '📱', AppColors.green, + ? '$status · ${device.snModel}' + : status; + return _deviceRow(context, '📱', + device.isOnline ? AppColors.green : DarkMode.color(context, AppColors.track), device.snName?.isNotEmpty == true ? device.snName! : device.serialno, sub, l10n.deviceManage); } diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index d46f360..d553e3a 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -1706,6 +1706,12 @@ abstract class AppLocalizations { /// **'在线'** String get deviceOnline; + /// 设备离线状态 + /// + /// In zh, this message translates to: + /// **'离线'** + String get deviceOffline; + /// 设备暂无详细信息 /// /// In zh, this message translates to: diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index e2a865c..7252efa 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -836,6 +836,9 @@ class AppLocalizationsEn extends AppLocalizations { @override String get deviceOnline => 'Online'; + @override + String get deviceOffline => 'Offline'; + @override String get deviceNoInfo => 'No details'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 9484bf9..337da12 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -822,6 +822,9 @@ class AppLocalizationsZh extends AppLocalizations { @override String get deviceOnline => '在线'; + @override + String get deviceOffline => '离线'; + @override String get deviceNoInfo => '暂无详情'; diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index 2d2b3df..328c4e3 100644 --- a/lib/l10n/intl_en.arb +++ b/lib/l10n/intl_en.arb @@ -280,6 +280,7 @@ "deviceLoadFailed": "Failed to load devices", "deviceNone": "No bound devices", "deviceOnline": "Online", + "deviceOffline": "Offline", "deviceNoInfo": "No details", "deviceManage": "Manage", "devTablet": "My Tablet Pro", diff --git a/lib/l10n/intl_zh.arb b/lib/l10n/intl_zh.arb index 7647508..e3bfcdd 100644 --- a/lib/l10n/intl_zh.arb +++ b/lib/l10n/intl_zh.arb @@ -1090,6 +1090,10 @@ "@deviceOnline": { "description": "设备在线状态" }, + "deviceOffline": "离线", + "@deviceOffline": { + "description": "设备离线状态" + }, "deviceNoInfo": "暂无详情", "@deviceNoInfo": { "description": "设备暂无详细信息" diff --git a/pubspec.yaml b/pubspec.yaml index a9fd247..df03f48 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,39 +34,10 @@ dependencies: flutter_localizations: sdk: flutter - mmkv: ^2.4.0 - flutter_baidu_mapapi_base: 3.9.9 - flutter_baidu_mapapi_map: 3.9.9 - # 百度地图定位插件:获取家属端手机真实经纬度,用于在地图上显示本机蓝色定位点 - flutter_bmflocation: ^3.8.4+1 - - # 运行时定位权限请求(Android 6.0+ / iOS 需动态申请定位权限) - permission_handler: ^12.0.1 - - # 跨平台 WebRTC(同时支持 Android 与 iOS) - flutter_webrtc: ^1.5.2 - - # 录制文件保存路径(app 专属目录,免运行时存储权限) - path_provider: ^2.1.5 - crypto: ^3.0.7 - - # WebSocket 信令通信 - web_socket_channel: ^3.0.3 - - # 安全存储:refreshToken 存 Keychain / EncryptedSharedPreferences - flutter_secure_storage: ^11.0.0 - - # 本地存储(非敏感偏好) - shared_preferences: ^2.3.0 - - # 设备 ID 生成 - uuid: ^4.4.0 - - # 官方提供的设备信息插件,用于获取 Android ID / 设备标识 - device_info_plus: ^13.1.0 - protobuf: ^6.0.0 - fixnum: ^1.1.1 - android_id: ^0.5.2+1 + # ═══════════════════════════════════════════════════════════════ + # 全平台依赖(Android / iOS / Web / Windows / macOS / Linux 均可用) + # 新增纯 Dart 包或全平台插件请放本段。 + # ═══════════════════════════════════════════════════════════════ # 状态管理(v2.x,注解 + 代码生成) flutter_riverpod: ^2.6.1 @@ -78,35 +49,81 @@ dependencies: # 网络请求(配合 json_annotation 进行 JSON 序列化) dio: ^5.7.0 + # WebSocket 信令通信 + web_socket_channel: ^3.0.3 + # 数据模型与不可变性 freezed_annotation: ^2.4.4 json_annotation: ^4.9.0 - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^1.0.8 + # 通用工具(纯 Dart) + crypto: ^3.0.7 + # 设备 ID 生成 + uuid: ^4.4.0 + protobuf: ^6.0.0 + fixnum: ^1.1.1 - # 二维码扫描(相机实时扫码 + 相册图片解析) - mobile_scanner: ^5.2.3 + # 国际化 / 中文拼音排序 + intl: any + lpinyin: ^2.0.3 + # 全平台 Flutter 插件 + # 安全存储:refreshToken 存 Keychain / EncryptedSharedPreferences + flutter_secure_storage: ^11.0.0 + # 本地存储(非敏感偏好) + shared_preferences: ^2.3.0 + # 设备信息插件,用于获取 Android ID / 设备标识 + device_info_plus: ^13.1.0 # 从相册选取图片(用于二维码图片识别) image_picker: ^1.1.2 + # UI 组件 / 图标字体(纯 Dart 实现,无平台原生代码) + # The following adds the Cupertino Icons font to your application. + # Use with the CupertinoIcons class for iOS style icons. + cupertino_icons: ^1.0.8 + cupertino_ui: ^1.1.3 + material_ui: ^1.7.0 azlistview_plus: ^3.0.0 + easy_image_viewer: ^1.5.1 + liquid_glass_widgets: ^1.16.1 + # ═══════════════════════════════════════════════════════════════ + # 非全平台依赖(平台支持受限,统一集中在本段,勿混入上方) + # ═══════════════════════════════════════════════════════════════ + + # 仅 Android / iOS + flutter_baidu_mapapi_base: 3.9.9 + flutter_baidu_mapapi_map: 3.9.9 + # 百度地图定位插件:获取家属端手机真实经纬度,用于在地图上显示本机蓝色定位点 + flutter_bmflocation: ^3.8.4+1 + + # 仅 Android + android_id: ^0.5.2+1 + # 高刷新率屏幕切换(仅 Android 生效) + flutter_displaymode: ^0.7.0 + + # Android / iOS / Windows / macOS / Linux(不支持 Web) + # 跨平台 WebRTC + flutter_webrtc: ^1.5.3 + # 高性能键值缓存 + mmkv: ^2.4.0 + # 录制文件保存路径(app 专属目录,免运行时存储权限) + path_provider: ^2.1.5 + + # Android / iOS / Web / Windows(不支持 macOS / Linux) + # 运行时定位权限请求(Android 6.0+ / iOS 需动态申请定位权限) + permission_handler: ^12.0.1 + + # Android / iOS / macOS / Web(不支持 Windows / Linux) + # 二维码扫描(相机实时扫码 + 相册图片解析) + mobile_scanner: ^5.2.3 + + # Android / iOS / macOS(不支持 Web / Windows / Linux) # 访问本机相册(读取照片/视频、按文件夹分组、按时间排序), # 统一处理 Android(6.0~12 READ_EXTERNAL_STORAGE / 13+ READ_MEDIA_IMAGES、READ_MEDIA_VIDEO) # 与 iOS(PhotoKit,含 limited 授权状态)的相册权限与读取。 photo_manager: ^3.6.0 - cupertino_ui: ^1.1.3 - material_ui: ^1.7.0 - intl: any - lpinyin: ^2.0.3 - flutter_displaymode: ^0.7.0 - easy_image_viewer: ^1.5.1 - liquid_glass_widgets: ^1.16.1 - dev_dependencies: flutter_test: sdk: flutter