首页 > 解决方案 > 如何在 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,
                        )),
                  ),

标签: flutterdartflutter-layoutflutter-dependenciesflutter-web

解决方案


我使用 TextEditingController myController 并将 myController.text 设置为 null


推荐阅读