Merge pull request #1384 from Cyrine-benabid/fix-option_button_doesnt-close_popover

Close popover on text field tap
This commit is contained in:
Nathan.fooo
2022-11-07 17:36:44 +08:00
committed by GitHub
2 changed files with 42 additions and 7 deletions

View File

@ -13,6 +13,7 @@ class InputTextField extends StatefulWidget {
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,
@ -21,6 +22,7 @@ class InputTextField extends StatefulWidget {
this.onChanged, this.onChanged,
this.autoClearWhenDone = false, this.autoClearWhenDone = false,
this.maxLength, this.maxLength,
this.focusNode,
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@ -35,7 +37,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);
SchedulerBinding.instance.addPostFrameCallback((Duration _) { SchedulerBinding.instance.addPostFrameCallback((Duration _) {
_focusNode.requestFocus(); _focusNode.requestFocus();
@ -81,7 +83,10 @@ class _InputTextFieldState extends State<InputTextField> {
@override @override
void dispose() { void dispose() {
_focusNode.removeListener(notifyDidEndEditing); _focusNode.removeListener(notifyDidEndEditing);
// only dispose the focusNode if it was created in this widget's initState
if (widget.focusNode == null) {
_focusNode.dispose(); _focusNode.dispose();
}
super.dispose(); super.dispose();
} }

View File

@ -45,9 +45,11 @@ class SelectOptionTypeOptionWidget extends StatelessWidget {
const TypeOptionSeparator(), const TypeOptionSeparator(),
const OptionTitle(), const OptionTitle(),
if (state.isEditingOption) if (state.isEditingOption)
const Padding( Padding(
padding: EdgeInsets.only(bottom: 10), padding: const EdgeInsets.only(bottom: 10),
child: _CreateOptionTextField(), child: _CreateOptionTextField(
popoverMutex: popoverMutex,
),
), ),
if (state.options.isEmpty && !state.isEditingOption) if (state.options.isEmpty && !state.isEditingOption)
const _AddOptionButton(), const _AddOptionButton(),
@ -251,8 +253,35 @@ class _AddOptionButton extends StatelessWidget {
} }
} }
class _CreateOptionTextField extends StatelessWidget { class _CreateOptionTextField extends StatefulWidget {
const _CreateOptionTextField({Key? key}) : super(key: key); final PopoverMutex? popoverMutex;
const _CreateOptionTextField({
Key? key,
this.popoverMutex,
}) : super(key: key);
@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();
}
});
widget.popoverMutex?.listenOnPopoverChanged(() {
if (_focusNode.hasFocus) {
_focusNode.unfocus();
}
});
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -263,6 +292,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>()