341 lines
13 KiB
Dart
341 lines
13 KiB
Dart
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 'package:ttstd_family_care/core/widgets/cupertino_hint.dart';
|
|
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
|
|
|
|
import '../../../core/widgets/feature_ui.dart';
|
|
import '../../../core/widgets/settings_ui.dart';
|
|
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';
|
|
|
|
/// 设备管理页(我的 → 绑定设备 → 管理)。
|
|
///
|
|
/// 列出当前账号已绑定的全部设备,每项尾部提供「解绑」按钮;
|
|
/// 右上角提供「添加设备」入口跳转到绑定页。
|
|
/// 支持下拉刷新;每次进入页面都会重新请求设备列表。
|
|
class DeviceManagePage extends ConsumerStatefulWidget {
|
|
const DeviceManagePage({super.key});
|
|
|
|
@override
|
|
ConsumerState<DeviceManagePage> createState() => _DeviceManagePageState();
|
|
}
|
|
|
|
class _DeviceManagePageState extends ConsumerState<DeviceManagePage> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// 每次进入页面强制重新请求设备列表(GoRouter 每次 push 都会创建新页面实例,
|
|
// 但 myDevicesProvider 有缓存,需 invalidate 才会重新拉取)。
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) ref.invalidate(myDevicesProvider);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final devicesAsync = ref.watch(myDevicesProvider);
|
|
final currentSn = ref.watch(selectedDeviceSnProvider);
|
|
final action = ref.watch(deviceManageControllerProvider);
|
|
|
|
// 一次性提示:展示后立即消费,避免重建时重复弹窗。
|
|
ref.listen<DeviceManageState>(deviceManageControllerProvider, (prev, next) {
|
|
final alert = next.alert;
|
|
if (alert == null) return;
|
|
ref.read(deviceManageControllerProvider.notifier).consumeAlert();
|
|
showCupertinoHint(context, alert);
|
|
});
|
|
|
|
return PopScope(
|
|
canPop: false,
|
|
onPopInvokedWithResult: (didPop, _) {
|
|
if (didPop) return;
|
|
if (context.canPop()) {
|
|
context.pop();
|
|
} else {
|
|
context.go('/profile');
|
|
}
|
|
},
|
|
child: AppPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: Text(l10n.deviceManageTitle),
|
|
leading: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
child: const Icon(CupertinoIcons.back),
|
|
onPressed: () => context.pop(),
|
|
),
|
|
trailing: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
child: const Icon(CupertinoIcons.add, size: 26),
|
|
onPressed: () => context.push('/device/bind'),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Stack(
|
|
children: [
|
|
// 下拉刷新:重新请求设备列表。
|
|
CustomScrollView(
|
|
physics: const AlwaysScrollableScrollPhysics(
|
|
parent: BouncingScrollPhysics(),
|
|
),
|
|
slivers: [
|
|
CupertinoSliverRefreshControl(
|
|
onRefresh: () => ref.refresh(myDevicesProvider.future),
|
|
),
|
|
SliverPadding(
|
|
padding: const EdgeInsets.only(bottom: 24),
|
|
sliver: SliverList(
|
|
delegate: SliverChildListDelegate([
|
|
SectionTitle(l10n.deviceBound),
|
|
if ((devicesAsync.valueOrNull?.length ?? 0) > 1)
|
|
Padding(
|
|
padding:
|
|
const EdgeInsets.fromLTRB(18, 0, 18, 6),
|
|
child: Text(
|
|
l10n.deviceSwitchHint,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: DarkMode.color(context, AppColors.sub),
|
|
),
|
|
),
|
|
),
|
|
devicesAsync.when(
|
|
loading: () => const CardBox(
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 20),
|
|
child: Center(child: CupertinoActivityIndicator()),
|
|
),
|
|
),
|
|
error: (e, _) => CardBox(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
l10n.deviceLoadFailed,
|
|
style: TextStyle(fontSize: 13, color: DarkMode.color(context, AppColors.sub)),
|
|
),
|
|
const SizedBox(height: 10),
|
|
// 局部错误:内联展示 + retry(见 AGENTS.md §12)。
|
|
GestureDetector(
|
|
onTap: () => ref.invalidate(myDevicesProvider),
|
|
child: Text(
|
|
l10n.commonRetry,
|
|
style: const TextStyle(
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w700,
|
|
color: AppColors.green,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
data: (devices) => devices.isEmpty
|
|
? CardBox(
|
|
child: Text(
|
|
l10n.deviceNone,
|
|
style: TextStyle(fontSize: 13, color: DarkMode.color(context, AppColors.sub)),
|
|
),
|
|
)
|
|
: CardBox(
|
|
padding: EdgeInsets.zero,
|
|
child: Column(
|
|
children: [
|
|
for (var i = 0; i < devices.length; i++) ...[
|
|
if (i > 0) const AppDivider(),
|
|
_DeviceRow(
|
|
device: devices[i],
|
|
isCurrent:
|
|
devices[i].serialno == currentSn,
|
|
onSelect: () => ref
|
|
.read(
|
|
selectedDeviceSnOverrideProvider
|
|
.notifier,
|
|
)
|
|
.select(devices[i].serialno),
|
|
onUnbind: () =>
|
|
_confirmUnbind(context, devices[i], l10n),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SecondaryButton(
|
|
text: l10n.deviceManageAdd,
|
|
icon: CupertinoIcons.add_circled,
|
|
onPressed: () => context.push('/device/bind'),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
// 解绑请求进行中:全屏蒙层拦截重复点击。
|
|
if (action.submitting)
|
|
const Positioned.fill(
|
|
child: ColoredBox(
|
|
color: Color(0x33000000),
|
|
child: Center(child: CupertinoActivityIndicator(radius: 14)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 解绑二次确认,确认后调用控制器并提示结果。
|
|
Future<void> _confirmUnbind(
|
|
BuildContext context,
|
|
DeviceBrief device,
|
|
AppLocalizations l10n,
|
|
) async {
|
|
final name = device.snName?.isNotEmpty == true ? device.snName! : device.serialno;
|
|
final confirmed = await AppDialogs.confirm(
|
|
context,
|
|
title: l10n.deviceUnbindConfirmTitle,
|
|
message: l10n.deviceUnbindConfirmMessage(name),
|
|
confirmText: l10n.deviceUnbind,
|
|
cancelText: l10n.commonCancel,
|
|
);
|
|
if (!confirmed) return;
|
|
|
|
final ok = await ref
|
|
.read(deviceManageControllerProvider.notifier)
|
|
.unbind(device.serialno);
|
|
if (!ok || !context.mounted) return;
|
|
|
|
// 解绑成功后重新请求设备列表;若解绑的是当前设备,
|
|
// selectedDeviceSnProvider 会自动回退到新列表首个设备。
|
|
ref.invalidate(myDevicesProvider);
|
|
|
|
showCupertinoHint(context, l10n.deviceUnbindSuccess);
|
|
}
|
|
}
|
|
|
|
/// 设备列表项:左侧图标 + 名称/SN,右侧解绑按钮。
|
|
///
|
|
/// 点击非当前设备所在行可将其切换为当前设备(列表上方有提示文案)。
|
|
class _DeviceRow extends StatelessWidget {
|
|
const _DeviceRow({
|
|
required this.device,
|
|
required this.isCurrent,
|
|
required this.onSelect,
|
|
required this.onUnbind,
|
|
});
|
|
|
|
final DeviceBrief device;
|
|
final bool isCurrent;
|
|
final VoidCallback onSelect;
|
|
final VoidCallback onUnbind;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final subtitle = device.snModel?.isNotEmpty == true
|
|
? '${device.serialno} · ${device.snModel}'
|
|
: device.serialno;
|
|
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: isCurrent ? null : onSelect,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
|
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: [
|
|
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),
|
|
if (isCurrent) ...[
|
|
const SizedBox(width: 6),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 6, vertical: 1),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.green.withValues(alpha: 0.12),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
l10n.deviceCurrentTag,
|
|
style: const TextStyle(
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.green,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
subtitle,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: TextStyle(fontSize: 12, color: DarkMode.color(context, AppColors.sub)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
GestureDetector(
|
|
onTap: onUnbind,
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 7),
|
|
decoration: BoxDecoration(
|
|
color: DarkMode.tone(context, const Color(0xFFFEF2F2), const Color(0xFF492A2D)),
|
|
borderRadius: BorderRadius.circular(18),
|
|
border: Border.all(color: AppColors.red.withValues(alpha: 0.4)),
|
|
),
|
|
child: Text(
|
|
l10n.deviceUnbind,
|
|
style: const TextStyle(
|
|
fontSize: 12.5,
|
|
fontWeight: FontWeight.w700,
|
|
color: AppColors.red,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|