Skip to content

Instantly share code, notes, and snippets.

@NerdFaisal404
Created June 8, 2021 06:39
Show Gist options
  • Select an option

  • Save NerdFaisal404/6e753daae7369a4e93edd3b06688dfcc to your computer and use it in GitHub Desktop.

Select an option

Save NerdFaisal404/6e753daae7369a4e93edd3b06688dfcc to your computer and use it in GitHub Desktop.
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