我只想接受 TextFormField
中的数字。我设计了一个 TextFormField
来接受电话号码。我已经更改了键盘类型。
keyboardType: TextInputType.phone,
但是,它也可以接受特殊符号或者也可以粘贴其他输入。 Keyboard screenshots
最佳答案
试试下面的代码,引用FilteringTextInputFormatter
导入下面的库
import 'package:flutter/services.dart';
您的小部件:
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
//you can used below formater also
/* inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[0-9]"),
),
],*/
),
关于flutter - 如何在 TextFormFields 中只接受数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72433582/