Flutter 输入框限制输入数字和小数点
可以使用 TextField 组件的 inputFormatters 属性来限制输入内容,例如:/n/ndart/nTextField(/n keyboardType: TextInputType.numberWithOptions(decimal: true),/n inputFormatters: <TextInputFormatter>[/n FilteringTextInputFormatter.allow(RegExp(r'^/d+/.?/d{0,2}')),/n ],/n)/n/n/n其中,keyboardType 属性设置为 numberWithOptions,表示只允许输入数字和小数点;inputFormatters 属性设置为 FilteringTextInputFormatter.allow,表示只允许输入符合正则表达式的内容,这里的正则表达式限制了输入数字和小数点,小数点后最多只能有两位小数。
原文地址: https://www.cveoy.top/t/topic/mLkA 著作权归作者所有。请勿转载和采集!