- 引入 DarkMode 工具类,提供系统栏、颜色、色调的暗色适配能力 - AppColors 调色板改为 CupertinoDynamicColor.withBrightness 自适应 - app.dart 增加 AnnotatedRegion 与 Material Theme 包裹以联动暗色系统栏 - 移除 system_ui_util 中硬编码的浅色图标配置,改由主题驱动 - CardBox、AppDivider、SectionTitle 等公共组件适配深色卡片与分割线 - settings_ui、change_mobile、change_password、login_devices、wechat_binding 等页面全面接入 DarkMode 颜色 - dio_client 新增 allowStale 响应缓存机制,网络异常时回退本地缓存 - token_storage 增加 currentAccountId(JWT 解析)及按用户隔离的设备偏好存取 - ApiException 改为通过私有构造持有真实 AppError 实例,appError getter 返回结构化错误 - 闹钟模块迁移至 FutureProvider.autoDispose.family,消除手动 loading 状态管理 - 闹钟编辑页 CupertinoTimerPicker 替换为 CupertinoDatePicker(24h 格式),标签输入改为 TextEditingController
253 lines
8.3 KiB
Dart
253 lines
8.3 KiB
Dart
import 'package:cupertino_ui/cupertino_ui.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:ttstd_family_care/app/theme/dark_mode.dart';
|
||
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
|
||
|
||
import '../../../core/widgets/feature_ui.dart';
|
||
import '../../../features/alarm/data/alarm_providers.dart';
|
||
import '../../../features/alarm/domain/alarm_models.dart';
|
||
import '../../../features/device/data/device_providers.dart';
|
||
import '../../../features/device/presentation/device_switcher.dart';
|
||
import '../../../l10n/app_localizations.dart';
|
||
import '../../../core/network/error_message.dart';
|
||
|
||
/// 闹钟页(管理 → 闹钟)。
|
||
///
|
||
/// 展示设备闹钟列表(后端同步),支持新增 / 编辑 / 删除 / 开关切换;
|
||
/// 变更后由后端推送 contentType=25 到设备端覆盖同步。
|
||
class AlarmPage extends ConsumerStatefulWidget {
|
||
const AlarmPage({super.key});
|
||
|
||
@override
|
||
ConsumerState<AlarmPage> createState() => _AlarmPageState();
|
||
}
|
||
|
||
class _AlarmPageState extends ConsumerState<AlarmPage> {
|
||
bool _updating = false;
|
||
|
||
AppLocalizations get _l10n => AppLocalizations.of(context);
|
||
|
||
Future<void> _openEdit([AlarmModel? alarm]) async {
|
||
final sn = ref.read(selectedDeviceSnProvider);
|
||
if (sn == null || _updating) return;
|
||
final changed = await context.push<bool>('/alarm/edit', extra: alarm);
|
||
if (mounted && changed == true) ref.invalidate(deviceAlarmsProvider(sn));
|
||
}
|
||
|
||
Future<void> _toggle(AlarmModel alarm, bool value) async {
|
||
final sn = ref.read(selectedDeviceSnProvider);
|
||
if (sn == null || _updating) return;
|
||
setState(() => _updating = true);
|
||
final error = await ref
|
||
.read(alarmApiProvider)
|
||
.updateAlarm(sn, alarm.copyWith(isEnabled: value));
|
||
if (!mounted) return;
|
||
setState(() => _updating = false);
|
||
ref.invalidate(deviceAlarmsProvider(sn));
|
||
if (error != null) _showAlert(error);
|
||
}
|
||
|
||
void _showAlert(String message) {
|
||
showCupertinoDialog<void>(
|
||
context: context,
|
||
builder: (ctx) => CupertinoAlertDialog(
|
||
content: Text(message),
|
||
actions: [
|
||
CupertinoDialogAction(
|
||
onPressed: () => Navigator.of(ctx).pop(),
|
||
child: Text(_l10n.commonConfirm),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
String _repeatText(AlarmModel a) {
|
||
final l10n = _l10n;
|
||
switch (a.repeatType) {
|
||
case 0:
|
||
return l10n.alarmRepeatOnce;
|
||
case 2:
|
||
return l10n.alarmRepeatWeekdays;
|
||
case 3:
|
||
return a.customDays.isEmpty
|
||
? l10n.alarmRepeatCustom
|
||
: '${l10n.alarmRepeatCustom} · ${a.customDays}';
|
||
default:
|
||
return l10n.alarmRepeatEveryday;
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final l10n = _l10n;
|
||
final sn = ref.watch(selectedDeviceSnProvider);
|
||
final alarms = sn == null ? null : ref.watch(deviceAlarmsProvider(sn));
|
||
return PopScope(
|
||
canPop: false,
|
||
onPopInvokedWithResult: (didPop, _) {
|
||
if (didPop) return;
|
||
if (context.canPop()) {
|
||
context.pop();
|
||
} else {
|
||
context.go('/messages');
|
||
}
|
||
},
|
||
child: AppPageScaffold(
|
||
navigationBar: CupertinoNavigationBar(
|
||
middle: DeviceContextTitle(l10n.alarmTitle),
|
||
leading: CupertinoButton(
|
||
padding: EdgeInsets.zero,
|
||
onPressed: () => context.pop(),
|
||
child: const Icon(CupertinoIcons.back),
|
||
),
|
||
trailing: CupertinoButton(
|
||
padding: EdgeInsets.zero,
|
||
onPressed: sn == null || _updating ? null : () => _openEdit(),
|
||
child: const Icon(CupertinoIcons.add, size: 26),
|
||
),
|
||
),
|
||
child: SafeArea(
|
||
child: alarms == null
|
||
? Center(child: Text(l10n.contactNoDevice))
|
||
: alarms.when(
|
||
skipLoadingOnRefresh: false,
|
||
loading: () => const Center(
|
||
child: CupertinoActivityIndicator(radius: 14),
|
||
),
|
||
error: (error, _) => Center(
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(16),
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Text(
|
||
userMessageOf(error),
|
||
textAlign: TextAlign.center,
|
||
),
|
||
CupertinoButton(
|
||
onPressed: () =>
|
||
ref.invalidate(deviceAlarmsProvider(sn!)),
|
||
child: Text(l10n.commonRetry),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
data: (items) => items.isEmpty
|
||
? _buildEmpty(l10n)
|
||
: ListView.separated(
|
||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||
itemCount: items.length + 1,
|
||
separatorBuilder: (_, _) =>
|
||
const SizedBox(height: 12),
|
||
itemBuilder: (_, i) => i == items.length
|
||
? _buildAddTile(l10n)
|
||
: _buildAlarmCard(items[i], l10n),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildEmpty(AppLocalizations l10n) {
|
||
return Center(
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Icon(CupertinoIcons.alarm, size: 56, color: DarkMode.color(context, AppColors.sub)),
|
||
const SizedBox(height: 12),
|
||
Text(
|
||
l10n.alarmEmpty,
|
||
style: TextStyle(fontSize: 14, color: DarkMode.color(context, AppColors.sub)),
|
||
),
|
||
const SizedBox(height: 20),
|
||
CupertinoButton.filled(
|
||
onPressed: () => _openEdit(),
|
||
child: Text(l10n.addAlarm),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildAddTile(AppLocalizations l10n) {
|
||
return GestureDetector(
|
||
onTap: () => _openEdit(),
|
||
child: Container(
|
||
margin: const EdgeInsets.symmetric(horizontal: 16),
|
||
height: 50,
|
||
decoration: BoxDecoration(
|
||
border: Border.all(color: const Color(0xFFCBD5E1), width: 2),
|
||
borderRadius: BorderRadius.circular(14),
|
||
),
|
||
child: Center(
|
||
child: Text(
|
||
'+ ${l10n.addAlarm}',
|
||
style: const TextStyle(
|
||
fontSize: 15,
|
||
fontWeight: FontWeight.w600,
|
||
color: Color(0xFF6B7280),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildAlarmCard(AlarmModel a, AppLocalizations l10n) {
|
||
return GestureDetector(
|
||
onTap: () => _openEdit(a),
|
||
child: CardBox(
|
||
child: Row(
|
||
children: [
|
||
RichText(
|
||
text: TextSpan(
|
||
children: [
|
||
TextSpan(
|
||
text: a.timeText,
|
||
style: TextStyle(
|
||
fontSize: 30,
|
||
fontWeight: FontWeight.w700,
|
||
color: DarkMode.color(context, AppColors.ink),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
const SizedBox(width: 14),
|
||
Expanded(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Text(
|
||
a.label.isEmpty ? l10n.addAlarm : a.label,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(
|
||
fontSize: 15,
|
||
fontWeight: FontWeight.w600,
|
||
color: DarkMode.color(context, AppColors.ink),
|
||
),
|
||
),
|
||
Text(
|
||
_repeatText(a),
|
||
style: TextStyle(fontSize: 12, color: DarkMode.color(context, AppColors.sub)),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
CupertinoSwitch(
|
||
value: a.isEnabled,
|
||
activeTrackColor: AppColors.green,
|
||
onChanged: _updating ? null : (v) => _toggle(a, v),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|