带条件的自动填充 textformfield 控制器
autofill textformfield controller with condition
我正在尝试在我的控制器中创建带条件的 textformfield。如果该字段为空,它将填充我准备的自动填充文本。如何在我的 textFormField 控制器中设置条件?
下面是我的代码。
@override
void initState() {
super.initState();
autofill = new TextEditingController(text: 'auto fill text');
autofill2 = new TextEditingController(text: 'auto fill text2');
}
这是变量。
final _key = new GlobalKey<FormState>();
TextEditingController autofill;
TextEditingController autofill2;
final first_nameController = TextEditingController();
final last_nameController = TextEditingController();
这是检查字段是否为空的验证器,所以会出现错误文本。
check() {
final form = _key.currentState;
if (form.validate()) {
form.save();
field();
}
}
这是我的 API 连接器,用于保存文本字段中的文本数据。
field() async {
String auto= autofill2.text;
String auto2= autofill.text;
String first_name = first_nameController.text;
String last_name = last_nameController.text;
// API URL
final response = await http.post(
config.baseurl + "autofilltextformfield",
body: {
'first_name'.isEmpty ? first_name : auto,
'last_name'.isEmpty ? last_name : auto2,
},
);
if (response.statusCode == 200) {
setState(
() {
visible = false;
},
);
}
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(response.body),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
这就是场。
Form(
key: _key,
child: TextFormField(
validator: (e) {
if (e.isEmpty) {
return "please fill text";
}
},
onSaved: (e) => first_nameController.text = e,
style: TextStyle(
color: Colors.black,
),
controller: first_nameController,
decoration: InputDecoration(
hintText: "text here",
hintStyle: _txtCustomSub,
enabledBorder: new UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 1.0,
style: BorderStyle.none),
),
),
autofocus: false,
),
InkWell(
child: Text(
"Save and Exit",
style: _txtCustomHead,
),
onTap: () {
check();
},
),
),
而不是:
autofill = new TextEditingController(text: 'auto fill text');
autofill2 = new TextEditingController(text: 'auto fill text2');
尝试:
first_nameController.text = 'auto fill text';
last_nameController.text = 'auto fill text2';
我正在尝试在我的控制器中创建带条件的 textformfield。如果该字段为空,它将填充我准备的自动填充文本。如何在我的 textFormField 控制器中设置条件? 下面是我的代码。
@override
void initState() {
super.initState();
autofill = new TextEditingController(text: 'auto fill text');
autofill2 = new TextEditingController(text: 'auto fill text2');
}
这是变量。
final _key = new GlobalKey<FormState>();
TextEditingController autofill;
TextEditingController autofill2;
final first_nameController = TextEditingController();
final last_nameController = TextEditingController();
这是检查字段是否为空的验证器,所以会出现错误文本。
check() {
final form = _key.currentState;
if (form.validate()) {
form.save();
field();
}
}
这是我的 API 连接器,用于保存文本字段中的文本数据。
field() async {
String auto= autofill2.text;
String auto2= autofill.text;
String first_name = first_nameController.text;
String last_name = last_nameController.text;
// API URL
final response = await http.post(
config.baseurl + "autofilltextformfield",
body: {
'first_name'.isEmpty ? first_name : auto,
'last_name'.isEmpty ? last_name : auto2,
},
);
if (response.statusCode == 200) {
setState(
() {
visible = false;
},
);
}
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(response.body),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
这就是场。
Form(
key: _key,
child: TextFormField(
validator: (e) {
if (e.isEmpty) {
return "please fill text";
}
},
onSaved: (e) => first_nameController.text = e,
style: TextStyle(
color: Colors.black,
),
controller: first_nameController,
decoration: InputDecoration(
hintText: "text here",
hintStyle: _txtCustomSub,
enabledBorder: new UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 1.0,
style: BorderStyle.none),
),
),
autofocus: false,
),
InkWell(
child: Text(
"Save and Exit",
style: _txtCustomHead,
),
onTap: () {
check();
},
),
),
而不是:
autofill = new TextEditingController(text: 'auto fill text');
autofill2 = new TextEditingController(text: 'auto fill text2');
尝试:
first_nameController.text = 'auto fill text';
last_nameController.text = 'auto fill text2';