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

Time:05-25

It is necessary to implement the following function: when entering text into the TextField, show the Remove button; when there are no characters in the TextField, do not show the Remove button. I do not yet understand how to implement this function, I need to track the input as a character somehow. I will be grateful if you help.

code

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,
                  ),
                ),
              ),
            ),

CodePudding user response:

You could use your onChanged function to keep track of what is being written in the TextField or use a TextEditingController(), and use the controller field of the TextField. Then you can find if the TextField is or isn't empty by using controller.text.isEmpty / controller.text.isNotEmpty.

https://stackoverflow.com/a/57747739/19178917

  • Related