diet-guard/app/lib/widgets/body/profile_card.dart
Krzysztof kuhy Rudnicki e761926df4
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Body tab: weight/body-fat logs, BMI, ideal weight, goal-based kcal target, workouts + Health Connect steps, week planner
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-28 09:03:03 +02:00

120 lines
3.5 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/theme.dart';
import 'package:diet_guard_app/widgets/body/body_section.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;
String? _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 showDatePicker(
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 = 'Height must be 50-260 cm.');
return;
}
await BodyService.instance.setProfile(
birth: _birth,
heightCm: height,
sex: _sex,
);
if (!mounted) return;
setState(() => _status = 'Saved.');
widget.onSaved();
}
@override
Widget build(BuildContext context) {
return BodySection(
title: 'Profile',
children: [
Row(
children: [
Expanded(child: Text('Born: ${_birth ?? 'not set'}')),
TextButton.icon(
icon: const Icon(Icons.cake),
label: const Text('Birth date'),
onPressed: _pickBirth,
),
],
),
TextField(
controller: _height,
decoration: const InputDecoration(labelText: 'Height (cm)'),
keyboardType: const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9.,]')),
],
),
const SizedBox(height: AppSpacing.sm),
SegmentedButton<String>(
segments: const [
ButtonSegment(value: 'm', label: Text('Male')),
ButtonSegment(value: 'f', label: Text('Female')),
],
selected: {?_sex},
emptySelectionAllowed: true,
onSelectionChanged: (value) =>
setState(() => _sex = value.isEmpty ? null : value.first),
),
const SizedBox(height: AppSpacing.sm),
Row(
children: [
Expanded(child: BodyNote(_status ?? 'Syncs to the PC.')),
FilledButton(onPressed: _save, child: const Text('Save profile')),
],
),
],
);
}
}