Reputation: 209
So, I have a TextField that looks like:
TextField(
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
controller: _amountController,
autofocus: true,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: '0',
hintStyle: TextStyle(color: Painter.gray500),
border: InputBorder.none,
prefixIcon: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: SvgPicture.asset(icon),
),
prefixIconConstraints: BoxConstraints(
maxHeight: 20,
maxWidth: 20,
),
),
),
This is in a bottom sheet that I use to add data. I also want to enable edits, so on tap the bottom sheet loads the TextField with existing value, if any. For that, the _amountController
is initialized like:
AddDataSheet({
required this.title,
required this.subtitle,
this.amount,
}) {
_amountController = TextEditingController.fromValue(
amount == null
? TextEditingValue.empty
: TextEditingValue(
text: amount.toString()
),
);
}
The issue is, when the TextField loads, it comes on with focus but I cannot edit it until I tap on it. I tried adding this,
selection: TextSelection.fromPosition(
TextPosition(offset: amount.toString().length),
),
to the controller initialization but it doesn't work.
How can I have the TextField so that I don't have to tap on it to start editing the value?
Upvotes: 0
Views: 1778
Reputation: 81
I think you need to use focusnode property
https://api.flutter.dev/flutter/material/TextField/focusNode.html
Also please refer to these answers https://stackoverflow.com/a/49912570/9414608 https://stackoverflow.com/a/60510624/9414608
Upvotes: 1