294fc4a781
用户实测已因日期选择器误录:Flutter 原生日历只给年份网格、月份必须靠 < > 逐月点,从 9 月回 4 月要点 5 次,他把当月(2026-09)的就医记录记成了 2026-04-09,进而误判「本月花费 ¥0」是聚合坏了。 新增 lib/core/widgets/app_date_picker.dart,全仓 7 处裸 showDatePicker 收口 (改造后 lib/ 下 showDatePicker 只出现在该文件内部一次): - pickAppDate(...):calendar 首屏 + 保留头部铅笔切手输;initialDate 自动夹进 [firstDate, lastDate] 防原生越界断言(调用方常传「当前值 ?? 今天」,而 「到期日期」的 firstDate 就是今天,历史值可能已越界);返回值统一抹时分秒; 中文文案一律交给 zh-CN 本地化,不硬编码,避免两处文案漂移。 - AppDateFieldTrailing(...):7 处日期行统一「今天 + 日历图标」。今天越界自动 隐藏按钮、提交中禁用、触控 44×44、primaryStrong 白底 4.49:1。 入口模式取舍:不用 calendarOnly——它恰好会砍掉手输按钮,把「录一个已知日期」 这条唯一快路堵死;也不用 input 首屏——「记今天」这类高频场景敲 8 个数字更慢。 两条路都留着最省事。 「今天」为何放表单行而非弹窗内:原生 showDatePicker 无法注入自定义动作 (builder 只能包裹整个 Dialog,拿不到内部选中态;塞进 Column 还会因 Dialog 在无界高度下贪心布局而溢出)。放表单行反而更快——一键落值连弹窗都不用开, 把容易走错的月份导航整段绕开,且一次实现 7 处形态完全一致。 各调用点原有的 firstDate/lastDate 业务约束原样传入、一字未改(健康事件 不许未来、到期日不许补记过去、疫苗 allowFuture 双态、生日不许未来), 并由 widget 测试直接断言 DatePickerDialog.firstDate/lastDate 防后续悄悄放宽。 顺带修配色:此前从未定制 datePickerTheme,选中日直接吃 ColorScheme.fromSeed 由珊瑚橙派生的暗红棕,与品牌脱节。新增 _datePickerTheme 只复用 05 号规范 (iteration-2/05、iteration-3/05)已审计的色对,不新造色值:头部 surfaceTint + primaryDark 7.98:1(选中 chip 同款)、选中日/年 primaryStrong 实底白字 4.49:1、今日 primaryStrong 1.5px 描边、星期表头 inkSoft 6.59:1、 越界日 muted(DEBT-2 允许的禁用态用途)。headerHeadlineStyle 取 22px (默认 32):中文「9月10日周四」在横屏侧栏头部 26px 起就折行。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
295 lines
9.8 KiB
Dart
295 lines
9.8 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/semantics.dart';
|
||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||
import 'package:patbond_flutter/features/pets/health_record_display.dart';
|
||
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
||
import 'package:patbond_flutter/features/pets/pet_models.dart';
|
||
import 'package:patbond_flutter/features/pets/pets_repository.dart';
|
||
|
||
/// 体重录入表单页(T2-13)。
|
||
///
|
||
/// - `weightKg` 前端校验对齐契约:(0, 500]、最多两位小数;后端 40000 兜底。
|
||
/// - 称重时间默认「现在」,可回选日期(非今日取当日 12:00,保持
|
||
/// measured_at DESC 排序直觉);提交前转 UTC,ISO 8601 带 Z 上送。
|
||
/// - 错误分层沿用登录纵切:字段 errorText / InlineErrorBanner / SnackBar。
|
||
/// - 埋点:首次输入 health_record_create_started(recordType=weight),
|
||
/// 成功/失败按 06 §1.4 经 [HealthRecordAnalytics] 上报。
|
||
class WeightFormPage extends StatefulWidget {
|
||
const WeightFormPage({
|
||
required this.repository,
|
||
required this.petId,
|
||
super.key,
|
||
this.analytics,
|
||
this.entryPoint = HealthRecordEntryPoint.recordList,
|
||
});
|
||
|
||
final PetsRepository repository;
|
||
final String petId;
|
||
final HealthRecordAnalytics? analytics;
|
||
final HealthRecordEntryPoint entryPoint;
|
||
|
||
@override
|
||
State<WeightFormPage> createState() => _WeightFormPageState();
|
||
}
|
||
|
||
class _WeightFormPageState extends State<WeightFormPage> {
|
||
final _weightCtrl = TextEditingController();
|
||
final _noteCtrl = TextEditingController();
|
||
|
||
DateTime _measuredDate = DateTime.now();
|
||
String? _weightError;
|
||
String? _formError;
|
||
bool _submitting = false;
|
||
|
||
bool _startedFired = false;
|
||
int _attemptSeq = 0;
|
||
late final DateTime _openedAt;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_openedAt = DateTime.now();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_weightCtrl.dispose();
|
||
_noteCtrl.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
void _markStarted() {
|
||
if (_startedFired) return;
|
||
_startedFired = true;
|
||
widget.analytics?.createStarted(
|
||
recordType: HealthRecordType.weight,
|
||
entryPoint: widget.entryPoint,
|
||
);
|
||
}
|
||
|
||
void _trackFailed(HealthRecordFailureReason reason, [int? errorCode]) {
|
||
widget.analytics?.createFailed(
|
||
recordType: HealthRecordType.weight,
|
||
reason: reason,
|
||
attemptSeq: _attemptSeq,
|
||
errorCode: errorCode,
|
||
);
|
||
}
|
||
|
||
String? _validateWeight() {
|
||
final text = _weightCtrl.text.trim();
|
||
if (text.isEmpty) return '请输入体重';
|
||
if (parseWeightKgInput(text) == null) {
|
||
return '体重需大于 0 且不超过 500 公斤,最多两位小数';
|
||
}
|
||
return null;
|
||
}
|
||
|
||
void _showFormError(String message) {
|
||
setState(() => _formError = message);
|
||
SemanticsService.sendAnnouncement(
|
||
View.of(context),
|
||
message,
|
||
TextDirection.ltr,
|
||
);
|
||
}
|
||
|
||
/// 称重时刻:今日取此刻,历史日期取当日 12:00(本地),提交前转 UTC。
|
||
DateTime _measuredAt() {
|
||
final now = DateTime.now();
|
||
final sameDay =
|
||
_measuredDate.year == now.year &&
|
||
_measuredDate.month == now.month &&
|
||
_measuredDate.day == now.day;
|
||
final local = sameDay
|
||
? now
|
||
: DateTime(
|
||
_measuredDate.year,
|
||
_measuredDate.month,
|
||
_measuredDate.day,
|
||
12,
|
||
);
|
||
return local.toUtc();
|
||
}
|
||
|
||
Future<void> _submit() async {
|
||
if (_submitting) return;
|
||
_attemptSeq++;
|
||
final weightError = _validateWeight();
|
||
if (weightError != null) {
|
||
setState(() => _weightError = weightError);
|
||
_trackFailed(HealthRecordFailureReason.validationError);
|
||
return;
|
||
}
|
||
|
||
setState(() {
|
||
_submitting = true;
|
||
_formError = null;
|
||
});
|
||
try {
|
||
final note = _noteCtrl.text.trim();
|
||
final record = await widget.repository.createWeight(
|
||
widget.petId,
|
||
CreateWeightRequest(
|
||
weightKg: parseWeightKgInput(_weightCtrl.text)!,
|
||
measuredAt: _measuredAt(),
|
||
note: note.isEmpty ? null : note,
|
||
),
|
||
);
|
||
widget.analytics?.createSucceeded(
|
||
recordType: HealthRecordType.weight,
|
||
durationMs: DateTime.now().difference(_openedAt).inMilliseconds,
|
||
);
|
||
if (mounted) Navigator.of(context).pop(record);
|
||
} on PetAccessDeniedException {
|
||
if (!mounted) return;
|
||
_showFormError('你没有权限为该宠物添加记录');
|
||
_trackFailed(HealthRecordFailureReason.permissionDenied, 40300);
|
||
} on PetNotFoundException {
|
||
if (!mounted) return;
|
||
final navigator = Navigator.of(context);
|
||
ScaffoldMessenger.of(
|
||
context,
|
||
).showSnackBar(const SnackBar(content: Text('宠物不存在或已被删除')));
|
||
_trackFailed(HealthRecordFailureReason.notFound, 40401);
|
||
navigator.pop();
|
||
} on ApiRateLimitException {
|
||
if (!mounted) return;
|
||
_showFormError('操作过于频繁,请稍后再试');
|
||
_trackFailed(HealthRecordFailureReason.rateLimited);
|
||
} on ApiBusinessException catch (error) {
|
||
if (!mounted) return;
|
||
final isParam = error.code == ApiCodes.paramError;
|
||
_showFormError(isParam ? '请检查填写内容后重试' : '保存失败,请稍后重试');
|
||
_trackFailed(
|
||
isParam
|
||
? HealthRecordFailureReason.validationError
|
||
: HealthRecordFailureReason.serverError,
|
||
error.code,
|
||
);
|
||
} on ApiNetworkException {
|
||
if (!mounted) return;
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(
|
||
content: const Text('网络异常,请检查网络后重试'),
|
||
action: SnackBarAction(label: '重试', onPressed: _submit),
|
||
),
|
||
);
|
||
_trackFailed(HealthRecordFailureReason.networkError);
|
||
} on SessionExpiredException {
|
||
// 会话失效:认证状态机自动回登录页。
|
||
} finally {
|
||
if (mounted) setState(() => _submitting = false);
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
backgroundColor: Colors.transparent,
|
||
elevation: 0,
|
||
foregroundColor: AppColors.ink,
|
||
title: const Text('记录体重'),
|
||
centerTitle: true,
|
||
titleTextStyle: const TextStyle(
|
||
color: AppColors.ink,
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.w800,
|
||
),
|
||
),
|
||
body: SafeArea(
|
||
child: ListView(
|
||
padding: const EdgeInsets.fromLTRB(20, 8, 20, 30),
|
||
children: [
|
||
AppTextField(
|
||
label: '体重(公斤)',
|
||
controller: _weightCtrl,
|
||
prefixIcon: Icons.monitor_weight_outlined,
|
||
errorText: _weightError,
|
||
helperText: '大于 0 且不超过 500,最多两位小数',
|
||
enabled: !_submitting,
|
||
keyboardType: const TextInputType.numberWithOptions(
|
||
decimal: true,
|
||
),
|
||
textInputAction: TextInputAction.next,
|
||
onChanged: (_) {
|
||
_markStarted();
|
||
if (_weightError != null || _formError != null) {
|
||
setState(() {
|
||
_weightError = null;
|
||
_formError = null;
|
||
});
|
||
}
|
||
},
|
||
),
|
||
const SizedBox(height: 14),
|
||
ListTile(
|
||
shape: RoundedRectangleBorder(
|
||
side: const BorderSide(color: AppColors.border),
|
||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||
),
|
||
tileColor: AppColors.surface,
|
||
leading: const Icon(Icons.event_outlined, color: AppColors.muted),
|
||
title: const Text('称重日期', style: TextStyle(fontSize: 14)),
|
||
subtitle: Text(
|
||
dateToJson(_measuredDate),
|
||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||
),
|
||
trailing: AppDateFieldTrailing(
|
||
firstDate: DateTime(1990),
|
||
lastDate: DateTime.now(),
|
||
enabled: !_submitting,
|
||
onToday: (value) {
|
||
_markStarted();
|
||
setState(() => _measuredDate = value);
|
||
},
|
||
),
|
||
enabled: !_submitting,
|
||
onTap: () async {
|
||
final now = DateTime.now();
|
||
final value = await pickAppDate(
|
||
context: context,
|
||
initialDate: _measuredDate,
|
||
firstDate: DateTime(1990),
|
||
lastDate: now,
|
||
);
|
||
if (value != null && mounted) {
|
||
_markStarted();
|
||
setState(() => _measuredDate = value);
|
||
}
|
||
},
|
||
),
|
||
const SizedBox(height: 14),
|
||
AppTextField(
|
||
label: '备注(可选,如:饭后称重)',
|
||
controller: _noteCtrl,
|
||
prefixIcon: Icons.sticky_note_2_outlined,
|
||
enabled: !_submitting,
|
||
textInputAction: TextInputAction.done,
|
||
onChanged: (_) => _markStarted(),
|
||
onSubmitted: (_) => _submit(),
|
||
),
|
||
if (_formError != null) ...[
|
||
const SizedBox(height: 16),
|
||
InlineErrorBanner(message: _formError!),
|
||
],
|
||
const SizedBox(height: 24),
|
||
PrimaryButton(
|
||
label: '保存记录',
|
||
isLoading: _submitting,
|
||
onPressed: _submit,
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|