How to change suffixIcon to custom icon in text field in flutter - 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->

Related

How to make the icon in TextFormField enabled in code?

I have set a color to an icon in a TextFormField, but the color only comes when the TextFormField is focused.
How do I set it so that upon a setState() the icon stays 'focused' or colored?
try this:
bool _textIsEmpty = true;
TextEditingController _controller= TextEditingController();
... // rest of your code
TextField(
controller: _controller,
decoration: InputDecoration(
icon: Icon(
Icons.abc,
color: _textIsEmpty ? Colors.transparent : Colors.pink,
),
),
onChanged: (value) {
setState(() {
_textIsEmpty = value.isEmpty;
});
},
),

Flutter TextFormField Obscure Password

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)
),

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.

storing input textfield on flutter

Im implementing voice search on flutter. Here I have an icon for voice search and when clicked, a function is called and the resulting string is stored in String _text.
IconButton(
onPressed: (){
_listen();
})},
icon: Icon(_isListening ? Icons.mic : Icons.mic_none, ),
),
I also have a searchbar with a controller txt where the inputs are stored
TextField(
controller: txt,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'What are you looking for?',),
onSubmitted: (txt){}
my problem is how can i store the _text string to the searchbar when they use voice search?

Flutter, is it possible to add a Icon inside TextFormField 'hinttext' prorperty?

is it possible to add a Icon inside TextFormField 'hinttext' prorperty?
I know about this shown below
InputDecoration(
prefixIcon: Icon(
Icons.lock_outline,
)
)
but what I want currently is hint Icon
so the if users click the hint then 'hintText' and 'hintIcon' would be disappeared
is there a way to achieve this? thanks
You should be able to achieve what you want by using states.
InputDecoration(
prefixIcon: _showHint ? IconButton(
icon: Icon(
Icons.lock_outline,
),
onPressed: () {
setState(() {
_showHint = false;
});
},
) : Container(),
);
To reset the _showHint, you'll want to use FocusNode to listen to events. The documentation of FocusNode describes that.