mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: implementation using focusNode
This commit is contained in:
parent
baeedf557d
commit
1a53a0e375
@ -9,22 +9,21 @@ class InputTextField extends StatefulWidget {
|
|||||||
final void Function(String)? onDone;
|
final void Function(String)? onDone;
|
||||||
final void Function(String)? onChanged;
|
final void Function(String)? onChanged;
|
||||||
final void Function() onCanceled;
|
final void Function() onCanceled;
|
||||||
final void Function()? onFocused;
|
|
||||||
final void Function()? onTap;
|
final void Function()? onTap;
|
||||||
|
|
||||||
final bool autoClearWhenDone;
|
final bool autoClearWhenDone;
|
||||||
final String text;
|
final String text;
|
||||||
final int? maxLength;
|
final int? maxLength;
|
||||||
|
final FocusNode? focusNode;
|
||||||
|
|
||||||
const InputTextField({
|
const InputTextField({
|
||||||
required this.text,
|
required this.text,
|
||||||
this.onDone,
|
this.onDone,
|
||||||
required this.onCanceled,
|
required this.onCanceled,
|
||||||
this.onChanged,
|
this.onChanged,
|
||||||
this.onFocused,
|
|
||||||
this.onTap,
|
this.onTap,
|
||||||
this.autoClearWhenDone = false,
|
this.autoClearWhenDone = false,
|
||||||
this.maxLength,
|
this.maxLength,
|
||||||
|
this.focusNode,
|
||||||
Key? key,
|
Key? key,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@ -39,7 +38,7 @@ class _InputTextFieldState extends State<InputTextField> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
_focusNode = FocusNode();
|
_focusNode = widget.focusNode ?? FocusNode();
|
||||||
_controller = TextEditingController(text: widget.text);
|
_controller = TextEditingController(text: widget.text);
|
||||||
|
|
||||||
_focusNode.addListener(notifyDidEndEditing);
|
_focusNode.addListener(notifyDidEndEditing);
|
||||||
@ -87,7 +86,10 @@ class _InputTextFieldState extends State<InputTextField> {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_focusNode.removeListener(notifyDidEndEditing);
|
_focusNode.removeListener(notifyDidEndEditing);
|
||||||
_focusNode.dispose();
|
// only dispose the focusNode if it was created in this widget's initState
|
||||||
|
if (widget.focusNode == null) {
|
||||||
|
_focusNode.dispose();
|
||||||
|
}
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,10 +102,6 @@ class _InputTextFieldState extends State<InputTextField> {
|
|||||||
widget.onDone!(_controller.text);
|
widget.onDone!(_controller.text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (widget.onFocused != null) {
|
|
||||||
widget.onFocused!();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,9 +267,29 @@ class _AddOptionButton extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _CreateOptionTextField extends StatelessWidget {
|
class _CreateOptionTextField extends StatefulWidget {
|
||||||
final PopoverMutex? popoverMutex;
|
final PopoverMutex? popoverMutex;
|
||||||
const _CreateOptionTextField({this.popoverMutex, Key? key}) : super(key: key);
|
const _CreateOptionTextField({
|
||||||
|
super.key,
|
||||||
|
this.popoverMutex,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_CreateOptionTextField> createState() => __CreateOptionTextFieldState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class __CreateOptionTextFieldState extends State<_CreateOptionTextField> {
|
||||||
|
late final FocusNode _focusNode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
_focusNode = FocusNode();
|
||||||
|
_focusNode.addListener(() {
|
||||||
|
if (_focusNode.hasFocus) {
|
||||||
|
widget.popoverMutex?.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -280,6 +300,7 @@ class _CreateOptionTextField extends StatelessWidget {
|
|||||||
autoClearWhenDone: true,
|
autoClearWhenDone: true,
|
||||||
maxLength: 30,
|
maxLength: 30,
|
||||||
text: text,
|
text: text,
|
||||||
|
focusNode: _focusNode,
|
||||||
onCanceled: () {
|
onCanceled: () {
|
||||||
context
|
context
|
||||||
.read<SelectOptionTypeOptionBloc>()
|
.read<SelectOptionTypeOptionBloc>()
|
||||||
@ -290,11 +311,8 @@ class _CreateOptionTextField extends StatelessWidget {
|
|||||||
.read<SelectOptionTypeOptionBloc>()
|
.read<SelectOptionTypeOptionBloc>()
|
||||||
.add(SelectOptionTypeOptionEvent.createOption(optionName));
|
.add(SelectOptionTypeOptionEvent.createOption(optionName));
|
||||||
},
|
},
|
||||||
onFocused: () {
|
|
||||||
popoverMutex?.close();
|
|
||||||
},
|
|
||||||
onTap: () {
|
onTap: () {
|
||||||
popoverMutex?.close();
|
widget.popoverMutex?.close();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user