Search code examples
flutterpasswordstextfieldprovider

Flutter : suffixIcon Icon clear the textfild on pressed


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.


Solution

  • Declare this two-line above build context

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