Created
June 8, 2021 06:39
-
-
Save NerdFaisal404/6e753daae7369a4e93edd3b06688dfcc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class ShowHidePasswordView extends StatefulWidget { | |
| @override | |
| _ShowHidePasswordViewState createState() => _ShowHidePasswordViewState(); | |
| } | |
| class _ShowHidePasswordViewState extends State<ShowHidePasswordView> { | |
| bool _passwordVisible; | |
| TextEditingController _userPasswordController= TextEditingController(); | |
| @override | |
| void initState() { | |
| _passwordVisible = false; | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Center( | |
| child: TextFormField( | |
| keyboardType: TextInputType.text, | |
| controller: _userPasswordController, | |
| obscureText: !_passwordVisible,//This will obscure text dynamically | |
| decoration: InputDecoration( | |
| labelText: 'Password', | |
| hintText: 'Enter your password', | |
| // Here is key idea | |
| suffixIcon: IconButton( | |
| icon: Icon( | |
| // Based on passwordVisible state choose the icon | |
| _passwordVisible | |
| ? Icons.visibility | |
| : Icons.visibility_off, | |
| color: Theme.of(context).primaryColorDark, | |
| ), | |
| onPressed: () { | |
| // Update the state i.e. toogle the state of passwordVisible variable | |
| setState(() { | |
| _passwordVisible = !_passwordVisible; | |
| }); | |
| }, | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment