- 引入 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
292 lines
9.2 KiB
Dart
292 lines
9.2 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/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 '../../../l10n/app_localizations.dart';
|
|
|
|
/// 闹钟新增 / 编辑页。
|
|
///
|
|
/// [alarm] 为空表示新增;否则为编辑(可删除)。
|
|
class AlarmEditPage extends ConsumerStatefulWidget {
|
|
const AlarmEditPage({super.key, this.alarm});
|
|
|
|
final AlarmModel? alarm;
|
|
|
|
@override
|
|
ConsumerState<AlarmEditPage> createState() => _AlarmEditPageState();
|
|
}
|
|
|
|
class _AlarmEditPageState extends ConsumerState<AlarmEditPage> {
|
|
late int _hour;
|
|
late int _minute;
|
|
late final TextEditingController _labelController;
|
|
late int _repeatType;
|
|
late bool _isEnabled;
|
|
bool _saving = false;
|
|
|
|
AppLocalizations get _l10n => AppLocalizations.of(context);
|
|
|
|
bool get _isEdit => widget.alarm != null;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final a = widget.alarm;
|
|
_hour = a?.hour ?? 7;
|
|
_minute = a?.minute ?? 0;
|
|
_labelController = TextEditingController(text: a?.label ?? '');
|
|
_repeatType = a?.repeatType ?? 1;
|
|
_isEnabled = a?.isEnabled ?? true;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_labelController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
if (_saving) return;
|
|
final l10n = _l10n;
|
|
final sn = ref.read(selectedDeviceSnProvider);
|
|
if (sn == null || sn.isEmpty) {
|
|
_alert(l10n.contactNoDevice);
|
|
return;
|
|
}
|
|
final base = widget.alarm ?? const AlarmModel();
|
|
final alarm = base.copyWith(
|
|
hour: _hour,
|
|
minute: _minute,
|
|
label: _labelController.text.trim(),
|
|
repeatType: _repeatType,
|
|
isEnabled: _isEnabled,
|
|
);
|
|
setState(() => _saving = true);
|
|
final err = _isEdit
|
|
? await ref.read(alarmApiProvider).updateAlarm(sn, alarm)
|
|
: (await ref.read(alarmApiProvider).addAlarm(sn, alarm)).error;
|
|
if (!mounted) return;
|
|
setState(() => _saving = false);
|
|
if (err != null) {
|
|
_alert(err);
|
|
return;
|
|
}
|
|
context.pop(true);
|
|
}
|
|
|
|
Future<void> _delete() async {
|
|
final l10n = _l10n;
|
|
final sn = ref.read(selectedDeviceSnProvider);
|
|
final id = widget.alarm?.id;
|
|
if (sn == null || sn.isEmpty || id == null) return;
|
|
final confirmed = await showCupertinoDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => CupertinoAlertDialog(
|
|
title: Text(l10n.alarmDeleteConfirm),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.of(ctx).pop(false),
|
|
child: Text(l10n.commonCancel),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDestructiveAction: true,
|
|
onPressed: () => Navigator.of(ctx).pop(true),
|
|
child: Text(l10n.commonDelete),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (confirmed != true || !mounted || _saving) return;
|
|
setState(() => _saving = true);
|
|
final err = await ref.read(alarmApiProvider).deleteAlarm(sn, id);
|
|
if (!mounted) return;
|
|
setState(() => _saving = false);
|
|
if (err != null) {
|
|
_alert(err);
|
|
return;
|
|
}
|
|
context.pop(true);
|
|
}
|
|
|
|
void _alert(String message) {
|
|
showCupertinoDialog<void>(
|
|
context: context,
|
|
builder: (ctx) => CupertinoAlertDialog(
|
|
content: Text(message),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.of(ctx).pop(),
|
|
child: Text(_l10n.commonConfirm),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = _l10n;
|
|
return AppPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: Text(_isEdit ? l10n.alarmEditTitle : l10n.addAlarm),
|
|
leading: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
child: const Icon(CupertinoIcons.back),
|
|
onPressed: () => context.pop(),
|
|
),
|
|
trailing: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: _saving ? null : _save,
|
|
child: _saving
|
|
? const CupertinoActivityIndicator(radius: 10)
|
|
: Text(
|
|
l10n.commonSave,
|
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: ListView(
|
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 32),
|
|
children: [
|
|
// 时间选择器
|
|
CardBox(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
l10n.alarmTime,
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
color: DarkMode.color(context, AppColors.sub),
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: 160,
|
|
child: CupertinoDatePicker(
|
|
mode: CupertinoDatePickerMode.time,
|
|
use24hFormat: true,
|
|
initialDateTime: DateTime(2000, 1, 1, _hour, _minute),
|
|
onDateTimeChanged: (d) {
|
|
setState(() {
|
|
_hour = d.hour;
|
|
_minute = d.minute;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
// 标签
|
|
CardBox(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
child: CupertinoTextField(
|
|
controller: _labelController,
|
|
placeholder: l10n.alarmLabel,
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 4,
|
|
vertical: 14,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
// 重复
|
|
CardBox(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
l10n.alarmRepeat,
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
color: DarkMode.color(context, AppColors.sub),
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
CupertinoSegmentedControl<int>(
|
|
groupValue: _repeatType,
|
|
onValueChanged: (v) => setState(() => _repeatType = v),
|
|
children: {
|
|
0: Text(l10n.alarmRepeatOnce),
|
|
1: Text(l10n.alarmRepeatEveryday),
|
|
2: Text(l10n.alarmRepeatWeekdays),
|
|
if (widget.alarm?.repeatType == 3)
|
|
3: Text(l10n.alarmRepeatCustom),
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
// 开启
|
|
CardBox(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
l10n.alarmEnabled,
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.w600,
|
|
color: DarkMode.color(context, AppColors.ink),
|
|
),
|
|
),
|
|
),
|
|
CupertinoSwitch(
|
|
value: _isEnabled,
|
|
activeTrackColor: AppColors.green,
|
|
onChanged: (v) => setState(() => _isEnabled = v),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (_isEdit) ...[
|
|
const SizedBox(height: 24),
|
|
CupertinoButton(
|
|
borderRadius: BorderRadius.circular(14),
|
|
color: DarkMode.tone(
|
|
context,
|
|
const Color(0xFFFEE2E2),
|
|
const Color(0xFF492A2D),
|
|
),
|
|
pressedOpacity: 0.7,
|
|
onPressed: _saving ? null : _delete,
|
|
child: SizedBox(
|
|
width: double.infinity,
|
|
child: Center(
|
|
child: Text(
|
|
l10n.alarmDelete,
|
|
style: TextStyle(
|
|
color: DarkMode.tone(
|
|
context,
|
|
const Color(0xFFDC2626),
|
|
const Color(0xFFFF9299),
|
|
),
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|