Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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(
          key: Key(keyValue),
          initialValue: valueBuilder,
          onSaved: (text) {
            fieldsController.text = text.trim();
          },
          inputFormatters: [inputFormatters],
          keyboardType: TextInputType.phoneNumber,)

I want create a keyboard like the below. For the input text form field.

this

like image 787
praveen r Avatar asked Nov 27 '18 17:11

praveen r


People also ask

How do I add a Done button on my keyboard in Flutter?

You only need signed: true to trigger this workaround. @GiulioPretis Characters are shown but not clickable...so, it allows only Numbers & that is what required in this case. you can set decimal to false here if you want to use All characters from Standard Keyboard.

How do you give a numeric keyboard in Flutter?

Steps to show numeric input keyboard in FlutterStep 1: Add TextField widget to your dart file. Step 2: Add keyboardType parameter and assign the TextInputType. number. Step 3: Add inputFormatters parameter and assign the [FilteringTextInputFormatter.

How do I get rid of the Done button on my keyboard on Flutter?

You can simply use textInputAction: TextInputAction. none on the TextField.

How do I enter only numbers in Flutter?

The TextField widget is required to set keyboardType: TextInputType. number, and inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter. digitsOnly] to accept numbers only as input.

How to show decimal number soft keyboard in flutter?

To show the decimal number soft keyboard in Flutter, just add the following option to your TextField widgets: Note: If you use keyboardType: TextInputType.number, the soft keyboard on iOS will not have the dot (.) symbol That’s it. You can continue exploring more about TextField and other stuff in Flutter by taking a look at the following articles:

Is there a keyboard plugin for flutter?

@bartwaggoner Flutter currently relies on the system keyboard for text entry, and we don't currently support anything like a keyboard plugin written in Flutter. It would be a fairly massive undertaking since we'd have to integrate with all the underlying platform APIs for all OSes we support.

How to add a text input to a controller?

One option would be to use a TextInputType other than number (i.e. the default keyboard) and add validation logic via the onChanged handler and manipulating the controller you pass in. Another possibility would be to implement an input accessory view in Objective-C.

How to submit a number entered in the text field?

Create a text field as follows: (The text field is in a dialog which is why the onSubmitted does a Navigator.pop) When keyboard appears it has 0-9, '.' an delete but no Done button. There is no way to 'submit' the number entered into the text field.


6 Answers

Change

keyboardType: TextInputType.number

to

keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true)
like image 178
Bob Lyons Avatar answered Oct 20 '22 08:10

Bob Lyons


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

enter image description here

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",
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
like image 35
diegoveloper Avatar answered Oct 20 '22 07:10

diegoveloper


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(),
     ),
);
like image 16
Toheeb Avatar answered Oct 20 '22 06:10

Toheeb


add this to your TextField

TextField(
...
keyboardType: TextInputType.numberWithOptions(signed: true),
inputFormatters: [
    FilteringTextInputFormatter.digitsOnly,
],
...
),
like image 11
Dũng Phạm Tiến Avatar answered Oct 20 '22 08:10

Dũng Phạm Tiến


This is how to add Done button to your TextFormField when keyboard is opened:

textInputAction: TextInputAction.done,

Please keep in mind that iOS doesn’t support Done on NUMERIC keyboards.

like image 8
Osama Remlawi Avatar answered Oct 20 '22 08:10

Osama Remlawi


I used this way to handle done button in IOS

      keyboardType: Platform.isIOS? 
                    TextInputType.numberWithOptions(signed: true, decimal: true)
                  : TextInputType.number,
// This regex for only amount (price). you can create your own regex based on your requirement
     inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,4}'))],
like image 3
BIS Tech Avatar answered Oct 20 '22 07:10

BIS Tech