Skip to content

Commit

Permalink
Support history clearing (tomgilder#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
smihica authored Dec 26, 2022
1 parent e6f04ec commit decb11a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/route_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ class RouteHistory {
return true;
}

/// Clear the navigation history.
///
/// Does nothing if there's no history entry.
void clear() {
if (_index < 0) return;
final last = _history[_index];
_index = 0;
_history.clear();
_history.add(last);
}

// coverage:ignore-start
void _goToIndex(int index) {
if (index == _index) {
Expand Down
49 changes: 49 additions & 0 deletions test/history_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,53 @@ void main() {
await tester.pumpPageTransition();
expect(find.byType(PageThree), findsOneWidget);
});

testWidgets('Check clearing history behavior', (tester) async {
final pageOneKey = GlobalKey();
final routeMap = RouteMap(
routes: {
'/': (_) => MaterialPage<void>(child: PageOne(key: pageOneKey)),
'/two': (_) => const MaterialPageTwo(),
'/two/three': (_) => const MaterialPageThree(),
},
);
final delegate = RoutemasterDelegate(routesBuilder: (_) => routeMap);

await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: const RoutemasterParser(),
routerDelegate: delegate,
),
);
await tester.pump();

final history = delegate.history;

// Push: / -> /two
delegate.push('/two');
await tester.pumpPageTransition();
expect(find.byType(PageTwo), findsOneWidget);

// Push: /two -> /two/three
delegate.push('/two/three');
await tester.pumpPageTransition();
expect(find.byType(PageThree), findsOneWidget);

// Back to page /two
history.back();
await tester.pumpPageTransition();

// Can go back -> /
expect(history.canGoBack, isTrue);
// Can go forward -> /two/three
expect(history.canGoForward, isTrue);

// Clear the history
history.clear();

// Cannot go back
expect(history.canGoBack, isFalse);
// Cannot go forward
expect(history.canGoForward, isFalse);
});
}

0 comments on commit decb11a

Please sign in to comment.