Flutter 颤振文本字段允许的文本格式
我有两个文本字段,其中一个应该是年份,所以我只允许输入1900到2020之间的数字,第二个用于评级,最多应该是9.9,可以是5或5.5等等, 当我使用下面的代码,我可以得到1800或3450年字段和评级方可以是123或任何有3位数字。有没有办法像我想的那样限制他们 多年来Flutter 颤振文本字段允许的文本格式,flutter,textfield,Flutter,Textfield,我有两个文本字段,其中一个应该是年份,所以我只允许输入1900到2020之间的数字,第二个用于评级,最多应该是9.9,可以是5或5.5等等, 当我使用下面的代码,我可以得到1800或3450年字段和评级方可以是123或任何有3位数字。有没有办法像我想的那样限制他们 多年来 new TextField( controller: _disControllerF, textAlign: TextAl
new TextField(
controller: _disControllerF,
textAlign: TextAlign.center,
style: new TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0
),
keyboardType: TextInputType.number,
inputFormatters:<TextInputFormatter>[
LengthLimitingTextInputFormatter(4),
WhitelistingTextInputFormatter.digitsOnly,
],
decoration: InputDecoration(
border: new OutlineInputBorder(
),
hintText: '1900',
hintStyle: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
contentPadding: const EdgeInsets
.symmetric(
horizontal: 10.0),
),),
新建文本字段(
控制器:,
textAlign:textAlign.center,
样式:新文本样式(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0
),
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(4),
WhiteListingDeputFormatter.digitsOnly,
],
装饰:输入装饰(
边框:新大纲输入边框(
),
hintText:'1900',
hintStyle:TextStyle(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
contentPadding:const EdgeInsets
.对称(
水平线:10.0),
),),
评级
new TextField(
controller: _disControllerR,
textAlign: TextAlign.center,
style: new TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0
),
keyboardType: TextInputType.number,
inputFormatters:<TextInputFormatter>[
LengthLimitingTextInputFormatter(3),
WhitelistingTextInputFormatter(RegExp("[1-9.]")),
],
decoration: InputDecoration(
border: new OutlineInputBorder(
),
hintText: '6.5',
hintStyle: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
contentPadding: const EdgeInsets
.symmetric(
horizontal: 10.0),
),),
新建文本字段(
控制器:\u ROLLERR,
textAlign:textAlign.center,
样式:新文本样式(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0
),
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(3),
WhiteListingPutFormatter(RegExp(“[1-9.]”),
],
装饰:输入装饰(
边框:新大纲输入边框(
),
hintText:'6.5',
hintStyle:TextStyle(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
contentPadding:const EdgeInsets
.对称(
水平线:10.0),
),),
使用表单小部件和文本表单字段,以便您可以验证程序在文本表单字段中添加您的条件。使用表单中的关键参数,可以通过单击按钮进行验证
var formkey = GlobalKey<FormState>();
Form(
key: formkey,
child: Column(children: [
TextFormField(
//controller: _disControllerF,
textAlign: TextAlign.center,
style: new TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(4),
WhitelistingTextInputFormatter.digitsOnly,
],
validator: (value) {
String errorString;
if (int.parse(value) < 2200 && int.parse(value) > 1800) {
} else {
errorString = "Enter value between 1800 and 220";
}
return errorString;
},
decoration: InputDecoration(
border: new OutlineInputBorder(),
hintText: '1900',
hintStyle: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
contentPadding: const EdgeInsets.symmetric(horizontal: 10.0),
),
),
TextFormField(
//controller: _disControllerR,
textAlign: TextAlign.center,
style: new TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(3),
WhitelistingTextInputFormatter(RegExp("[1-9.]")),
],
validator: (value) {
String errorString;
if (double.parse(value) > 9.9 || double.parse(value) < 5) {
errorString = "Enter inbetween 5 to 9.9";
}
return errorString;
},
decoration: InputDecoration(
border: new OutlineInputBorder(),
hintText: '6.5',
hintStyle: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "SegoeUI",
fontStyle: FontStyle.normal,
fontSize: 16.0),
contentPadding: const EdgeInsets.symmetric(horizontal: 10.0),
),
),
RaisedButton(onPressed: () {
if (formkey.currentState.validate()) {}
})
]))
var formkey=GlobalKey();
形式(
key:formkey,
子项:列(子项:[
TextFormField(
//控制器:,
textAlign:textAlign.center,
样式:新文本样式(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(4),
WhiteListingDeputFormatter.digitsOnly,
],
验证器:(值){
字符串错误字符串;
如果(int.parse(值)<2200&&int.parse(值)>1800){
}否则{
errorString=“输入介于1800和220之间的值”;
}
返回错误字符串;
},
装饰:输入装饰(
边框:新大纲输入边框(),
hintText:'1900',
hintStyle:TextStyle(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
contentPadding:const EdgeInsets.symmetric(水平:10.0),
),
),
TextFormField(
//控制器:\u ROLLERR,
textAlign:textAlign.center,
样式:新文本样式(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
键盘类型:TextInputType.number,
输入格式化程序:[
长度限制文本输入格式化程序(3),
WhiteListingPutFormatter(RegExp(“[1-9.]”),
],
验证器:(值){
字符串错误字符串;
if(double.parse(value)>9.9 | | double.parse(value)<5){
errorString=“在5到9.9之间输入”;
}
返回错误字符串;
},
装饰:输入装饰(
边框:新大纲输入边框(),
hintText:'6.5',
hintStyle:TextStyle(
fontWeight:fontWeight.w400,
fontFamily:“SegoeUI”,
fontStyle:fontStyle.normal,
字体大小:16.0),
contentPadding:const EdgeInsets.symmetric(水平:10.0),
),
),
升起按钮(按下时:(){
if(formkey.currentState.validate()){}
})
]))
谢谢。我以前想使用validator,但我不能确定如何使用validator中的每个数据,因为我使用这些数据来重新创建api