Skip to content

Commit

Permalink
Scroll to selection after keyboard opened (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P authored Mar 27, 2024
1 parent f6e5fe6 commit 3b28f7f
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ class RawEditorState extends EditorState
if (_hasFocus) {
// Listen for changing viewInsets, which indicates keyboard showing up.
WidgetsBinding.instance.addObserver(this);
_lastBottomViewInset = View.of(context).viewInsets.bottom;
_showCaretOnScreen();
// _lastBottomViewInset = WidgetsBinding.instance.window.viewInsets.bottom;
// if (!_value.selection.isValid) {
Expand Down Expand Up @@ -1539,7 +1540,7 @@ class RawEditorState extends EditorState

bool _showCaretOnScreenScheduled = false;

void _showCaretOnScreen() {
void _showCaretOnScreen([bool withAnimation = true]) {
if (!widget.showCursor || _showCaretOnScreenScheduled) {
return;
}
Expand All @@ -1564,11 +1565,16 @@ class RawEditorState extends EditorState
);

if (offset != null) {
_scrollController.animateTo(
math.min(offset, _scrollController.position.maxScrollExtent),
duration: _caretAnimationDuration,
curve: _caretAnimationCurve,
);
if (withAnimation) {
_scrollController.animateTo(
math.min(offset, _scrollController.position.maxScrollExtent),
duration: _caretAnimationDuration,
curve: _caretAnimationCurve,
);
} else {
_scrollController.jumpTo(
math.min(offset, _scrollController.position.maxScrollExtent));
}
}
});
}
Expand All @@ -1594,6 +1600,28 @@ class RawEditorState extends EditorState
}
}

late double _lastBottomViewInset;

@override
void didChangeMetrics() {
super.didChangeMetrics();
if (!mounted) {
return;
}
final bottomViewInset = View.of(context).viewInsets.bottom;
if (_lastBottomViewInset != bottomViewInset) {
SchedulerBinding.instance.addPostFrameCallback((_) {
_selectionOverlay?.updateForScroll();
});
if (_lastBottomViewInset < bottomViewInset) {
// Because the metrics change signal from engine will come here every frame
// (on both iOS and Android). So we don't need to show caret with animation.
_showCaretOnScreen(false);
}
}
_lastBottomViewInset = bottomViewInset;
}

// On MacOS some actions are sent as selectors. We need to manually find the right Action and invoke it.
// Ref: https://github.com/flutter/flutter/blob/3.7.0/packages/flutter/lib/src/widgets/editable_text.dart#L3731
@override
Expand Down

0 comments on commit 3b28f7f

Please sign in to comment.