【发布时间】:2018-09-11 07:45:16
【问题描述】:
TextFormField 无法正常工作,它不断闪烁并且不允许我写任何东西,当我点击 TextFormField 时,我的键盘会出现一秒钟然后立即消失。我很困惑我的代码做错了什么,我已经将我的代码与以前的工作代码相匹配,但仍然出现这种行为。
这是我的代码。
class ComingSoonState extends State<ComingSoon> {
String language;
TextEditingController _textEdititingController ;
@override
void initState() {
_textEdititingController = new TextEditingController(); //Initialised TextEditingController
super.initState();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final Size size = MediaQuery.of(context).size;
final formData = new Form(
key: widget._formKey,
child: new Container(
padding: const EdgeInsets.only(
left: 35.0,
right: 35.0),
child: new Column(
children: <Widget>[
new Theme(
data: theme.copyWith(primaryColor: Colors.black54),
child: new TextFormField(
controller: _textEdititingController, //ADDED CONTROLLER HERE
style: const TextStyle(color: Colors.black54),
decoration: new InputDecoration(
labelText: 'Amount',
labelStyle: const TextStyle(color: Colors.black54)
),
// validator: this._validateEmail,
validator: (val) {
return val.isEmpty
? "Please enter amount"
: null;
},
onSaved: (String value) {
// this._data.email = value;
this.language = value;
}
),
),
],
),
),
);
return Scaffold(
appBar: new AppBar(
leading: null,
title: const Text('Send Money', style: const TextStyle(
color: Colors.white
),
),
),
body: new Container(
color: Colors.grey[300],
child: new ListView(
children: <Widget>[
new Container(
child: new Column(
children: <Widget>[
new Container(
height: 60.0 ,
padding: const EdgeInsets.all(5.0),
child: new Card(
child: new Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: new Text("Available balance in wallet", style:
new TextStyle(color: Colors.black54,
fontSize: 16.0
),),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: new Text("123 KSH", style:
new TextStyle(color: Colors.blueAccent,
fontSize: 16.0
),),
),
],
),
),
) ,
new Container(
//height: 300.0,
padding: const EdgeInsets.all(5.0),
child: new Card(
child: new Container(
child: new Center(
child: new Column(
children: <Widget>[
formData
],
),
),
),
),
)
],
)
),
],
),
),
);
}
}
【问题讨论】:
-
您需要创建一个自定义的 TextEditingController 并将其传递给 TextField。通过将实例保留在构建方法之外,确保不会在每次构建时重新创建控制器。
-
我已经创建了控制器并对其进行了初始化,并将其分配给
TextFormField为controller: _textEdititingController,,但它似乎没有任何区别。 -
你有控制器的例子吗?
-
我明白了,什么例子? ,让我编辑我为控制器添加的代码的问题。