I have textformfields like this : If i don't define a keyboardType for the second textformfield, there is no problem when i go through from first one to second field, i don't lose the first field's input. But if i define keyboardtype in my code, it loses the first field's data when i click on the second field.
Note : I am using riverpod as State Management.
class NewTx extends StatelessWidget {
final TextEditingController titleController = TextEditingController(text: '');
final TextEditingController priceController = TextEditingController(text: '');
@override
Widget build(BuildContext context) {
return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
TextFormField(
controller: titleController,
decoration: InputDecoration(hintText: 'Title')
),
TextFormField(
keyboardType: TextInputType.number,
controller: priceController,
decoration: InputDecoration(hintText: 'Title')),
Container(
alignment: AlignmentDirectional.bottomEnd,
child: IconButton(
icon: Icon(Icons.add),
onPressed: () => context.read(transactionProvider).addTx(
Transaction(titleController.text, priceController.text))))
]);
}
}
Edit : riverpod_hooks and useTextEditingController is working perfectly.(Another solution than stateful widget.)
I think issue is not in loosing focus, but rather in the fact that widget is rebuilt, it's class recreated and as consequence - controllers keeping text recreated.
I suggest changing NewTx to stateful widget and moving controllers to its state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With