Auto Scroll in an empty field during validation in flutter - forms

I have created a form in flutter and during validation (when clicking on the button) I want it to scroll up to the empty field. I have tried many ways including FocusNode and because there are many textfields, its not working. I think I am doing it the wrong way. Can someone help?

create a diffrent focusNode for each TextFormField and then
call requestFocus() on the Node when the validation is false.
first some where
final FocusNode d = FocusNode();
then in the TextFormField Widegt
focusNode: d
then in your validat
d.requestFocus();
and redo that for every TextField and not forget to check for the currect Text Field in the validator

Related

How can I code a editable textfield in a CustomPainter in Flutter

I have a CustomPainter extending class. Until now, I paint only rectangles with some static text in it. Now I want to improve this a little bit and want that the user of my Flutter-App can edit this text.
I added a TextEditingController to my Object-Class and tried this:
TextField textField = TextField(
controller: object.textController,
);
textField.createRenderObject(context).paint;
textFieldPainter.paint(canvas, Offset.zero);
}
Unfortunaly, there is nothing like a textField.createRenderObject-Function in Flutter. So I look for a idea how I can get my a "controlled Text" working.
I also played around with TextSpan(). But I can't set the Controller to this.
The following steps are not the best solution , but you can try this solution.
Steps:
create an OverlayEntry, put a (hidden) TextField into it with a FocusNode and TextEditController.
after added the overlay entry, request focus on the focusNode, so the keyboard will open.
Add an onChanged to the TextField, and notify the painter somehow (e.g. valueNotifier) on text change.
In the TextPainer use the TextEditController.text value
For me, the following solution fit:
I built a function that checks if a click was done within the dimensions of the rectangle. If yes, a "properties dialog" opens (currently only the text) and here the text can now also be changed.
I know, this is only a workaround and not a real solution to my question, but maybe the approach helps one or the other.

Flutter focus: is FocusNode the thing that has focus?

In the focus system, there are a lot of widgets such as Focus, FocusNode, FocusScope, FocusScopeNode, etc. I'm trying to understand the focus system and have two specific questions:
Is a FocusNode the thing that is considered to be in focus? I know that FocusScopeNode has a hasFocus but it's not as if you can focus on a FocusScopeNode because you'd actually be focusing on it's FocusNode?
Is calling .requestFocus() or .unfocus() of a FocusNode the only way that focus will be removed or given to a FocusNode? (except for autofocus) In other words, things won't automatically be focused by clicking, tapping, or hitting TAB on them?
In the Flutter framework's focus system, a FocusNode is a representation of the focus state for a particular widget or group of widgets. When a FocusNode has focus, it means that it is currently the widget that is responding to keyboard events and other forms of input focus.
A FocusScopeNode is a widget that is used to create a scope within the focus tree. A scope defines a new node in the focus tree, and all FocusNodes within that scope are descendants of that scope's node. A FocusScopeNode also has a hasFocus property, which indicates whether any of the FocusNodes within its scope currently has focus.
To answer your first question, a FocusNode is considered to be in
focus when its hasFocus property is set to true. A FocusScopeNode is
not something that can be focused on directly, but rather it is a
way of organizing the focus tree and determining which nodes are
within a particular scope.
To answer your second question, calling requestFocus() or unfocus()
on a FocusNode is not the only way to remove or give focus to a
FocusNode. In Flutter, focus can also be changed by clicking or
tapping on a widget, or by using the TAB key to navigate through the
focus tree. However, these methods of changing focus will only work
if the FocusNode is part of the focus tree and is attached to a
widget that is part of the widget hierarchy.

TextFormField Focus Flutter

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

setState blocks editing of TextEditingController

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

Flutter TextFormField onChanged event

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'