Auto focus and select text for edit_set

This commit is contained in:
Brandon Presley 2023-04-11 11:47:03 +12:00
parent 4d3de751f3
commit a8b6c9f8a3

View File

@ -18,6 +18,8 @@ class _EditGymSetPageState extends State<EditGymSetPage> {
final TextEditingController _repsController = TextEditingController(); final TextEditingController _repsController = TextEditingController();
final TextEditingController _weightController = TextEditingController(); final TextEditingController _weightController = TextEditingController();
late GymSetsCompanion gymSet; late GymSetsCompanion gymSet;
final nameNode = FocusNode();
final repsNode = FocusNode();
@override @override
void initState() { void initState() {
@ -26,6 +28,17 @@ class _EditGymSetPageState extends State<EditGymSetPage> {
_nameController.text = gymSet.name.value; _nameController.text = gymSet.name.value;
_repsController.text = gymSet.reps.value.toString(); _repsController.text = gymSet.reps.value.toString();
_weightController.text = gymSet.weight.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 @override
@ -49,7 +62,12 @@ class _EditGymSetPageState extends State<EditGymSetPage> {
children: [ children: [
TextFormField( TextFormField(
controller: _nameController, controller: _nameController,
focusNode: nameNode,
decoration: const InputDecoration(labelText: 'Name'), decoration: const InputDecoration(labelText: 'Name'),
onTap: () {
_nameController.selection = TextSelection(
baseOffset: 0, extentOffset: _nameController.text.length);
},
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
gymSet = gymSet.copyWith(name: Value(value)); gymSet = gymSet.copyWith(name: Value(value));
@ -58,6 +76,11 @@ class _EditGymSetPageState extends State<EditGymSetPage> {
), ),
TextFormField( TextFormField(
controller: _repsController, controller: _repsController,
focusNode: repsNode,
onTap: () {
_repsController.selection = TextSelection(
baseOffset: 0, extentOffset: _repsController.text.length);
},
decoration: const InputDecoration(labelText: 'Reps'), decoration: const InputDecoration(labelText: 'Reps'),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
onChanged: (value) { onChanged: (value) {
@ -71,6 +94,10 @@ class _EditGymSetPageState extends State<EditGymSetPage> {
controller: _weightController, controller: _weightController,
decoration: const InputDecoration(labelText: 'Weight (kg)'), decoration: const InputDecoration(labelText: 'Weight (kg)'),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
onTap: () {
_weightController.selection = TextSelection(
baseOffset: 0, extentOffset: _weightController.text.length);
},
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
gymSet = gymSet =