Flutter: Show/Hide Password in TextField/TextFormField

Last updated on August 30, 2021 A Goodman Loading... 3 comments

To hide an entered password in a TextField/TextFormField, just set its obscureText property to true:

TextField(
   obscureText: true,
   /* ... */
),

Screenshot:

To show the entered password for the user to read it, set obscureText to false:

TextField(
   obscureText: false,
   /* ... */
),

Screenshot:

The Complete Example

App Preview

We’ll make a simple Flutter app that contains a TextField widget (you can use TextFormField as well) at the center of the screen. This text field lets the user type a password in and has an eye-icon button to show/hide the entered password. Here’s how it works:

The Full Code

// main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _isObscure = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(25),
        child: Center(
          child: TextField(
            obscureText: _isObscure,
            decoration: InputDecoration(
                labelText: 'Password',
                suffixIcon: IconButton(
                    icon: Icon(
                        _isObscure ? Icons.visibility : Icons.visibility_off),
                    onPressed: () {
                      setState(() {
                        _isObscure = !_isObscure;
                      });
                    })),
          ),
        ),
      ),
    );
  }
}

That’s it.

Conclusion

You’ve learned how to programmatically show/hide the password characters in a TextField/TextFormField. If you’d like to explore more beautiful widgets and powerful features of Flutter, take a look at the following articles:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.

Subscribe
Notify of
guest
3 Comments
Inline Feedbacks
View all comments
Vinicius Mendonça Martins
Vinicius Mendonça Martins
6 months ago

it’s helped me a lot!! Thanks!!!

Frank
Frank
10 months ago

Nice example

Related Articles