From bd89ba6791dfe2762b37b3e291d8d1a7979cb3f5 Mon Sep 17 00:00:00 2001 From: Bruno D'Luka Date: Thu, 1 Sep 2022 09:09:51 -0300 Subject: [PATCH] Correctly keep the state of NavigationIndicator --- .../navigation_view/indicators.dart | 28 ++++++++++++++----- .../navigation_view/pane_items.dart | 27 +++++++++++++++++- lib/src/controls/surfaces/expander.dart | 11 ++++++-- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/lib/src/controls/navigation/navigation_view/indicators.dart b/lib/src/controls/navigation/navigation_view/indicators.dart index 6a33fddd2..c3e061d33 100644 --- a/lib/src/controls/navigation/navigation_view/indicators.dart +++ b/lib/src/controls/navigation/navigation_view/indicators.dart @@ -75,7 +75,7 @@ class NavigationIndicatorState extends State { } bool get isSelected { - return pane.isSelected(pane.effectiveItems[itemIndex]); + return pane.isSelected(item); } Axis get axis { @@ -94,15 +94,17 @@ class NavigationIndicatorState extends State { return InheritedNavigationView.of(context).oldIndex; } + PaneItem get item { + return pane.effectiveItems[itemIndex]; + } + /// The parent of this item, if any PaneItemExpander? get parent { - final items = InheritedNavigationView.of(context).pane!.effectiveItems; + final items = pane.effectiveItems; final expandableItems = items.whereType(); if (expandableItems.isEmpty) return null; - final item = - InheritedNavigationView.of(context).pane!.effectiveItems[itemIndex]; for (final expandable in expandableItems) { if (expandable.items.contains(item)) return expandable; } @@ -256,9 +258,17 @@ class _StickyNavigationIndicatorState return; } - _old = PageStorage.of(context)?.readState(context) as int? ?? _old; + _old = (PageStorage.of(context)?.readState( + context, + identifier: 'oldIndex$itemIndex', + ) as num?) + ?.toInt() ?? + _old; - if (_old == oldIndex) return; + // do not perform the animation twice + if (_old == oldIndex) { + return; + } if (isShowing) { if (isBelow) { @@ -300,7 +310,11 @@ class _StickyNavigationIndicatorState _old = oldIndex; if (mounted) { - PageStorage.of(context)?.writeState(context, _old); + PageStorage.of(context)?.writeState( + context, + _old, + identifier: 'oldIndex$itemIndex', + ); setState(() {}); } } diff --git a/lib/src/controls/navigation/navigation_view/pane_items.dart b/lib/src/controls/navigation/navigation_view/pane_items.dart index 3f6a3e437..dd2b748a3 100644 --- a/lib/src/controls/navigation/navigation_view/pane_items.dart +++ b/lib/src/controls/navigation/navigation_view/pane_items.dart @@ -596,12 +596,25 @@ class __PaneItemExpanderState extends State<_PaneItemExpander> return widget.displayMode != PaneDisplayMode.open; } - bool _open = false; + late bool _open; late AnimationController controller = AnimationController( vsync: this, duration: const Duration(milliseconds: 100), ); + @override + void initState() { + super.initState(); + _open = PageStorage.of(context)?.readState( + context, + identifier: 'paneItemExpanderOpen', + ) as bool? ?? + false; + if (_open) { + controller.value = 1; + } + } + @override void dispose() { controller.dispose(); @@ -611,6 +624,12 @@ class __PaneItemExpanderState extends State<_PaneItemExpander> void toggleOpen() { setState(() => _open = !_open); + + PageStorage.of(context)?.writeState( + context, + _open, + identifier: 'paneItemExpanderOpen', + ); if (useFlyout) { flyoutController.toggle(); } @@ -627,6 +646,12 @@ class __PaneItemExpanderState extends State<_PaneItemExpander> final theme = FluentTheme.of(context); final body = InheritedNavigationView.of(context); + _open = PageStorage.of(context)?.readState( + context, + identifier: 'paneItemExpanderOpen', + ) as bool? ?? + _open; + // Indexes // Ensure, if the child item is not visible, this is shown as the selected // item diff --git a/lib/src/controls/surfaces/expander.dart b/lib/src/controls/surfaces/expander.dart index 2927882bc..fc95d15f6 100644 --- a/lib/src/controls/surfaces/expander.dart +++ b/lib/src/controls/surfaces/expander.dart @@ -133,7 +133,10 @@ class ExpanderState extends State void initState() { super.initState(); _controller = AnimationController(vsync: this); - _open = PageStorage.of(context)?.readState(context) as bool? ?? + _open = PageStorage.of(context)?.readState( + context, + identifier: 'expanderOpen', + ) as bool? ?? widget.initiallyExpanded; if (_open == true) { _controller.value = 1; @@ -166,7 +169,11 @@ class ExpanderState extends State ); _open = true; } - PageStorage.of(context)?.writeState(context, open); + PageStorage.of(context)?.writeState( + context, + open, + identifier: 'expanderOpen', + ); widget.onStateChanged?.call(open); if (mounted) setState(() {}); }