import
'package:flutter/material.dart'
;
void
main() {
runApp(MyApp());
}
class
MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner:
false
,
home: PasswordTextField(),
);
}
}
class
PasswordTextField extends StatefulWidget {
@override
_PasswordTextFieldState createState() => _PasswordTextFieldState();
}
class
_PasswordTextFieldState extends State<PasswordTextField> {
String password =
''
;
bool
showPassword =
false
;
void
toggleShowPassword() {
setState(() {
showPassword = !showPassword;
});
}
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(
'Password TextField Example'
),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
obscureText: !showPassword,
decoration: InputDecoration(
labelText:
'Password'
,
hintText:
'Enter your password'
,
),
onChanged: (value) {
setState(() {
password = value;
});
},
),
SizedBox(height: 10.0),
ElevatedButton(
onPressed: toggleShowPassword,
child: Text(showPassword ?
'Hide Password'
:
'Show Password'
),
),
SizedBox(height: 10.0),
Text(
'Password: ${showPassword ? password : '
******
'}'
,
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
),
),
);
}
}