fmassive/lib/edit_set.dart

100 lines
3.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:fmassive/gym_set.dart';
class EditGymSetPage extends StatefulWidget {
final GymSet gymSet;
const EditGymSetPage({required this.gymSet, super.key});
@override
createState() => _EditGymSetPageState();
}
class _EditGymSetPageState extends State<EditGymSetPage> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _repsController = TextEditingController();
final TextEditingController _weightController = TextEditingController();
late GymSet _editedGymSet;
late GymSetDatabaseHelper _db;
@override
void initState() {
super.initState();
_db = GymSetDatabaseHelper.instance;
_editedGymSet = GymSet(
id: widget.gymSet.id,
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(
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))
]),
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(
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;
Navigator.pop(context, _editedGymSet);
},
child: const Icon(Icons.check),
),
);
}
}