IncorrectMouse
IncorrectMouse

Reputation: 209

Flutter - cannot edit text in textfield without tapping on it

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

Answers (1)

Related Questions