414 lines
14 KiB
Dart
414 lines
14 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cupertino_ui/cupertino_ui.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:ttstd_family_care/app/theme/dark_mode.dart';
|
|
|
|
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__';
|
|
|
|
/// 打开设备切换面板;未绑定设备时直接进入设备管理页。
|
|
Future<void> showDeviceSwitcher(BuildContext context, WidgetRef ref) async {
|
|
final devices = ref.read(myDevicesProvider).value ?? const <DeviceBrief>[];
|
|
if (devices.isEmpty) {
|
|
await context.push('/device/manage');
|
|
return;
|
|
}
|
|
final selected = await showCupertinoModalPopup<String>(
|
|
context: context,
|
|
builder: (_) => _DeviceSwitcherSheet(devices: devices),
|
|
);
|
|
if (selected == null || !context.mounted) return;
|
|
if (selected == _manageDevicesValue) {
|
|
await context.push('/device/manage');
|
|
return;
|
|
}
|
|
ref.read(selectedDeviceSnOverrideProvider.notifier).select(selected);
|
|
}
|
|
|
|
/// 首页导航栏的当前设备入口:显示设备名与在线状态,点击切换。
|
|
///
|
|
/// 设备端每 30 秒上报一次心跳,后台按 Redis TTL(90s) 判定在线;
|
|
/// 此处在页面存活期间每 30 秒刷新一次设备列表,保证状态与设备近似实时。
|
|
class DeviceSwitcherButton extends ConsumerStatefulWidget {
|
|
const DeviceSwitcherButton({super.key});
|
|
|
|
@override
|
|
ConsumerState<DeviceSwitcherButton> createState() =>
|
|
_DeviceSwitcherButtonState();
|
|
}
|
|
|
|
class _DeviceSwitcherButtonState extends ConsumerState<DeviceSwitcherButton> {
|
|
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!;
|
|
final sn = device.serialno;
|
|
return sn.length > 10 ? sn.substring(sn.length - 10) : sn;
|
|
}
|
|
|
|
@override
|
|
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),
|
|
child: Container(
|
|
padding: const EdgeInsets.fromLTRB(10, 5, 7, 5),
|
|
decoration: BoxDecoration(
|
|
color: DarkMode.color(context, AppColors.surface),
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
width: 7,
|
|
height: 7,
|
|
decoration: BoxDecoration(
|
|
color: online
|
|
? AppColors.green
|
|
: DarkMode.color(context, AppColors.track),
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
const SizedBox(width: 6),
|
|
ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 110),
|
|
child: Text(
|
|
device == null ? l10n.homeBindDevice : _shortLabel(device),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
color: DarkMode.color(context, AppColors.ink),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 3),
|
|
Icon(
|
|
CupertinoIcons.chevron_down,
|
|
size: 11,
|
|
color: DarkMode.color(context, AppColors.sub),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 「当前设备」提示条:展示正在查看的设备并提供切换入口。
|
|
class CurrentDeviceBar extends ConsumerWidget {
|
|
const CurrentDeviceBar({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final device = ref.watch(currentDeviceProvider);
|
|
return CardBox(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () => showDeviceSwitcher(context, ref),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 36,
|
|
height: 36,
|
|
decoration: BoxDecoration(
|
|
color: AppColors.green,
|
|
borderRadius: BorderRadius.circular(11),
|
|
),
|
|
child: const Center(
|
|
child: Text('📱', style: TextStyle(fontSize: 17)),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
l10n.currentDevice,
|
|
style:
|
|
TextStyle(fontSize: 11.5, color: DarkMode.color(context, AppColors.sub)),
|
|
),
|
|
const SizedBox(height: 2),
|
|
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,
|
|
),
|
|
],
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Text(
|
|
l10n.switchDevice,
|
|
style: const TextStyle(
|
|
fontSize: 12.5,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.green,
|
|
),
|
|
),
|
|
const Icon(
|
|
CupertinoIcons.chevron_right,
|
|
size: 14,
|
|
color: AppColors.green,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 按设备操作的子页面标题:主标题下方展示当前设备名,避免多设备下混淆对象。
|
|
class DeviceContextTitle extends ConsumerWidget {
|
|
const DeviceContextTitle(this.title, {super.key});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final device = ref.watch(currentDeviceProvider);
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(
|
|
fontSize: 17,
|
|
fontWeight: FontWeight.w600,
|
|
color: DarkMode.color(context, AppColors.ink),
|
|
),
|
|
),
|
|
if (device != null) ...[
|
|
const SizedBox(height: 1),
|
|
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),
|
|
],
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _DeviceSwitcherSheet extends ConsumerWidget {
|
|
const _DeviceSwitcherSheet({required this.devices});
|
|
|
|
final List<DeviceBrief> devices;
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final currentSn = ref.watch(selectedDeviceSnProvider);
|
|
return Container(
|
|
constraints: BoxConstraints(
|
|
maxHeight: MediaQuery.sizeOf(context).height * 0.7,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: CupertinoColors.systemBackground.resolveFrom(context),
|
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
|
|
),
|
|
child: SafeArea(
|
|
top: false,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 18, 12, 6),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
l10n.switchDeviceTitle,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w700,
|
|
color: DarkMode.color(context, AppColors.ink),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () =>
|
|
Navigator.of(context).pop(_manageDevicesValue),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 8,
|
|
vertical: 4,
|
|
),
|
|
child: Text(
|
|
l10n.deviceManageTitle,
|
|
style: const TextStyle(
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.green,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Flexible(
|
|
child: ListView.separated(
|
|
shrinkWrap: true,
|
|
padding: const EdgeInsets.only(bottom: 8),
|
|
itemCount: devices.length,
|
|
separatorBuilder: (_, _) => const Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 20),
|
|
child: AppDivider(),
|
|
),
|
|
itemBuilder: (_, i) {
|
|
final device = devices[i];
|
|
final selected = device.serialno == currentSn;
|
|
final subtitle = device.snModel?.isNotEmpty == true
|
|
? '${device.serialno} · ${device.snModel}'
|
|
: device.serialno;
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () => Navigator.of(context).pop(device.serialno),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 20,
|
|
vertical: 12,
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 36,
|
|
height: 36,
|
|
decoration: BoxDecoration(
|
|
color: selected
|
|
? AppColors.green.withValues(alpha: 0.12)
|
|
: DarkMode.color(context, AppColors.surface),
|
|
borderRadius: BorderRadius.circular(11),
|
|
),
|
|
child: const Center(
|
|
child:
|
|
Text('📱', style: TextStyle(fontSize: 17)),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
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(
|
|
subtitle,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: DarkMode.color(context, AppColors.sub),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Icon(
|
|
selected
|
|
? CupertinoIcons.check_mark_circled_solid
|
|
: CupertinoIcons.circle,
|
|
size: 20,
|
|
color:
|
|
selected ? AppColors.green : DarkMode.color(context, AppColors.track),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|