mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 00:32:21 +01:00
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Replace showDatePicker, showDateRangePicker and showTimePicker with numeral grid/wheel dialogs confirmed by check and close icons. Adds a no-words picker guard and a repo invariant banning the framework pickers. Todo: e09dac36 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
4.4 KiB
Dart
156 lines
4.4 KiB
Dart
/// 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/material.dart';
|
|
import 'package:flutter/services.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<ProfileCard> createState() => _ProfileCardState();
|
|
}
|
|
|
|
class _ProfileCardState extends State<ProfileCard> {
|
|
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<void> _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<void> _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<String>(
|
|
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'),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|