fmassive/lib/edit_set.dart

125 lines
4.0 KiB
Dart
Raw Normal View History

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