import 'package:flutter/material.dart'; import 'package:flutter/material.dart' as material; import 'package:fmassive/database.dart'; import 'package:fmassive/main.dart'; import 'package:moor_flutter/moor_flutter.dart'; class EditGymSetPage extends StatefulWidget { final GymSetsCompanion gymSet; const EditGymSetPage({required this.gymSet, super.key}); @override createState() => _EditGymSetPageState(); } class _EditGymSetPageState extends State { final TextEditingController _nameController = TextEditingController(); final TextEditingController _repsController = TextEditingController(); final TextEditingController _weightController = TextEditingController(); late GymSetsCompanion gymSet; final nameNode = FocusNode(); final repsNode = FocusNode(); @override void initState() { super.initState(); gymSet = widget.gymSet; _nameController.text = gymSet.name.value; _repsController.text = gymSet.reps.value.toString(); _weightController.text = gymSet.weight.value.toString(); if (gymSet.id.present) repsNode.requestFocus(); else nameNode.requestFocus(); } @override dispose() { nameNode.dispose(); repsNode.dispose(); super.dispose(); } @override Widget build(BuildContext context) { List actions = []; if (widget.gymSet.id.present) actions.add(IconButton( onPressed: () async { await db.gymSets.deleteOne(widget.gymSet); if (!mounted) return; Navigator.pop(context); }, icon: const Icon(Icons.delete))); return Scaffold( appBar: AppBar(title: const Text('Edit Gym Set'), actions: actions), body: Padding( padding: const EdgeInsets.all(16.0), child: material.Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ TextFormField( controller: _nameController, focusNode: nameNode, decoration: const InputDecoration(labelText: 'Name'), onTap: () { _nameController.selection = TextSelection( baseOffset: 0, extentOffset: _nameController.text.length); }, onChanged: (value) { setState(() { gymSet = gymSet.copyWith(name: Value(value)); }); }, ), TextFormField( controller: _repsController, focusNode: repsNode, onTap: () { _repsController.selection = TextSelection( baseOffset: 0, extentOffset: _repsController.text.length); }, decoration: const InputDecoration(labelText: 'Reps'), keyboardType: TextInputType.number, onChanged: (value) { setState(() { gymSet = gymSet.copyWith(reps: Value(int.tryParse(value) ?? 0)); }); }, ), TextFormField( controller: _weightController, decoration: const InputDecoration(labelText: 'Weight (kg)'), keyboardType: TextInputType.number, onTap: () { _weightController.selection = TextSelection( baseOffset: 0, extentOffset: _weightController.text.length); }, onChanged: (value) { setState(() { gymSet = gymSet.copyWith(weight: Value(int.tryParse(value) ?? 0)); }); }, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () async { if (gymSet.id.present) await db.update(db.gymSets).write(gymSet); else await db.into(db.gymSets).insert(gymSet); if (!mounted) return; Navigator.pop(context); }, child: const Icon(Icons.check), ), ); } }