fix: add isMetricsChanged to FloatingToolbarBuilder (#1081)
This commit is contained in:
parent
f46e991d0a
commit
c40ee05b39
1 changed files with 19 additions and 8 deletions
|
|
@ -23,6 +23,7 @@ typedef FloatingToolbarBuilder = Widget Function(
|
|||
BuildContext context,
|
||||
Widget child,
|
||||
VoidCallback onDismiss,
|
||||
bool isMetricsChanged,
|
||||
);
|
||||
|
||||
/// A floating toolbar that displays at the top of the editor when the selection
|
||||
|
|
@ -73,6 +74,8 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
|
||||
late Brightness brightness = Theme.of(context).brightness;
|
||||
|
||||
bool hasMetricsChanged = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
|
@ -118,8 +121,8 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
@override
|
||||
void didChangeMetrics() {
|
||||
super.didChangeMetrics();
|
||||
|
||||
_showAfterDelay();
|
||||
hasMetricsChanged = true;
|
||||
_showAfterDelay(isMetricsChanged: true);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -139,7 +142,11 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
_clear();
|
||||
} else {
|
||||
// uses debounce to avoid the computing the rects too frequently.
|
||||
_showAfterDelay(const Duration(milliseconds: 200));
|
||||
_showAfterDelay(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
isMetricsChanged: hasMetricsChanged,
|
||||
);
|
||||
if (hasMetricsChanged) hasMetricsChanged = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -148,7 +155,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
|
||||
// TODO: optimize the toolbar showing logic, making it more smooth.
|
||||
// A quick idea: based on the scroll controller's offset to display the toolbar.
|
||||
_showAfterDelay(Duration.zero);
|
||||
_showAfterDelay();
|
||||
}
|
||||
|
||||
final String _debounceKey = 'show the toolbar';
|
||||
|
|
@ -160,19 +167,22 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
_toolbarContainer = null;
|
||||
}
|
||||
|
||||
void _showAfterDelay([Duration duration = Duration.zero]) {
|
||||
void _showAfterDelay({
|
||||
Duration duration = Duration.zero,
|
||||
bool isMetricsChanged = false,
|
||||
}) {
|
||||
// uses debounce to avoid the computing the rects too frequently.
|
||||
Debounce.debounce(
|
||||
_debounceKey,
|
||||
duration,
|
||||
() {
|
||||
_clear(); // clear the previous toolbar.
|
||||
_showToolbar();
|
||||
_showToolbar(isMetricsChanged);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showToolbar() {
|
||||
void _showToolbar(bool isMetricsChanged) {
|
||||
final selection = editorState.selection;
|
||||
if (selection == null || selection.isCollapsed) {
|
||||
return;
|
||||
|
|
@ -212,7 +222,8 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
_toolbarContainer = OverlayEntry(
|
||||
builder: (context) {
|
||||
final child = _buildToolbar(context);
|
||||
return widget.toolbarBuilder?.call(context, child, _clear) ??
|
||||
return widget.toolbarBuilder
|
||||
?.call(context, child, _clear, isMetricsChanged) ??
|
||||
Positioned(
|
||||
top: max(0, top) - floatingToolbarHeight,
|
||||
left: left,
|
||||
|
|
|
|||
Loading…
Reference in a new issue