Flutter TextFormField Obscure Password - flutter

Whenever I am defining the obscureText property of the TextFormField a suffix icon is automatically added at the end but after pressing it nothing happen,
The prefix icon is defined by me but the suffix icon is added automatically.
How to make that default icon interactive so that the password will be shown or hidden accordingly without adding our own suffix icon?
TextFormField(
style: TextStyle(color: Colors.black),
obscureText: true,
decoration: InputDecoration(
hintText: "Enter Password",
labelText: "Password",
prefixIcon: Icon(Icons.lock),
),
),

suffixIcon: IconButton(
onPressed: () => ShowHideFuncion(),
icon: Icon(Icons.YourIcon),
),

you need to make a variable in your widget of type bool (e.g. obscure). You then change the obscureText parameter from being true to being the variable you made.
Then add a suffixIcon with an IconButton() (see more here). In the IconButton()'s onPressed parameter set the variable to be not equal to the variable. (e.g. obscure = !obscure). There is a possibility you might need to make your widget a stateless widget and call setState(() => obscure = !obscure) if it doesn't update automatically.
The final code should look something like this:
Icon icon = Icon(Icons.visibility);
bool obscure = true;
TextFormField(
style: TextStyle(color: Colors.black),
obscureText: obscure,
decoration: InputDecoration(
hintText: "Enter Password",
labelText: "Password",
suffixIcon: IconButton(
onPressed: () {
setState(() {
if (obscure == true) {
obscure = false;
icon = Icon(Icons.visibility_off);
} else {
obscure = true;
icon = Icon(Icons.visibility);
}
});
},
icon: icon
),
),
),

This is because you are not changing the state of the obsecuretext.
In order to do that please declare variable of boolean type in main class.
bool isPassword = false;
Now in obscureText use the variable which you declared in main class as below.
`obscureText = isPasswordVisible`
Now make a Icon button to change the state of the boolean value by using set state as below.
suffixIcon: IconButton(
onPressed: () {
setState(() {
isPasswordVisible = !isPasswordVisible
});
},
icon: isPasswordVisible ? Icon(Icons.any_icon_you want) : Icon(Icons.any_Icon_you_want_to_display_when_obsecure_is_false)
),

Related

How to change suffixIcon to custom icon in text field in flutter

My requirement is need to use some different icon in place of suffixIcon in flutter but using suffixIcon there are in build icons like :
suffixIcon: IconButton(
//eye icon
color: Color(0xFF919191),
onPressed: () {
//for keyboard to hide
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
//for keyboard to hide
setState(() {
isHidePassword = !isHidePassword;
});
},
icon: Icon(isHidePassword
? Icons.visibility
: Icons.visibility_off)),,
How to use custom icon instead of visibility & visibility_off in suffixIcon in text field in flutter.
Attaching image for more understanding what i have tried using suffic icon is as below
what i need to achieve is
Any help is appreciated!
Use outlined Icon data for it.
Replace Icons.visibility with Icons.visibility_outlined and
Icons.visibility_off with Icons.visibility_off_outlined,
icon: Icon(
isHidePassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
check visibility_outlined-constant
Try below code hope its help to you.
declare bool variable:
bool obsecureText = true;
Hide/Unhide Password function
void showPassword() {
setState(() {
obsecureText = !obsecureText;
});
}
Your Widget:
TextFormField(
obscureText: obsecureText,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: showPassword,
icon: Icon(
obsecureText ? Icons.visibility_off : Icons.visibility,
),
),
prefixIcon: Icon(
Icons.vpn_key,
),
border: OutlineInputBorder(),
labelText: 'Password',
hintText: 'Enter Password Here',
),
),
Your result screen hide password ->
Your result screen display password->

How to show AllertDialog if Textfield is empty in Flutter

I'm building an application on flutter and i have a problem because of the list.
In flutter, I made an empty list, in this list filling by users. An assignment takes place when the user enters a value, but this always happens when a value is entered. If no value is entered, it reflects the previous value in the list. What I want is for the program to create an Alertdialog if no string value is entered in the TextField.
Here is my List (TaskData)
enter image description here
And my TextField
enter image description here
Try adding a errorText:
Full example
final _text = TextEditingController();
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Enter the Value',
errorText: _validate ? 'Value Can\'t Be Empty' : null, //here
),
),
Inside a Button use:
bool _validate = false;
RaisedButton(
onPressed: () {
setState(() {
_text.text.isEmpty ? _validate = true : _validate = false;
});
},
child: Text('Submit')
textColor: Colors.white,
color: Colors.blueAccent,
),
try this in your addTask method
void addTask() {
if (name = '') showMyAlertDialog();
else {nameList.add(name);
notifyListeners();}
name = '';
}

How to change the set value of a text field

So I have TextFormField and it has a set value. However when I try to change the value of it by typing in the Feild, it just reverts to the specified value. This is my code:
TextFormField(
onEditingComplete: () {
FocusScope.of(context).unfocus();
setState(() {
// THIS IS WHERE I NEED TO CHANGE ITS VALUE
});
},
controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
cursorHeight: 23.0,
decoration: InputDecoration(
disabledBorder: UnderlineInputBorder(),
border: OutlineInputBorder(),
hintText: "Enter Text",
labelText: "Title",
labelStyle: TextStyle(
fontSize: 20.0
),
),
),
I need to somehow set _titleController.text to the value that is in the field when enter or the tick button is clicked - but I cant just do _titleController.text = _titleController.text; for obvious reasons. Please help. Comment if you need more details.
change it to this:
#override
void initState() {
super.initState();
_titleController.text = newTask.title;
}
.
.
.
.
.
//And in your TextFormField
controller: _titleController,
You assign the value of newTask.title to _titleController.text
controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
Make it:
controller: _titleController

On web, how do I control the visibility icon that automatically appears on a focused TextFormField that has an obscureText property set to true?

Here's my code for the password field:
TextFormField(
obscureText: isObscure,
decoration: InputDecoration(
suffix: TextButton(
child: isPasswordObscure
? Text(
'Show',
style: TextStyle(color: Colors.grey),
)
: Text(
'Hide',
style: TextStyle(color: Colors.grey),
),
onPressed: () {
setState(() { isObscure = !isObscure; });
},
),
),
)
If I run it, the password field would look like this:
If you review my code, I only specified a text button and not an icon as the suffix. The visibility icon was added by Flutter Edge and when I click on it, it only changes its icon and does not unobscure or obscure the text field.
What I want to know is how do I change or remove the icon? And maybe also give it a callback so it knows what to do when I click on it.
The problem doesn't exist on mobile, only on browsers desktop Edge.
Edit:
I tried setting suffix and suffixIcon to null but the visibility icon is still showing.
Update: I've discovered that the problem only exists on MS Edge.
If you wants to turn off the visibility icon set onPressed: () {},
also if you want to remove the visibility icon form overview wrap it with opacity widget
Opacity(
opacity: 0.0,
child: textButton(),
Please find the below code sample to include the visibility option for the textField. by including a variable _isObscured in a stateful widget. we have implemented it with the auto obscure after 2 second delay.
Center(child: TextField(
obscureText: _isObscured,
decoration : InputDecoration(
suffix:InkWell(
onTap: (){
setState(() => this._isObscured =
!this._isObscured);
Future.delayed(Duration(seconds: 2), (){
setState(() => this._isObscured =
!this._isObscured);
});
},
child: Icon( Icons.visibility),
),
),
),
),
),
I found a solution:
// the magic function
void fixEdgePasswordRevealButton(FocusNode passwordFocusNode) {
passwordFocusNode.unfocus();
Future.microtask(() {
passwordFocusNode.requestFocus();
js.context.callMethod("fixPasswordCss", []);
});
}
// widget code
child: TextField(
onChanged: (_) async {
fixEdgePasswordRevealButton(passwordFocusNode);
},
focusNode: passwordFocusNode,
obscureText: true,
// end of index.html
window.fixPasswordCss = () => {
let style = document.createElement('style');
style.innerHTML = '::-ms-reveal { display: none; }';
document.head.appendChild(style);
}
</script>
</body>
Also posted on the relevant issue.

How to clear selected tags using flutter_tagging?

I need to use tags in my application. For this, I found the flutter_tagging library. My problem is that I do not know how to clear the selected tags when I click on the button.
FlutterTagging(
textFieldDecoration: InputDecoration(
border: OutlineInputBorder(),
hintText: "Tags",
labelText: "Enter tags"),
addButtonWidget: _buildAddButton(),
chipsColor: Colors.pinkAccent,
chipsFontColor: Colors.white,
deleteIcon: Icon(Icons.cancel,color: Colors.white),
chipsPadding: EdgeInsets.all(2.0),
chipsFontSize: 14.0,
chipsSpacing: 5.0,
chipsFontFamily: 'helvetica_neue_light',
suggestionsCallback: (pattern) async {
return await TagSearchService.getSuggestions(pattern);
},
onChanged: (result) {
setState(() {
text = result.toString();
});
},
)
I see two options here:
Fork library and add method that will clear Map _selectedTagValues variable.
Initiate Key taggingKey = UniqueKey() (UniqueKey) in your widget, pass it to FlutterTagging and call key.currentState.reset() when you have to reset it and rebuild whole FlutterTagging widget with their initial state.