I have a TextFormField which is empty by default.
I'm trying to handle the change when the user finishes editing the field (i don't have a confirm button)
How to trigger a function when the TextFormField loses focus ?
You can add a FocusNode to the field and add a listener to it, within the listener you can know if it gains/loses focus.
refer to How to listen focus change in flutter?
When you say TextFormField loses focus, I am assuming that whenever the user clicks anywhere other than the TextFormField on the screen, the TextFormField loses focus.
In that case, you can wrap the entire widget with a GestureDetector and inside the onTap function
GestureDetector(
onTap: (){}, //call the function here
);
Related
I want to show my own page on dropdown button tap, then item selected on page should be set as dropdown button value.
So basically, I need DropdownButton without any popup on tap. When I use onTap still default popup will be shown, how to prevent that?
why you don't use a button instead? or you can try to create an Inkwell put a container on its child and make it look like a button and write your code inside Inkwell OnTap(){}
1- Pass null to items parameter to disable the button.
2- You'll notice icon's color will be grey with disabled button, you can change it by setting color of the icon you send to icon parameter or send color directly to iconDisabledColor parameter.
3- You'll not use value parameter, instead you'll just use hint to show both your hint and your value. and update it using your state management after you pick new value from your own page.
4- Wrap your DropdownButton with GestureDetector or InkWell to show your own page when you tap on the button.
5- If you want to customize your DropdownButton shape, size and more. You can try my new package DropdownButton2. It's simple, easy, based on Flutter's core DropdownButton and have lots of features.
How can I implement default android Messages like a custom widget in place of the keyboard shown in the GIF?
When you tap on + button, what happens is,
Keyboard Closed, to achieve this you can use FocusNode instance and assign in TextField, to unfocus or to close keyboard, you can achieve it by focusNode.unfocus().
The content you see on tap of + is outside keyboard, i.e. you have to create widget and change it's visibility accordingly, Visibility can be used.
In other case misc options is visible and user tap on keyboard icon then change visibility of extra options and obtain focus of FocusNode instance(enable keyboard), by focusNode.requestFocus().
I am using a TextFormField as a widget and TextEditingController inside TextFormField for a phone number, and I am trying to validate a number while typing with the flutter_libphonenumber library, which provides an async function for validation.
I am trying to change the colour of the text depending upon validation status while entering it.
Current state:
Implemented TextFormField and providing it TextEditingController as a controller with valid initial text.
Implemented onFieldSubmitted and it works fine on tap of Done button of the keyboard.
Added listener to this controller for validation and set _isValid bool there.
I have used this bool to set colour of text inside the controller while building it.
So the current issue is, when I am entering text I get proper validated status inside the listener, but the colour of text is not changing accordingly.
I understand this is happening due to not calling setState after validation, so it will rebuild the widget and change the colour according to _isValid.
Error: When I try to setState inside the listener, it just doesn't allow editing.
Note: I have tried the same approach using onChanged method and it also does same, cannot enter or delete anything inside textField if there is pre-populated text (initialText).
Can anyone help me to solve this issue, or guide the correct path?
Thank you!
You can use the Form widget's autovalidate field by doing
Form(
autovalidate: true
In Flutter, we can either use GestureDetector onTap or Button to capture user press event. Is there any difference between them for accessibility like in the case of HTML (div vs button)?
Furthermore, how is GestureDetector translated into flutter web? Is it translated into a div with a onClick handler or a button?
Difference in accessibility
If you take a closer look at each of them, they both are triggered by the underlying callbacks.
onPressed for the Button
VoidCallback onPressed
Called when the button is tapped or otherwise activated.
If this callback and onLongPress are null, then the button will be disabled.
See also:
enabled, which is true if the button is enabled.
onTap for GestureDetector
GestureTapCallback onTap
A tap with a primary button has occurred.
This triggers when the tap gesture wins. If the tap gesture did not win, onTapCancel is called instead.
You will notice that there isn't much of a difference as both GestureTapCallback and VoidCallback are typedef equal to void Function(); used for the same purpose.
onTap and onPressed difference in flutter web
As for your second question, you can check my personal website built in flutter and inspect the source code yourself, I use both GestureDetector and Button, they translate to the exact same role="button" in the html output.
Documentation:
GestureDetector Class
TextButton Class
onPressed
onTap
I want to build autocomplete using TextFormField, I don't know how to create OnChanged event for TextFormField
My requirement is there will be text field with some text, if user edits that text, textfield should behave like auto suggest.
Lucky for us the onChanged option in the TextFormField is already available. If you update flutter and doesn't work just go to the text_form_field.dart file by clicking on the Widget TextFormField + command/control and modify the text with code in the link:
text_form_field.dart
Are you wrapping TextFormField in a FormField widget? If not, I suggest to use just TextField which has an onChange property
Edit: To set an initial value you can add a TextEditingController and asign it to the TextFormField
TextEditingController _controller = TextEditingController();
then in initState() you can do
_controller.text = 'Initial Value'