/// Birth date, height and sex -- the synced profile the formulas need. library; import 'package:diet_guard_app/models/local_time.dart'; import 'package:diet_guard_app/services/body_service.dart'; import 'package:diet_guard_app/ui/cues.dart'; import 'package:diet_guard_app/ui/theme.dart'; import 'package:diet_guard_app/widgets/body/body_section.dart'; import 'package:diet_guard_app/widgets/pickers/pickers.dart'; import 'package:flutter/services.dart'; import 'package:material_ui/material_ui.dart'; /// Edits the profile; calls [onSaved] after a successful save. class ProfileCard extends StatefulWidget { /// Creates the card. const ProfileCard({required this.onSaved, super.key}); /// Called once the profile has been written. final VoidCallback onSaved; @override State createState() => _ProfileCardState(); } class _ProfileCardState extends State { final _height = TextEditingController(); String? _birth; String? _sex; Widget? _status; @override void initState() { super.initState(); final body = BodyService.instance; _birth = body.birth; _sex = body.sex; final height = body.heightCm; if (height != null) _height.text = oneDecimal(height); } @override void dispose() { _height.dispose(); super.dispose(); } Future _pickBirth() async { final now = DateTime.now(); final initial = DateTime.tryParse(_birth ?? '') ?? DateTime(now.year - 30); final picked = await pickDate( context: context, firstDate: DateTime(1900), lastDate: now, initialDate: initial, initialDatePickerMode: DatePickerMode.year, ); if (picked == null || !mounted) return; setState(() => _birth = localDateKey(picked)); } Future _save() async { final height = double.tryParse(_height.text.replaceAll(',', '.')); if (height == null || height < 50 || height > 260) { setState( () => _status = StatusCue( key: UniqueKey(), kind: CueKind.error, icon: Icons.height, semanticLabel: 'Height must be 50-260 cm.', ), ); return; } await BodyService.instance.setProfile( birth: _birth, heightCm: height, sex: _sex, ); if (!mounted) return; setState( () => _status = StatusCue( key: UniqueKey(), kind: CueKind.ok, semanticLabel: 'Saved.', ), ); widget.onSaved(); } @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; final birth = _birth; final status = _status; return BodySection( semanticLabel: 'Profile', children: [ Align( alignment: Alignment.centerLeft, child: TextButton( onPressed: _pickBirth, child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon( Icons.cake, color: birth == null ? scheme.error : null, semanticLabel: 'Birth date', ), if (birth != null) Text(' $birth'), ], ), ), ), IconField( icon: Icons.height, semanticLabel: 'Height (cm)', controller: _height, keyboardType: const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp('[0-9.,]')), ], ), const SizedBox(height: AppSpacing.sm), SegmentedButton( showSelectedIcon: false, segments: const [ ButtonSegment( value: 'm', icon: Icon(Icons.male, semanticLabel: 'Male'), ), ButtonSegment( value: 'f', icon: Icon(Icons.female, semanticLabel: 'Female'), ), ], selected: {?_sex}, emptySelectionAllowed: true, onSelectionChanged: (value) => setState(() => _sex = value.isEmpty ? null : value.first), ), const SizedBox(height: AppSpacing.sm), Row( children: [ if (status != null) Expanded(child: status) else const Spacer(), IconButton.filled( onPressed: _save, icon: const Icon(Icons.check, semanticLabel: 'Save profile'), ), ], ), ], ); } }