Home > Enterprise >  Flutter : suffixIcon Icon clear the textfild on pressed
Flutter : suffixIcon Icon clear the textfild on pressed

Time:06-22

I have Flutter app contain login screen, I have two textfield (email and password). the code something like this :

Widget build(BuildContext context) {
    TextEditingController _email = TextEditingController();
    TextEditingController _pass = TextEditingController();
    return Column(
        chilfren:[
         emailText(_email)
         passwordText(_pass)
       ]
    );}
    //===Widgets Method
    TextFiled emailText(controller){
        return TextFiled(
         controller : _email,
         );}
    Consumer passwordText(controller){
      return Consumer<AuthProvider>(builder:(context,auth,child){
        return TextFiled( 
         obscureText: auth.isLoginPassowrdHidden,
         controller : _pass,
         suffixIcon: IconButton(
                            onPressed: () {
                              auth.showLoginFormPassword();
                            },
                            icon: Icon(
                              auth.isLoginPassowrdHidden
                                  ? Icons.visibility_outlined
                                  : Icons.visibility_off_outlined,
                              color: const Color(0xFFBDBDBD),
                              size: 18,
                            )),
         );
     });

when I press the suffixIcon icon the obscureText of password text filed show and hide correctly, but the textfiled of email be clear.

CodePudding user response:

Declare this two-line above build context

TextEditingController _email = TextEditingController();
TextEditingController _pass = TextEditingController();
Widget build(BuildContext context) {
}

CodePudding user response:

Declare controller globally and initialize in initState method like below-

late TextEditingController _email;
late TextEditingController _pass;

 @override
 void initState(){
  _email = TextEditingController();
   _pass = TextEditingController();
  super.initState();
}

CodePudding user response:

you are already getting controller in both function so pass the controller to textfield

TextFiled emailText(controller){
        return TextFiled(
         controller : controller,
         );}

same as in password widget

  • Related