Skip to content

Commit 1a53a0e

Browse files
refactor: implementation using focusNode
1 parent baeedf5 commit 1a53a0e

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

frontend/app_flowy/lib/plugins/grid/presentation/widgets/common/text_field.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ class InputTextField extends StatefulWidget {
99
final void Function(String)? onDone;
1010
final void Function(String)? onChanged;
1111
final void Function() onCanceled;
12-
final void Function()? onFocused;
1312
final void Function()? onTap;
14-
1513
final bool autoClearWhenDone;
1614
final String text;
1715
final int? maxLength;
16+
final FocusNode? focusNode;
1817

1918
const InputTextField({
2019
required this.text,
2120
this.onDone,
2221
required this.onCanceled,
2322
this.onChanged,
24-
this.onFocused,
2523
this.onTap,
2624
this.autoClearWhenDone = false,
2725
this.maxLength,
26+
this.focusNode,
2827
Key? key,
2928
}) : super(key: key);
3029

@@ -39,7 +38,7 @@ class _InputTextFieldState extends State<InputTextField> {
3938

4039
@override
4140
void initState() {
42-
_focusNode = FocusNode();
41+
_focusNode = widget.focusNode ?? FocusNode();
4342
_controller = TextEditingController(text: widget.text);
4443

4544
_focusNode.addListener(notifyDidEndEditing);
@@ -87,7 +86,10 @@ class _InputTextFieldState extends State<InputTextField> {
8786
@override
8887
void dispose() {
8988
_focusNode.removeListener(notifyDidEndEditing);
90-
_focusNode.dispose();
89+
// only dispose the focusNode if it was created in this widget's initState
90+
if (widget.focusNode == null) {
91+
_focusNode.dispose();
92+
}
9193
super.dispose();
9294
}
9395

@@ -100,10 +102,6 @@ class _InputTextFieldState extends State<InputTextField> {
100102
widget.onDone!(_controller.text);
101103
}
102104
}
103-
} else {
104-
if (widget.onFocused != null) {
105-
widget.onFocused!();
106-
}
107105
}
108106
}
109107
}

frontend/app_flowy/lib/plugins/grid/presentation/widgets/header/type_option/select_option.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,29 @@ class _AddOptionButton extends StatelessWidget {
267267
}
268268
}
269269

270-
class _CreateOptionTextField extends StatelessWidget {
270+
class _CreateOptionTextField extends StatefulWidget {
271271
final PopoverMutex? popoverMutex;
272-
const _CreateOptionTextField({this.popoverMutex, Key? key}) : super(key: key);
272+
const _CreateOptionTextField({
273+
super.key,
274+
this.popoverMutex,
275+
});
276+
277+
@override
278+
State<_CreateOptionTextField> createState() => __CreateOptionTextFieldState();
279+
}
280+
281+
class __CreateOptionTextFieldState extends State<_CreateOptionTextField> {
282+
late final FocusNode _focusNode;
283+
284+
@override
285+
void initState() {
286+
_focusNode = FocusNode();
287+
_focusNode.addListener(() {
288+
if (_focusNode.hasFocus) {
289+
widget.popoverMutex?.close();
290+
}
291+
});
292+
}
273293

274294
@override
275295
Widget build(BuildContext context) {
@@ -280,6 +300,7 @@ class _CreateOptionTextField extends StatelessWidget {
280300
autoClearWhenDone: true,
281301
maxLength: 30,
282302
text: text,
303+
focusNode: _focusNode,
283304
onCanceled: () {
284305
context
285306
.read<SelectOptionTypeOptionBloc>()
@@ -290,11 +311,8 @@ class _CreateOptionTextField extends StatelessWidget {
290311
.read<SelectOptionTypeOptionBloc>()
291312
.add(SelectOptionTypeOptionEvent.createOption(optionName));
292313
},
293-
onFocused: () {
294-
popoverMutex?.close();
295-
},
296314
onTap: () {
297-
popoverMutex?.close();
315+
widget.popoverMutex?.close();
298316
},
299317
);
300318
},

0 commit comments

Comments
 (0)