How to show/hide password in TextFormField?

前端 未结 9 1343
故里飘歌
故里飘歌 2021-02-01 00:54

Currently I have my password TextFormField like this:

TextFormField(
  decoration: const InputDecoration(
         


        
9条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 01:20

    class SignIn extends StatefulWidget {
    
      @override
      _SignInState createState() => _SignInState();
    }
    
    class _SignInState extends State {
    
            // Initially password is obscure
      bool _obscureText = true;
        // Toggles the password show status
      void _togglePasswordStatus() {
        setState(() {
          _obscureText = !_obscureText;
        });
      }
      @override
      Widget build(BuildContext context) {
        return 
        Scaffold(
          backgroundColor: Colors.brown[100],
          appBar: AppBar(
            backgroundColor: Colors.brown[400],
            elevation: 0.0,
            title: Text('Sign In'),
          ),
          body: Container(
            padding: EdgeInsets.symmetric(vertical:20.0,horizontal:50.0),
            child: Form(
              key: _formKey,
              child: Column(children: [
                TextFormField(
                  decoration: InputDecoration(
                    hintText: 'Password',
                    suffixIcon:  IconButton(
                      icon:Icon(_obscureText ? Icons.visibility:Icons.visibility_off,),
                       onPressed: _togglePasswordStatus,
                       color: Colors.pink[400],
                       ),
                  ),
                  validator: (val){
                    return
                    val.length < 6 ? 'Enter A Password Longer Than 6 Charchters' :null;
                  },
                  obscureText: _obscureText,
                  onChanged: (val){
                    setState(() {
                      password = val.trim();
                    });
                  },
                ),
            ],),),
          ),
        );
      }
    }
    

提交回复
热议问题