Mee*_*Mee 6 button textfield txtextcontrol flutter
我是 flutter 新手,只要某些文本字段为空,我就会尝试禁用一个按钮,所以我制作了 3 个文本控制器来充当 3 个文本字段的控制器,并且我制作了一个函数来检查该函数是否为:
bool isEmpty(){
setState(() {
if(textEditingController.text!=" "&&textEditingController2.text!=" "&& textEditingController3.text!=" "){
isButtonEnabled=true;
}
});
return isButtonEnabled;
}
Run Code Online (Sandbox Code Playgroud)
文本字段的代码是:
TextField(
onSubmitted:null,
controller: textEditingController3,
)
Run Code Online (Sandbox Code Playgroud)
然后我为按钮编写代码如下:Center(
child: RaisedButton(
child: Text("Button 1",style: TextStyle(color:Colors.white),),
onPressed:isButtonEnabled?(){ print("enabled");}:null,
color: Colors.red,
),
Run Code Online (Sandbox Code Playgroud)
问题是即使我在文本字段中写入后,该按钮仍然处于禁用状态。任何想法?提前致谢。 编辑: 感谢@diegovoper的回答,这有效,但是如果我想输入初始值并且我希望仅当文本字段具有如下文本字段值时才启用按钮怎么办:
@override
void initState() {
super.initState();
textEditingController = new TextEditingController(text: name);
textEditingController2 = new TextEditingController(text: email);
textEditingController3 = new TextEditingController(text: " place");
}
Run Code Online (Sandbox Code Playgroud)
然后我将 isEmpty 方法更新为:
bool isEmpty(){
setState(() {
if((textEditingController.text!=" ")&&(textEditingController2.text!=" ")&& (textEditingController3.text!=" ")&&(textEditingController!=null)&&(textEditingController2!=null)&&(textEditingController3!=null)){
isButtonEnabled=true;
}
else{
isButtonEnabled=false;
}
});
return isButtonEnabled;
}
Run Code Online (Sandbox Code Playgroud)
问题是,尽管我给文本字段提供了缩写值,按钮仍然被禁用,而且当我编辑文本字段的 3 个值时,按钮被启用,但如果我删除了文本(我认为它的意思是 null)按钮仍然处于禁用状态。
Kyr*_*iev 19
您不需要任何额外的库来做到这一点。Flutter 具有开箱即用的功能,您可以确保每次击键时不会重建整个树。
TextEditingController扩展ValueNotifier<TextEditingValue>意味着您可以利用ValueListenableBuilder包material来监听文本更改。
class MyWidget extends StatelessWidget {
final TextEditingController _inputController = TextEditingController();
@override
void dispose() {
_inputController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(children: [
TextField(controller: _inputController),
ValueListenableBuilder<TextEditingValue>(
valueListenable: _inputController,
builder: (context, value, child) {
return ElevatedButton(
onPressed: value.text.isNotEmpty ? () {} : null,
child: Text('I am disabled when text is empty'),
);
},
),
]);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在这里阅读有关 TextField 的更多信息:https://flutter.io/cookbook/forms/text-field-changes/
所以你有两个选择:
1 - 监听每个变化TextEditingController并调用您的方法isEmpty()
2 -onChanged在每个TextField您想要监听更改的回调上添加回调。
选项2
TextField(
onSubmitted: null,
onChanged: (val) {
isEmpty();
},
controller: textEditingController3,
)
Run Code Online (Sandbox Code Playgroud)
注意:不要忘记将 ELSE 条件添加到 isEmpty 方法中。
编辑
修改您的 initState 方法以检查您的按钮是否已启用(请重构此代码)
@override
void initState() {
super.initState();
textEditingController = TextEditingController(text: "name");
textEditingController2 = TextEditingController(text: "email");
textEditingController3 = TextEditingController(text: "place");
if ((textEditingController.text.trim() != "") && (textEditingController2.text.trim() != "") && (textEditingController3.text.trim() != "")) {
isButtonEnabled = true;
} else {
isButtonEnabled = false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21038 次 |
| 最近记录: |