Skip to content

Commit e222582

Browse files
committed
Set FocusTraversalGroup policy
1 parent 1c1d6fa commit e222582

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

lib/src/form_builder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ class FormBuilderState extends State<FormBuilder> {
166166
Widget build(BuildContext context) {
167167
return Form(
168168
key: _formKey,
169-
child: FocusTraversalGroup(child: widget.child),
169+
child: FocusTraversalGroup(
170+
policy: WidgetOrderTraversalPolicy(),
171+
child: widget.child,
172+
),
170173
autovalidate: widget.autovalidate,
171174
onWillPop: widget.onWillPop,
172175
onChanged: () {

lib/src/form_builder_field.dart

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/scheduler.dart';
3-
import 'package:flutter/services.dart';
43
import 'package:flutter_form_builder/flutter_form_builder.dart';
54

65
class FormBuilderField<T> extends FormField<T> {
@@ -71,9 +70,11 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
7170

7271
T _initialValue;
7372

74-
FocusNode _node;
73+
FocusNode _focusNode;
7574

76-
bool _focused = false;
75+
FocusNode get _effectiveFocusNode =>
76+
widget.focusNode ??
77+
(_focusNode ??= FocusNode(debugLabel: '${widget.attribute}'));
7778

7879
@override
7980
void initState() {
@@ -86,31 +87,9 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
8687
false)
8788
? _formBuilderState.initialValue[widget.attribute]
8889
: null);
89-
_node = widget.focusNode ?? FocusNode(debugLabel: '${widget.attribute}');
90-
_node.addListener(_handleFocusChange);
91-
_node.attach(context, onKey: _handleKeyPress);
9290
setValue(_initialValue);
9391
}
9492

95-
void _handleFocusChange() {
96-
if (_node.hasFocus != _focused) {
97-
setState(() {
98-
_focused = _node.hasFocus;
99-
});
100-
}
101-
}
102-
103-
bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
104-
if (event is RawKeyDownEvent) {
105-
print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
106-
if (event.logicalKey == LogicalKeyboardKey.tab) {
107-
FocusScope.of(context).nextFocus();
108-
return true;
109-
}
110-
}
111-
return false;
112-
}
113-
11493
@override
11594
void save() {
11695
super.save();
@@ -130,7 +109,7 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
130109
@override
131110
void reset() {
132111
super.reset();
133-
setValue(_initialValue);
112+
setValue(initialValue);
134113
widget.onReset?.call();
135114
}
136115

@@ -140,15 +119,14 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
140119
}
141120

142121
void requestFocus() {
143-
FocusScope.of(context).requestFocus(_node);
122+
FocusScope.of(context).requestFocus(_effectiveFocusNode);
144123
}
145124

146125
@override
147126
void dispose() {
148127
_formBuilderState?.unregisterFieldKey(widget.attribute);
149-
_node.removeListener(_handleFocusChange);
150128
// The attachment will automatically be detached in dispose().
151-
_node.dispose();
129+
_focusNode?.dispose();
152130
super.dispose();
153131
}
154132
}

0 commit comments

Comments
 (0)