Flutter - Show/Hide Password in TextField
Last Updated :
26 Apr, 2025
In this article, we will Implement how to show/hide the password in the Textfield. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the material package that gives us the important method to use and call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Now we have to make a stateful widget RunMyApp Because our application does change its state and then return the materialApp widget which allows us the set the title and theme and have many more properties.
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
bool passwordVisible=false;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home:
);
}
}
Step 4: Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar has the title property to set the title of the application. Body contains a Container that has TextField as a child. TextField has many parameters like decoration, obscureText. Here is the point obscureText takes the true and false values. If true that means the Password is visible otherwise not. We can use the Suffix icon button to change the state of the obscureText value. When we press the suffix icon button the setState method is called and the variable value of the passwordVisible is changed. decoration parameter generally used to customize the textField. It set the border, hint text, label text, helper text, suffix Icon, prefix Icon, onpressed method.
Scaffold(
appBar: AppBar(title: Text('Show or Hide Password in TextField'),),
body: Container(
padding: EdgeInsets.all(20.0),
child: TextField(
obscureText: passwordVisible,
decoration: InputDecoration(
border: UnderlineInputBorder(),
hintText: "Password",
labelText: "Password",
helperText:"Password must contain special character",
helperStyle:TextStyle(color:Colors.green),
suffixIcon: IconButton(
icon: Icon(passwordVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
passwordVisible = !passwordVisible;
},
);
},
),
alignLabelWithHint: false,
filled: true,
),
keyboardType: TextInputType.visiblePassword,
textInputAction: TextInputAction.done,
),
),
),
Final Code
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
bool passwordVisible=false;
@override
void initState(){
super.initState();
passwordVisible=true;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(title: Text('Show or Hide Password in TextField'),),
body: Container(
padding: EdgeInsets.all(20.0),
child: TextField(
obscureText: passwordVisible,
decoration: InputDecoration(
border: UnderlineInputBorder(),
hintText: "Password",
labelText: "Password",
helperText:"Password must contain special character",
helperStyle:TextStyle(color:Colors.green),
suffixIcon: IconButton(
icon: Icon(passwordVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
passwordVisible = !passwordVisible;
},
);
},
),
alignLabelWithHint: false,
filled: true,
),
keyboardType: TextInputType.visiblePassword,
textInputAction: TextInputAction.done,
),
),
),
);
}
}
Output UI
 Output
Similar Reads
Flutter - Handle TextField Validation in Password
Password validation is a method to validate whether the given password entered by the user meets some specific condition or not according to it if the password satisfies the given condition then the password is valid otherwise the password is invalid. In this article we are going to create a passwor
5 min read
Flutter - Prefix and Suffix Icon in TextField
In this article, we will implement how to add prefix and Suffix icons in the TextField. A sample image is given below to get an idea about what we are going to do in this article. Â Step by Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter Development on Android Stu
2 min read
Multiline TextField in Flutter
Multiline TextField is the input TextField which takes the input in more than one line, This type of TextField is used in the scenario like taking Feedback from the user, User Comments, and Description, etc., We can achieve multiline TextField by setting the property keyBoardType and maxLines of the
2 min read
Flutter - Gradient TextFields
Decorating text fields can be a big task if you have a large application. There is a package gradient_textfield that could make this time-consuming task pretty simple and fast. Although in Flutter we can create text fields using TextField, for beginners especially, it takes time to understand decora
3 min read
Flutter - Save Password in Google Account
When you are using different websites and apps, you can see there is an option available to store the password in Google so that you can use it in the future and donât need to remember it. You can fill many things from autofill in Google for future reference. We can do this from the flutter built-in
5 min read
Custom Label Text in TextFormField in Flutter
Today we will explore how to make a custom label above the text field whose style will change according to your tap on the text field/text form field. So that user can easily understand which text field is currently active. Example: In this, there are two fields, the first is email and the second is
5 min read
Flutter - Generate Strong Random Password
In this article, we are going to make an application in Flutter that generates random passwords that cannot be easily cracked by hackers or attackers. Here we define a method named _generatePassword that is responsible for generating random passwords. It uses a mix of lowercase letters, uppercase le
4 min read
Retrieve Data From TextFields in Flutter
In this article, we'll learn how to retrieve data from TextFields. TextField() widget is the most common widget used in flutter apps to take user input. We'll talk about two major methods used to extract text from TextField.Using VariablesThe TextField widget has various callback properties through
4 min read
How to Hide the Keyboard When User Tap Out of the TextField in Flutter?
Flutter is a UI toolkit developed by Google. It is being utilized by big tech companies like Alibaba, Airbnb, and Google itself to build apps that serve billions of users around the globe. This article will show how to make Rounded Buttons in Flutter. A sample video is given below to get an idea abo
2 min read
Flutter - Hide the Status Bar
StatusBar is located at the top of the Application, which basically shows the notification of the phones. Sample images are given below to get an idea about what we are going to do in this article. Before Hiding the status bar: Â After Hiding the status bar: Â If you see the difference between the i
2 min read