TextFormField是继承了TextFiled,
TextFormField一般是放在Form里面使用,可以使用GlobalKey进行控制:
首先在类里面声明GlobalKey,如:
final globalKeyValue=GlobalKey<FormState>();
其次在Form里面加入 key:GlobalKey,如:
key: globalKeyValue,
再次在TextFormFiled里面定义onSave(){}和validate(){}
onSaved: (value){username=value;},
validator: validateUsername,
String validateUsername(value){
if(value.isEmpty){return 'Username is required';}else{return null;}
}
最后在使用下面这两句就直接调用了TextFormField内部定义的onSave(){}和validate(){}:
globalKeyValue.currentState.save();
globalKeyValue.currentState.validate();