Flutter 键盘类型:文本输入类型,数字不适用于文本编辑控制器

bqjvbblv  于 2023-02-13  发布在  Flutter
关注(0)|答案(3)|浏览(256)

在Flutter TextFeild中使用keyboardType:TextInputType.number沿着TextEditingController类型的控制器时,无法直接使用数字键盘输入数字。首先我必须按任何特殊符号,如“。”,“-”,然后键入数字。

final expenseAmount = TextEditingController();
TextField(
          decoration: InputDecoration(
            labelText: "Amount",
          ),
          controller: expenseAmount,
          keyboardType: TextInputType.number,
        ),

Screen Shots 1st unable to type number 2nd after . able to type

dced5bon

dced5bon1#

我在使用TextInputType.phone时也遇到过类似的问题,执行了这几行代码controller.text = ''后,数字键盘变得不可用。controller.clean这帮助我解决了这个问题。

0qx6xfy6

0qx6xfy62#

你应该试试这个

final TextEditingController price = new TextEditingController();

TextFormField(
   controller: price,
   keyboardType: TextInputType.number,
   decoration: InputDecoration(
      border: OutlineInputBorder(),
      labelText: 'Amount',
      hintText: 'MRP',
     ),
     textInputAction: TextInputAction.done,
   ),

其他解决方案:添加程序包:import 'package:flutter/services.dart';

TextFormField(
                keyboardType: TextInputType.number,
                inputFormatters: [
                  FilteringTextInputFormatter.allow(
                    RegExp(r'[0-9]'),
                  ),
                ],
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Price',
                  hintText: '0.00',
                ),
              ),

参考FilteringTextInputFormatterhere

tpgth1q7

tpgth1q73#

更改此行

final expenseAmount = TextEditingController();

TextEditingController expenseAmount = TextEditingController();

我想这会解决你问题

相关问题