Flutter - how to add done button in number keyboard in flutter

后端 未结 5 574
甜味超标
甜味超标 2021-02-05 12:49

I am trying to add done button in number type input for a TextFormField in flutter but I could not able to do that.

TextFormField(
              


        
相关标签:
5条回答
  • 2021-02-05 13:08

    Please try change to

    inputType: const TextInputType.numberWithOptions(signed: true),
    inputFormatters: <TextInputFormatter>[
       FilteringTextInputFormatter.digitsOnly,
    ],
    
    0 讨论(0)
  • 2021-02-05 13:10

    I've just created a package for add basic actions to the current keyboards .

    You can take a look here :

    https://pub.dartlang.org/packages/keyboard_actions

    Usage :

        import  'package:flutter/material.dart';
        import  'package:keyboard_actions/keyboard_actions.dart';
    
         //...
          FocusNode _nodeText1 = FocusNode();
          FocusNode _nodeText2 = FocusNode();
          FocusNode _nodeText3 = FocusNode();
          FocusNode _nodeText4 = FocusNode();
          FocusNode _nodeText5 = FocusNode();
    
         @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: Text("Keyboard Actions Sample"),
              ),
              body: FormKeyboardActions(
                keyboardActionsPlatform: KeyboardActionsPlatform.ALL, //optional
                keyboardBarColor: Colors.grey[200], //optional
                nextFocus: true, //optional
                actions: [
                  KeyboardAction(
                    focusNode: _nodeText1,
                  ),
                  KeyboardAction(
                    focusNode: _nodeText2,
                    closeWidget: IconButton(
                      icon: Icon(Icons.close),
                      onPressed: () {},
                    ),
                  ),
                  KeyboardAction(
                    focusNode: _nodeText3,
                    onTapAction: () {
                      showDialog(
                          context: context,
                          builder: (context) {
                            return AlertDialog(
                              content: Text("Custom Action"),
                              actions: <Widget>[
                                FlatButton(
                                  child: Text("OK"),
                                  onPressed: () => Navigator.of(context).pop(),
                                )
                              ],
                            );
                          });
                    },
                  ),
                  KeyboardAction(
                    focusNode: _nodeText4,
                    displayCloseWidget: false,
                  ),
                  KeyboardAction(
                    focusNode: _nodeText5,
                    closeWidget: Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text("CLOSE"),
                    ),
                  ),
                ],
                child: Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: <Widget>[
                        TextField(
                          keyboardType: TextInputType.number,
                          focusNode: _nodeText1,
                          decoration: InputDecoration(
                            hintText: "Input Number",
                          ),
                        ),
                        TextField(
                          keyboardType: TextInputType.text,
                          focusNode: _nodeText2,
                          decoration: InputDecoration(
                            hintText: "Input Text with Custom Close Widget",
                          ),
                        ),
                        TextField(
                          keyboardType: TextInputType.number,
                          focusNode: _nodeText3,
                          decoration: InputDecoration(
                            hintText: "Input Number with Custom Action",
                          ),
                        ),
                        TextField(
                          keyboardType: TextInputType.text,
                          focusNode: _nodeText4,
                          decoration: InputDecoration(
                            hintText: "Input Text without Close Widget",
                          ),
                        ),
                        TextField(
                          keyboardType: TextInputType.number,
                          focusNode: _nodeText5,
                          decoration: InputDecoration(
                            hintText: "Input Number with Custom Close Widget",
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
          }
    
    0 讨论(0)
  • 2021-02-05 13:12

    Change

    keyboardType: TextInputType.number
    

    to

    keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true)
    
    0 讨论(0)
  • 2021-02-05 13:18

    There is no done button in iOS but we can check the length of the input and clear focus to hide the numberpad keyboard. Implement like below,(it'll work with fix length of number value)

    onChanged: (val) {
            if (val.length == 10) { //10 is the length of the phone number you're allowing
              FocusScope.of(context).requestFocus(FocusNode());
            }
          }
    
    0 讨论(0)
  • 2021-02-05 13:28

    You don't need a done button just wrap MaterialApp with a GestureDetector

    GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
    
        if (!currentFocus.hasPrimaryFocus &&
            currentFocus.focusedChild != null) {
          FocusManager.instance.primaryFocus.unfocus();
        }
      },
      child: MaterialApp(
         title: "My title",
         home:MyHomeScreen(),
         ),
    );
    
    0 讨论(0)
提交回复
热议问题