How to show/hide password in TextFormField?

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

Currently I have my password TextFormField like this:

TextFormField(
  decoration: const InputDecoration(
         


        
9条回答
  •  不知归路
    2021-02-01 01:07

    I did it with holding and releasing the longTap:

        bool _passwordVisible;
    
    @override
    void initState() {
        _passwordVisible = false;
        super.initState();
    }
    
    // ...
    TextFormField(
      obscureText: !_passwordVisible,
      decoration: InputDecoration(
        hasFloatingPlaceholder: true,
        filled: true,
        fillColor: Colors.white.withOpacity(0.5),
        labelText: "Password",
        suffixIcon: GestureDetector(
          onLongPress: () {
            setState(() {
              _passwordVisible = true;
            });
          },
          onLongPressUp: () {
            setState(() {
              _passwordVisible = false;
            });
          },
          child: Icon(
              _passwordVisible ? Icons.visibility : Icons.visibility_off),
        ),
      ),
      validator: (String value) {
        if (value.isEmpty) {
          return "*Password needed";
        }
      },
      onSaved: (String value) {
        _setPassword(value);
      },
    );
    

提交回复
热议问题