How to show/hide password in TextFormField?

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

Currently I have my password TextFormField like this:

TextFormField(
  decoration: const InputDecoration(
         


        
9条回答
  •  再見小時候
    2021-02-01 00:57

      bool _obscuredText = true; 
    
      _toggle(){
        setState(() {
          _obscuredText = !_obscuredText;
        });
      }
    
      Widget _createPassword(){
        return TextField(
          obscureText: _obscuredText,
          cursorColor: Colors.black54,
          style: TextStyle( color: Colors.black54),
          decoration: InputDecoration(
            labelStyle: TextStyle(
                color: Colors.black54
            ),
            focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Colors.black54
                )
            ),
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(5.0)
            ),
            labelText: 'Contraseña',
            hintText: 'Contraseña',
            suffixIcon: FlatButton(onPressed: _toggle, child:Icon(Icons.remove_red_eye, color: _obscuredText ? Colors.black12 : Colors.black54))
          ),
          onChanged: (value) {
            setState(() {
              _password = value;
            });
          },
        );
      }
    

    Hope this helps!

提交回复
热议问题