diet-guard/app/lib/widgets/body/profile_card.dart
Krzysztof kuhy Rudnicki 49144a2c49 Move the app to material_ui for Flutter 3.50
Flutter 3.50 deprecates package:flutter/material.dart; 73 deprecation
infos failed flutter analyze --fatal-infos, and four Dart pins were
behind (crdt_sync_flutter 0.8.2, flutter_local_notifications 23.0.2,
github_device_auth 0.5.0, sync_settings_ui 0.8.2). Those UI packages
now sit on material_ui, so the app has to as well or their widgets lose
MaterialLocalizations.

Imports swap one-for-one and dart fix re-sorted the directives; no
behaviour change. analyze is clean and all six test shards pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
2026-11-16 09:25:34 +01:00

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/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<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'),
),
],
),
],
);
}
}