How to Show/Hide Password in Textformfield

How to show/hide password in TextFormField?

First make you widget StatefulWidget if it is a StatelessWidget.

Then have a variable bool _obscureText and pass it to your TextFormField. The toggle it with setState as required.

Example:

class _FormFieldSampleState extends State<FormFieldSample> {

// Initially password is obscure
bool _obscureText = true;

String _password;

// Toggles the password show status
void _toggle() {
setState(() {
_obscureText = !_obscureText;
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Sample"),
),
body: new Container(
child: new Column(
children: <Widget>[
new TextFormField(
decoration: const InputDecoration(
labelText: 'Password',
icon: const Padding(
padding: const EdgeInsets.only(top: 15.0),
child: const Icon(Icons.lock))),
validator: (val) => val.length < 6 ? 'Password too short.' : null,
onSaved: (val) => _password = val,
obscureText: _obscureText,
),
new FlatButton(
onPressed: _toggle,
child: new Text(_obscureText ? "Show" : "Hide"))
],
),
),
);
}
}

Show/Hide Passwords in Flutter's TextFormField

Thanks @ShyjuM and @ diegoveloper! I see what I was doing wrong - I was calling the buildTextFormField in the constructor of my State class and not in the build method. Moving the call to buildTextFormField inside the build method fixed it. Thanks again for all of your help!

How to visible/hide password in flutter?

Actually, the suffix icon is visible but when i click on TextFormField then the icon color is changing to white so simply in icon field i added a color property and give a black color to icon so even textfield is in focus its color remain black.

Password show/hide toggle deletes password TextField value (Flutter)

When you use the setState to toggle the visibility, the build method will get called to redraw that widget. Since you are initialising your TextEditingController in the build method, it get initialised again and loose the previous value. To fix this you just remove the initialisation from build method to class level.

class _LoginScreenState extends State<LoginScreen> {
bool _showPassword = true;
final usernameController = TextEditingController();
final passwordController = TextEditingController();

@override
Widget build(BuildContext context) {

return SafeArea(
//... Your code
);
}
//... Your code
}

How to hide password ENTIRELY in flutter

You need it on Android and iOs, right? Because on other platforms seems to be implemented by default like you want it.
In any case, try this:

class ObscuringTextEditingController extends TextEditingController {
ObscuringTextEditingController(String text) : super(text: text);

@override
TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) {
var displayValue = '•' * value.text.length;
if (!value.composing.isValid || !withComposing) {
return TextSpan(style: style, text: displayValue);
}
final TextStyle composingStyle = style?.merge(
const TextStyle(decoration: TextDecoration.underline),
) ??
const TextStyle(decoration: TextDecoration.underline);
return TextSpan(
style: style,
children: <TextSpan>[
TextSpan(text: value.composing.textBefore(displayValue)),
TextSpan(
style: composingStyle,
text: value.composing.textInside(displayValue),
),
TextSpan(text: value.composing.textAfter(displayValue)),
],
);
}
}

Updated the original code from here

How to add show/add password icon to a TextField widget with an obscure text property in flutter

import 'package:flutter/material.dart';

class MainClass extends StatefulWidget {
const MainClass({Key key}) : super(key: key);

@override
_MainClassState createState() => _MainClassState();
}

class _MainClassState extends State<MainClass> {
bool _isObscure = false;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Sample"),
),
body: new Container(
child: new Column(
children: <Widget>[
TextField(
obscureText: _isObscure,
decoration: InputDecoration(
labelText: 'Password',
// this button is used to toggle the password visibility
suffixIcon: IconButton(
icon: Icon(_isObscure ? Icons.visibility : Icons.visibility_off),
onPressed: () {
setState(() {
_isObscure = !_isObscure;
});
}
)
),
),
],
),
),
);
}
}

How to add a Password input type in flutter makes the password user input is not visible , just like Android Native EditText 's inputtype:password?

In case you are using the TextField widget (or something that derives from this widget), you can use the obscureText property and set it to true. More details can be found here.

Additionally, consider adding these properties to prevent input suggestions because they risk revealing at least part of the password input to screen viewers.

obscureText: true,
enableSuggestions: false,
autocorrect: false,

How to add a toggle for hiding and viewing password in Flutter?

Make your CustomRoundedTextField as StatefulWidget and add suffixIcon to TextField's InputDecoration. Below your code:

class CustomRoundedTextField extends StatefulWidget {
final label;
final onChange;
final isPassword;
final bottomPadding;
final textCapitalization;
final inputType;
final controller;
final iconData;


CustomRoundedTextField(
{
this.iconData,
this.controller,
this.inputType = TextInputType.text,
this.label,
this.onChange,
this.isPassword = false,
this.bottomPadding = 16,
this.textCapitalization = TextCapitalization.none});

@override
_CustomRoundedTextFieldState createState() => _CustomRoundedTextFieldState();
}

class _CustomRoundedTextFieldState extends State<CustomRoundedTextField> {
bool _showPwd = false;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: TextField(
controller: widget.controller,
keyboardType: widget.inputType,
textCapitalization: widget.textCapitalization,
obscureText: widget.isPassword && !_showPwd,
style:
TextStyle(fontSize: 15, color: Colors.black),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Colors.black54,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Colors.black,
),
),
labelText: widget.label,
labelStyle: TextStyle(
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.w600,
),
suffixIcon: widget.isPassword ? IconButton(
icon: Icon(_showPwd ? Icons.visibility : Icons.visibility_off),
onPressed:(){
setState((){
_showPwd = !_showPwd;
});
}
) : null,
),
onChanged: widget.onChange,
),
);
}
}

How to fully hide password input in Flutter?

There is no easy way to do that;
So would have to use controller and StatefulWidget that converts inputed value to "*"

class Testas extends StatefulWidget{
@override
_TestasState createState() => _TestasState();
}

class _TestasState extends State<Testas> {
String _valueToShow = "";
String _value = "";

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: TextField(
controller: TextEditingController.fromValue(TextEditingValue(text: _valueToShow, selection: new TextSelection.collapsed(offset: _valueToShow.length))),
onChanged: (String val){
String value = "";
if(val.length > _value.length){
value+=val.substring(_value.length, val.length);
}
if(val.length < _value.length){
value = _value.substring(1, val.length);
}
String valueToShow = "*" * val.length;
setState(() {
_valueToShow = valueToShow;
_value = value;

});
},
),
),
);
}
}


Related Topics



Leave a reply



Submit