Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SuperTextField] Fix setState called during build phase (Resolves #1980) #1981

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 35 additions & 27 deletions super_editor/lib/src/super_textfield/android/android_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,36 +382,44 @@ class SuperAndroidTextFieldState extends State<SuperAndroidTextField>
bool get _isMultiline => (widget.minLines ?? 1) != 1 || widget.maxLines != 1;

void _updateSelectionAndImeConnectionOnFocusChange() {
if (_focusNode.hasFocus) {
if (!_textEditingController.isAttachedToIme) {
_log.info('Attaching TextInputClient to TextInput');
// The focus change callback might be invoked in the build phase. It that's the case,
// defer the setState call until the end of the frame.
WidgetsBinding.instance.runAsSoonAsPossible(() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a method that automatically bails when not mounted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have for run code always on the next frame, but not to run it as soon as possible

if (!mounted) {
return;
}

if (_focusNode.hasFocus) {
if (!_textEditingController.isAttachedToIme) {
_log.info('Attaching TextInputClient to TextInput');
setState(() {
if (!_textEditingController.selection.isValid) {
_textEditingController.selection = TextSelection.collapsed(offset: _textEditingController.text.length);
}

if (widget.imeConfiguration != null) {
_textEditingController.attachToImeWithConfig(widget.imeConfiguration!);
} else {
_textEditingController.attachToIme(
textInputAction: widget.textInputAction ?? TextInputAction.done,
textInputType: _isMultiline ? TextInputType.multiline : TextInputType.text,
);
}

_autoScrollToKeepTextFieldVisible();
_showEditingControlsOverlay();
});
}
} else {
_log.info('Lost focus. Detaching TextInputClient from TextInput.');
setState(() {
if (!_textEditingController.selection.isValid) {
_textEditingController.selection = TextSelection.collapsed(offset: _textEditingController.text.length);
}

if (widget.imeConfiguration != null) {
_textEditingController.attachToImeWithConfig(widget.imeConfiguration!);
} else {
_textEditingController.attachToIme(
textInputAction: widget.textInputAction ?? TextInputAction.done,
textInputType: _isMultiline ? TextInputType.multiline : TextInputType.text,
);
}

_autoScrollToKeepTextFieldVisible();
_showEditingControlsOverlay();
_textEditingController.detachFromIme();
_textEditingController.selection = const TextSelection.collapsed(offset: -1);
_textEditingController.composingRegion = TextRange.empty;
_removeEditingOverlayControls();
});
}
} else {
_log.info('Lost focus. Detaching TextInputClient from TextInput.');
setState(() {
_textEditingController.detachFromIme();
_textEditingController.selection = const TextSelection.collapsed(offset: -1);
_textEditingController.composingRegion = TextRange.empty;
_removeEditingOverlayControls();
});
}
});
}

void _onTextOrSelectionChange() {
Expand Down
62 changes: 35 additions & 27 deletions super_editor/lib/src/super_textfield/ios/ios_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -399,36 +399,44 @@ class SuperIOSTextFieldState extends State<SuperIOSTextField>
DeltaTextInputClient get imeClient => _textEditingController;

void _updateSelectionAndImeConnectionOnFocusChange() {
if (_focusNode.hasFocus) {
if (!_textEditingController.isAttachedToIme) {
_log.info('Attaching TextInputClient to TextInput');
// The focus change callback might be invoked in the build phase. It that's the case,
// defer the setState call until the end of the frame.
WidgetsBinding.instance.runAsSoonAsPossible(() {
if (!mounted) {
return;
}

if (_focusNode.hasFocus) {
if (!_textEditingController.isAttachedToIme) {
_log.info('Attaching TextInputClient to TextInput');
setState(() {
if (!_textEditingController.selection.isValid) {
_textEditingController.selection = TextSelection.collapsed(offset: _textEditingController.text.length);
}

if (widget.imeConfiguration != null) {
_textEditingController.attachToImeWithConfig(widget.imeConfiguration!);
} else {
_textEditingController.attachToIme(
textInputAction: widget.textInputAction ?? TextInputAction.done,
textInputType: _isMultiline ? TextInputType.multiline : TextInputType.text,
);
}

_autoScrollToKeepTextFieldVisible();
_showHandles();
});
}
} else {
_log.info('Lost focus. Detaching TextInputClient from TextInput.');
setState(() {
if (!_textEditingController.selection.isValid) {
_textEditingController.selection = TextSelection.collapsed(offset: _textEditingController.text.length);
}

if (widget.imeConfiguration != null) {
_textEditingController.attachToImeWithConfig(widget.imeConfiguration!);
} else {
_textEditingController.attachToIme(
textInputAction: widget.textInputAction ?? TextInputAction.done,
textInputType: _isMultiline ? TextInputType.multiline : TextInputType.text,
);
}

_autoScrollToKeepTextFieldVisible();
_showHandles();
_textEditingController.detachFromIme();
_textEditingController.selection = const TextSelection.collapsed(offset: -1);
_textEditingController.composingRegion = TextRange.empty;
_removeEditingOverlayControls();
});
}
} else {
_log.info('Lost focus. Detaching TextInputClient from TextInput.');
setState(() {
_textEditingController.detachFromIme();
_textEditingController.selection = const TextSelection.collapsed(offset: -1);
_textEditingController.composingRegion = TextRange.empty;
_removeEditingOverlayControls();
});
}
});
}

void _onTextOrSelectionChange() {
Expand Down