How to show/hide in this particular code in flutter

Issue

I created this code but I don’t know how to show/hide the password could anyone help me

  Container(
          margin: EdgeInsets.all(10),
          child: TextField(
            controller: nameController3,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'Password',
            ),
            onChanged: (Text) {
              setState(() {});
            },
          )),

Solution

You mean like this?


class MyWidgetState extends State<MyWidget> {
  
  TextEditingController nameController3 = TextEditingController();
  bool showPassword = false;
  
  @override
  Widget build(BuildContext context) {
    return Container(
          margin: EdgeInsets.all(10),
      child: Column(
        children: [
          TextField(
        obscureText: showPassword,
        obscuringCharacter: "*",
        controller: nameController3,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'Password',
        ),
        onChanged: (Text) {
          setState(() {
          });
        },
      ),
      TextButton(
         onPressed: () {
           setState(() {
                  showPassword = !showPassword;
           });
         },
         child: Text('Show / Hide Password')
         )
        ]
      )
    );
  }
}

enter image description here

Answered By – Roman Jaquez

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *