如何清除Flutter中的输入文本
How to clear in put text in Flutter
当用户点击 X 图标时,我试图清除文本,当我点击它时我的搜索结果为空,但我的输入文本仍然存在,所以如果我能得到任何帮助,我将不胜感激或者关于如何清除输入文本的建议。
Expanded(
flex: 8,
child: GestureDetector(
onTap: () {},
child: TextFormField(
autofocus: true,
enabled: true,
onChanged: (va) {
filterSearchResult(va, model);
},
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
border: InputBorder.none,
hintStyle: TextStyle(
color:
Theme.of(context).secondaryHeaderColor)),
),
)),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
items.clear();
itemList.clear();
});
},
child: Image.asset(
"assets/icons/cancel.png",
color: Theme.of(context).secondaryHeaderColor,
height: 30,
)),
),
我使用 TextEditingController myController 并将 myController.text 设置为 null
您应该使用TextEditingController
来清除TextFormField
的值。
// Some code ...
final textEditingController = TextEditingController();
// Some code ...
Expanded(
flex: 8,
child: GestureDetector(
onTap: () {},
child: TextFormField(
autofocus: true,
enabled: true,
controller: textEditingController, // Your TextEditingController here
onChanged: (va) {
filterSearchResult(va, model);
},
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
border: InputBorder.none,
hintStyle:
TextStyle(color: Theme.of(context).secondaryHeaderColor),
),
),
),
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
textEditingController.clear(); // Clear the TextFormField
items.clear();
itemList.clear();
});
},
child: Image.asset(
"assets/icons/cancel.png",
color: Theme.of(context).secondaryHeaderColor,
height: 30,
),
),
),
当用户点击 X 图标时,我试图清除文本,当我点击它时我的搜索结果为空,但我的输入文本仍然存在,所以如果我能得到任何帮助,我将不胜感激或者关于如何清除输入文本的建议。
Expanded(
flex: 8,
child: GestureDetector(
onTap: () {},
child: TextFormField(
autofocus: true,
enabled: true,
onChanged: (va) {
filterSearchResult(va, model);
},
decoration: InputDecoration(
floatingLabelBehavior:
FloatingLabelBehavior.never,
border: InputBorder.none,
hintStyle: TextStyle(
color:
Theme.of(context).secondaryHeaderColor)),
),
)),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
items.clear();
itemList.clear();
});
},
child: Image.asset(
"assets/icons/cancel.png",
color: Theme.of(context).secondaryHeaderColor,
height: 30,
)),
),
我使用 TextEditingController myController 并将 myController.text 设置为 null
您应该使用TextEditingController
来清除TextFormField
的值。
// Some code ...
final textEditingController = TextEditingController();
// Some code ...
Expanded(
flex: 8,
child: GestureDetector(
onTap: () {},
child: TextFormField(
autofocus: true,
enabled: true,
controller: textEditingController, // Your TextEditingController here
onChanged: (va) {
filterSearchResult(va, model);
},
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
border: InputBorder.none,
hintStyle:
TextStyle(color: Theme.of(context).secondaryHeaderColor),
),
),
),
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
textEditingController.clear(); // Clear the TextFormField
items.clear();
itemList.clear();
});
},
child: Image.asset(
"assets/icons/cancel.png",
color: Theme.of(context).secondaryHeaderColor,
height: 30,
),
),
),