Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent focusing of TextFormField when I tap suffixIcon inside the TextFormField

Tags:

flutter

When I press the button and the TextFormField will be focused and the keyboard will appear. How to fix this problem?
code below:

TextFormField(
              controller: code,
              focusNode: codeFocus,
              decoration: InputDecoration(
                labelText: 'verify code',
                prefixIcon: Icon(Icons.sms),
                suffixIcon: FlatButton(
                  onPressed:(){}
                  child: Text('send sms'),
                  textTheme: ButtonTextTheme.primary,
                ),
              ),
              keyboardType: TextInputType.number,
            ),
            focusNode: codeFocus,
          )
like image 697
Coco Avatar asked Oct 03 '19 17:10

Coco


People also ask

How do you change the suffix icon in flutter?

An icon that appears after the editable part of the text field and after the suffix or suffixText, within the decoration's container. The size and color of the suffix icon is configured automatically using an IconTheme and therefore does not need to be explicitly given in the icon widget.

What is TextFormField?

TextFormField. TextFormField wraps a TextField and integrates it with the enclosing Form . This provides additional functionality, such as validation and integration with other FormField widgets.


2 Answers

You can use stack like this.

Stack(
  alignment: Alignment.centerRight,
  children: <Widget>[
    TextFormField(
      decoration: InputDecoration(
        labelText: 'verify code',
        prefixIcon: Icon(Icons.sms),
      ),
      keyboardType: TextInputType.number,
    ),
    FlatButton(
      onPressed: () {},
      textTheme: ButtonTextTheme.primary,
      child: Text('send sms'),
    ),
  ],
)

The same question was asked few days ago by the way...

like image 51
janstol Avatar answered Oct 18 '22 12:10

janstol


Use this function when you tap on your suffixIcon.

() {
  // This will help to wait for TextField to get focus before
  // we unfocus but it will happen fast so that you won't notice.
  Future.delayed(Duration.zero, () {
    _yourInputFocusNode.unfocus();
    // Your implementation to run when tap suffixIcon in TextField
  });
},

Example

TextFormField(
  focusNode: _phoneNumberFocusNode,
  decoration: InputDecoration(
    suffixIcon: IconButton(
      icon: Icon(Icons.contacts),
      onPressed: () {
        Future.delayed(Duration.zero, () {
          _phoneNumberTwoFocusNode.unfocus();
          Navigator.pushNamed(context, 'Contacts');
        });
      },
    ),
  ),
),
like image 25
Gad Ishimwe Avatar answered Oct 18 '22 13:10

Gad Ishimwe