在TextField中输入文本时,显示按钮,否则不在Flutter中显示

When entering text into the TextField, display the button, otherwise do not display in Flutter

需要实现如下功能:在TextField中输入文字时,显示Remove按钮;当 TextField 中没有字符时,不显示 Remove 按钮。我还不明白如何实现这个功能,我需要以某种方式将输入作为字符进行跟踪。如果您能提供帮助,我将不胜感激。

代码

TextField(
              autofocus: true,
              onChanged: onChanged,
              style: constants.Styles.textFieldTextStyleWhite,
              cursorColor: Colors.white,
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.only(
                  top: 10,
                ),
                border: InputBorder.none,
                prefixIcon: Container(
                  width: 10,
                  height: 10,
                  alignment: Alignment.center,
                  child: SvgPicture.asset(
                    constants.Assets.search,
                  ),
                ),
              ),
            ),

您可以使用 onChanged 函数来跟踪在 TextField 中写入的内容或使用 TextEditingController(),并使用 TextField 的控制器字段。 然后,您可以使用 controller.text.isEmpty / controller.text.isNotEmpty.

查找 TextField 是否为空