Skip to content

Commit

Permalink
Correctly keep the state of NavigationIndicator
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed Sep 1, 2022
1 parent cf0fae1 commit bd89ba6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
28 changes: 21 additions & 7 deletions lib/src/controls/navigation/navigation_view/indicators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class NavigationIndicatorState<T extends NavigationIndicator> extends State<T> {
}

bool get isSelected {
return pane.isSelected(pane.effectiveItems[itemIndex]);
return pane.isSelected(item);
}

Axis get axis {
Expand All @@ -94,15 +94,17 @@ class NavigationIndicatorState<T extends NavigationIndicator> extends State<T> {
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<PaneItemExpander>();
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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(() {});
}
}
Expand Down
27 changes: 26 additions & 1 deletion lib/src/controls/navigation/navigation_view/pane_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/src/controls/surfaces/expander.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ class ExpanderState extends State<Expander>
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;
Expand Down Expand Up @@ -166,7 +169,11 @@ class ExpanderState extends State<Expander>
);
_open = true;
}
PageStorage.of(context)?.writeState(context, open);
PageStorage.of(context)?.writeState(
context,
open,
identifier: 'expanderOpen',
);
widget.onStateChanged?.call(open);
if (mounted) setState(() {});
}
Expand Down

0 comments on commit bd89ba6

Please sign in to comment.