Skip to content

Commit bd89ba6

Browse files
committed
Correctly keep the state of NavigationIndicator
1 parent cf0fae1 commit bd89ba6

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

lib/src/controls/navigation/navigation_view/indicators.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class NavigationIndicatorState<T extends NavigationIndicator> extends State<T> {
7575
}
7676

7777
bool get isSelected {
78-
return pane.isSelected(pane.effectiveItems[itemIndex]);
78+
return pane.isSelected(item);
7979
}
8080

8181
Axis get axis {
@@ -94,15 +94,17 @@ class NavigationIndicatorState<T extends NavigationIndicator> extends State<T> {
9494
return InheritedNavigationView.of(context).oldIndex;
9595
}
9696

97+
PaneItem get item {
98+
return pane.effectiveItems[itemIndex];
99+
}
100+
97101
/// The parent of this item, if any
98102
PaneItemExpander? get parent {
99-
final items = InheritedNavigationView.of(context).pane!.effectiveItems;
103+
final items = pane.effectiveItems;
100104

101105
final expandableItems = items.whereType<PaneItemExpander>();
102106
if (expandableItems.isEmpty) return null;
103107

104-
final item =
105-
InheritedNavigationView.of(context).pane!.effectiveItems[itemIndex];
106108
for (final expandable in expandableItems) {
107109
if (expandable.items.contains(item)) return expandable;
108110
}
@@ -256,9 +258,17 @@ class _StickyNavigationIndicatorState
256258
return;
257259
}
258260

259-
_old = PageStorage.of(context)?.readState(context) as int? ?? _old;
261+
_old = (PageStorage.of(context)?.readState(
262+
context,
263+
identifier: 'oldIndex$itemIndex',
264+
) as num?)
265+
?.toInt() ??
266+
_old;
260267

261-
if (_old == oldIndex) return;
268+
// do not perform the animation twice
269+
if (_old == oldIndex) {
270+
return;
271+
}
262272

263273
if (isShowing) {
264274
if (isBelow) {
@@ -300,7 +310,11 @@ class _StickyNavigationIndicatorState
300310

301311
_old = oldIndex;
302312
if (mounted) {
303-
PageStorage.of(context)?.writeState(context, _old);
313+
PageStorage.of(context)?.writeState(
314+
context,
315+
_old,
316+
identifier: 'oldIndex$itemIndex',
317+
);
304318
setState(() {});
305319
}
306320
}

lib/src/controls/navigation/navigation_view/pane_items.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,25 @@ class __PaneItemExpanderState extends State<_PaneItemExpander>
596596
return widget.displayMode != PaneDisplayMode.open;
597597
}
598598

599-
bool _open = false;
599+
late bool _open;
600600
late AnimationController controller = AnimationController(
601601
vsync: this,
602602
duration: const Duration(milliseconds: 100),
603603
);
604604

605+
@override
606+
void initState() {
607+
super.initState();
608+
_open = PageStorage.of(context)?.readState(
609+
context,
610+
identifier: 'paneItemExpanderOpen',
611+
) as bool? ??
612+
false;
613+
if (_open) {
614+
controller.value = 1;
615+
}
616+
}
617+
605618
@override
606619
void dispose() {
607620
controller.dispose();
@@ -611,6 +624,12 @@ class __PaneItemExpanderState extends State<_PaneItemExpander>
611624

612625
void toggleOpen() {
613626
setState(() => _open = !_open);
627+
628+
PageStorage.of(context)?.writeState(
629+
context,
630+
_open,
631+
identifier: 'paneItemExpanderOpen',
632+
);
614633
if (useFlyout) {
615634
flyoutController.toggle();
616635
}
@@ -627,6 +646,12 @@ class __PaneItemExpanderState extends State<_PaneItemExpander>
627646
final theme = FluentTheme.of(context);
628647
final body = InheritedNavigationView.of(context);
629648

649+
_open = PageStorage.of(context)?.readState(
650+
context,
651+
identifier: 'paneItemExpanderOpen',
652+
) as bool? ??
653+
_open;
654+
630655
// Indexes
631656
// Ensure, if the child item is not visible, this is shown as the selected
632657
// item

lib/src/controls/surfaces/expander.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ class ExpanderState extends State<Expander>
133133
void initState() {
134134
super.initState();
135135
_controller = AnimationController(vsync: this);
136-
_open = PageStorage.of(context)?.readState(context) as bool? ??
136+
_open = PageStorage.of(context)?.readState(
137+
context,
138+
identifier: 'expanderOpen',
139+
) as bool? ??
137140
widget.initiallyExpanded;
138141
if (_open == true) {
139142
_controller.value = 1;
@@ -166,7 +169,11 @@ class ExpanderState extends State<Expander>
166169
);
167170
_open = true;
168171
}
169-
PageStorage.of(context)?.writeState(context, open);
172+
PageStorage.of(context)?.writeState(
173+
context,
174+
open,
175+
identifier: 'expanderOpen',
176+
);
170177
widget.onStateChanged?.call(open);
171178
if (mounted) setState(() {});
172179
}

0 commit comments

Comments
 (0)