fix: do not recreate the toolbar when the selection not changed (#1086)
This commit is contained in:
parent
e214cfea4a
commit
d5c9f64ebe
2 changed files with 34 additions and 0 deletions
|
|
@ -75,6 +75,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
late Brightness brightness = Theme.of(context).brightness;
|
||||
|
||||
bool hasMetricsChanged = false;
|
||||
Selection? lastSelection;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
|
@ -82,6 +83,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
editorState.selectionNotifier.addListener(_onSelectionChanged);
|
||||
lastSelection = editorState.selection;
|
||||
widget.editorScrollController.offsetNotifier.addListener(
|
||||
_onScrollPositionChanged,
|
||||
);
|
||||
|
|
@ -133,6 +135,8 @@ class _FloatingToolbarState extends State<FloatingToolbar>
|
|||
void _onSelectionChanged() {
|
||||
final selection = editorState.selection;
|
||||
final selectionType = editorState.selectionType;
|
||||
if (lastSelection == selection) return;
|
||||
lastSelection = selection;
|
||||
|
||||
if (selection == null ||
|
||||
selection.isCollapsed ||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,32 @@ class FloatingToolbarWidget extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
|
||||
EditorState get editorState => widget.editorState;
|
||||
PropertyValueNotifier<Selection?> get selectionNotifier =>
|
||||
editorState.selectionNotifier;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
selectionNotifier.addListener(_onSelectionChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(FloatingToolbarWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.editorState != oldWidget.editorState) {
|
||||
oldWidget.editorState.selectionNotifier
|
||||
.removeListener(_onSelectionChanged);
|
||||
selectionNotifier.addListener(_onSelectionChanged);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
selectionNotifier.removeListener(_onSelectionChanged);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var activeItems = _computeActiveItems();
|
||||
|
|
@ -97,6 +123,10 @@ class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
|
|||
);
|
||||
}
|
||||
|
||||
void _onSelectionChanged() {
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
Iterable<ToolbarItem> _computeActiveItems() {
|
||||
final activeItems = widget.items
|
||||
.where((e) => e.isActive?.call(widget.editorState) ?? false)
|
||||
|
|
|
|||
Loading…
Reference in a new issue