Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fully hide password input in Flutter?

I'm working on a login form using Flutter web. I want my second TextField (the one for my password) to hide the user inputs. I've already set the obscureText property to true.

enter image description here

I dont want the "f" to be displayed. Is it possible to do that ?

like image 359
loic.k Avatar asked Oct 16 '22 12:10

loic.k


1 Answers

There is no easy way to do that; So would have to use controller and StatefulWidget that converts inputed value to "*"

class Testas extends StatefulWidget{
  @override
  _TestasState createState() => _TestasState();
}

class _TestasState extends State<Testas> {
  String _valueToShow = "";
  String _value = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: TextField(
          controller: TextEditingController.fromValue(TextEditingValue(text: _valueToShow, selection: new TextSelection.collapsed(offset: _valueToShow.length))),
          onChanged: (String val){
            String value = "";
            if(val.length > _value.length){
                value+=val.substring(_value.length, val.length);
              }
              if(val.length < _value.length){
                value = _value.substring(1, val.length);
              }
            String valueToShow = "*" * val.length;
            setState(() {
                _valueToShow = valueToShow;
                _value = value;

            });
          },
        ),
      ),
    );
  }
}
like image 72
Nuts Avatar answered Dec 02 '22 08:12

Nuts