Sarmed MQ Berwari
Sarmed MQ Berwari

Reputation: 89

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.

Upvotes: 0

Views: 90

Answers (3)

Sadhik
Sadhik

Reputation: 300

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

Upvotes: 0

Harish Sharma
Harish Sharma

Reputation: 1549

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();
}

Upvotes: 0

Jahidul Islam
Jahidul Islam

Reputation: 12565

Declare this two-line above build context

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

Upvotes: 1

Related Questions