fmassive/lib/edit_set.dart

100 lines
3.1 KiB
Dart
Raw Normal View History

2023-04-07 11:50:02 +12:00
import 'package:flutter/material.dart';
2023-04-07 15:56:17 +12:00
import 'package:fmassive/gym_set.dart';
2023-04-07 11:50:02 +12:00
class EditGymSetPage extends StatefulWidget {
final GymSet gymSet;
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();
late GymSet _editedGymSet;
2023-04-07 15:56:17 +12:00
late GymSetDatabaseHelper _db;
2023-04-07 11:50:02 +12:00
@override
void initState() {
super.initState();
2023-04-07 15:56:17 +12:00
_db = GymSetDatabaseHelper.instance;
2023-04-07 11:50:02 +12:00
_editedGymSet = GymSet(
2023-04-07 15:56:17 +12:00
id: widget.gymSet.id,
2023-04-07 11:50:02 +12:00
name: widget.gymSet.name,
reps: widget.gymSet.reps,
weight: widget.gymSet.weight,
created: DateTime.now());
_nameController.text = _editedGymSet.name;
_repsController.text = _editedGymSet.reps.toString();
_weightController.text = _editedGymSet.weight.toString();
}
@override
Widget build(BuildContext context) {
return Scaffold(
2023-04-07 15:56:17 +12:00
appBar: AppBar(title: const Text('Edit Gym Set'), actions: [
IconButton(
onPressed: () async {
await _db.delete(_editedGymSet);
if (!mounted) return;
Navigator.pop(context, _editedGymSet);
},
icon: const Icon(Icons.delete))
]),
2023-04-07 11:50:02 +12:00
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _nameController,
decoration: const InputDecoration(labelText: 'Name'),
onChanged: (value) {
setState(() {
_editedGymSet.name = value;
});
},
),
TextFormField(
controller: _repsController,
decoration: const InputDecoration(labelText: 'Reps'),
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
_editedGymSet.reps = int.tryParse(value) ?? 0;
});
},
),
TextFormField(
controller: _weightController,
decoration: const InputDecoration(labelText: 'Weight (kg)'),
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
_editedGymSet.weight = int.tryParse(value) ?? 0;
});
},
),
],
),
),
floatingActionButton: FloatingActionButton(
2023-04-07 15:56:17 +12:00
onPressed: () async {
print('edited gym set id: ${_editedGymSet.id}');
if (_editedGymSet.id != null)
await _db.update(_editedGymSet);
else
await _db.insert(_editedGymSet);
if (!mounted) return;
2023-04-07 11:50:02 +12:00
Navigator.pop(context, _editedGymSet);
},
child: const Icon(Icons.check),
),
);
}
}