Files
ttstd_family_care/lib/features/account/presentation/change_mobile_page.dart
T
tongtong 71dd8eb52c feat(theme): 添加全局深色模式支持与主题适配
- 引入 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
2026-11-07 02:35:30 +08:00

222 lines
7.3 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 '../../../core/widgets/settings_ui.dart';
import '../../../l10n/app_localizations.dart';
import 'account_controller.dart';
/// 更换手机页(账号与安全 → 更换手机)。
///
/// 流程:输入新手机号 → 获取验证码(60s 倒计时)→ 输入验证码与账号密码 → 提交。
class ChangeMobilePage extends ConsumerStatefulWidget {
const ChangeMobilePage({super.key, this.currentMobile});
/// 当前绑定手机号,用于顶部脱敏展示(可选)。
final String? currentMobile;
@override
ConsumerState<ChangeMobilePage> createState() => _ChangeMobilePageState();
}
class _ChangeMobilePageState extends ConsumerState<ChangeMobilePage> {
final TextEditingController _mobileController = TextEditingController();
final TextEditingController _codeController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
/// 中国大陆手机号校验。
static final RegExp _mobileRule = RegExp(r'^1[3-9]\d{9}$');
@override
void dispose() {
_mobileController.dispose();
_codeController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final action = ref.watch(accountControllerProvider);
ref.listen<AccountActionState>(accountControllerProvider, (prev, next) {
final alert = next.alert;
if (alert == null) return;
ref.read(accountControllerProvider.notifier).consumeAlert();
AppDialogs.alert(context, message: alert, confirmText: l10n.commonConfirm);
});
final counting = action.countdown > 0;
return AppPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text(l10n.changeMobileTitle),
leading: CupertinoButton(
padding: EdgeInsets.zero,
child: const Icon(CupertinoIcons.back),
onPressed: () => context.pop(),
),
),
child: SafeArea(
child: ListView(
padding: const EdgeInsets.only(top: 8, bottom: 24),
children: [
if (widget.currentMobile?.isNotEmpty == true)
CardBox(
padding: EdgeInsets.zero,
child: SettingsRow(
emoji: '📞',
color: AppColors.gray,
title: l10n.changeMobileCurrent,
value: _mask(widget.currentMobile!),
trailing: Text(
_mask(widget.currentMobile!),
style: TextStyle(fontSize: 13, color: DarkMode.color(context, AppColors.sub)),
),
),
),
CardBox(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Column(
children: [
LabeledField(
label: l10n.changeMobileNew,
controller: _mobileController,
keyboardType: TextInputType.phone,
maxLength: 11,
),
LabeledField(
label: l10n.changeMobileCode,
controller: _codeController,
keyboardType: TextInputType.number,
maxLength: 6,
suffix: _CodeButton(
text: counting
? l10n.changeMobileResendIn(action.countdown)
: l10n.changeMobileSendCode,
enabled: !counting && !action.sendingCode,
loading: action.sendingCode,
onPressed: () => _sendCode(l10n),
),
),
LabeledField(
label: l10n.changeMobilePassword,
controller: _passwordController,
obscureText: true,
),
],
),
),
const SizedBox(height: 6),
PrimaryButton(
text: l10n.commonSave,
loading: action.submitting,
onPressed: () => _submit(l10n),
),
],
),
),
);
}
/// 校验新手机号后发送验证码。
Future<void> _sendCode(AppLocalizations l10n) async {
final mobile = _mobileController.text.trim();
if (!_mobileRule.hasMatch(mobile)) {
await AppDialogs.alert(
context,
message: l10n.changeMobileInvalid,
confirmText: l10n.commonConfirm,
);
return;
}
await ref.read(accountControllerProvider.notifier).sendChangeMobileCode(mobile);
}
/// 校验三项输入后提交更换请求。
Future<void> _submit(AppLocalizations l10n) async {
final mobile = _mobileController.text.trim();
final code = _codeController.text.trim();
final password = _passwordController.text;
final error = !_mobileRule.hasMatch(mobile)
? l10n.changeMobileInvalid
: code.isEmpty
? l10n.changeMobileCodeEmpty
: password.isEmpty
? l10n.changeMobilePasswordEmpty
: null;
if (error != null) {
await AppDialogs.alert(
context,
message: error,
confirmText: l10n.commonConfirm,
);
return;
}
final ok = await ref.read(accountControllerProvider.notifier).changeMobile(
newMobile: mobile,
code: code,
password: password,
);
if (!ok || !mounted) return;
await AppDialogs.alert(
context,
message: l10n.changeMobileSuccess,
confirmText: l10n.commonConfirm,
);
if (mounted) context.pop();
}
/// 手机号脱敏:保留前 3 后 4 位。
String _mask(String mobile) {
if (mobile.length < 7) return mobile;
return '${mobile.substring(0, 3)}****${mobile.substring(mobile.length - 4)}';
}
}
/// 「获取验证码 / 倒计时」按钮。
class _CodeButton extends StatelessWidget {
const _CodeButton({
required this.text,
required this.enabled,
required this.loading,
required this.onPressed,
});
final String text;
final bool enabled;
final bool loading;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) => GestureDetector(
onTap: enabled ? onPressed : null,
child: Container(
height: 44,
padding: const EdgeInsets.symmetric(horizontal: 14),
decoration: BoxDecoration(
color: enabled ? AppColors.green : DarkMode.color(context, AppColors.track),
borderRadius: BorderRadius.circular(12),
),
child: Center(
child: loading
? const CupertinoActivityIndicator(color: CupertinoColors.white)
: Text(
text,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: enabled ? CupertinoColors.white : DarkMode.color(context, AppColors.sub),
),
),
),
),
);
}