Skip to content

Commit

Permalink
Merge pull request #30 from Moncader/fix/page-data-not-updating-route
Browse files Browse the repository at this point in the history
Fixes new pageData not being used in old StandardPage's brought back to the top of the navigation stack.
  • Loading branch information
Moncader authored Jan 8, 2025
2 parents 700dcb8 + 84755b5 commit b0dd00b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/patapata_core/lib/src/widgets/standard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,13 @@ abstract class StandardPageWithResult<T extends Object?, E extends Object?>

return;
}

// TODO: In the future, we should look at use cases here
// And decide if we want to update the current navigation
// data (like the URL) or not. Currently, we do not.
// If we were to, we'd need to update _pageInstanceToRouteData
// with the newest pageData.

_sendPageDataEvent();
(Router.of(context).routerDelegate as StandardRouterDelegate?)
?._updatePages();
Expand Down Expand Up @@ -1192,8 +1199,9 @@ class StandardRouteInformationParser

@override
RouteInformation? restoreRouteInformation(StandardRouteData configuration) {
final tStringLocation =
configuration.factory?.generateLink(configuration.pageData);
final tStringLocation = configuration.factory?.generateLink(
configuration.pageData ??
configuration.factory?.pageDataWhenNull?.call());

if (tStringLocation != null) {
final tLocation = Uri.tryParse(tStringLocation);
Expand Down Expand Up @@ -1897,9 +1905,12 @@ class StandardRouterDelegate extends RouterDelegate<StandardRouteData>
?.standardPageKey
.currentState;

bool tLastPageDataChanged = false;

if (tLastPage != null) {
if (tLastPage.pageData != pageData) {
tLastPage.pageData = pageData;
tLastPageDataChanged = true;
}

if (tLastPage.active) {
Expand Down Expand Up @@ -1929,6 +1940,13 @@ class StandardRouterDelegate extends RouterDelegate<StandardRouteData>

final tFuture = (tCompleter as Completer<E?>).future;

if (tLastPageDataChanged) {
_pageInstanceToRouteData[tLastPageInstance] = StandardRouteData(
factory: tFactory,
pageData: pageData,
);
}

_updatePages();

return tFuture;
Expand Down
39 changes: 39 additions & 0 deletions packages/patapata_core/test/standard_app_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,45 @@ void main() {
tApp.dispose();
});

testWidgets(
"Standard Page PageData Test. If you have a pageKey for a StandardPage that ignores pageData.",
(WidgetTester tester) async {
final App tApp = createApp(
appWidget: StandardCupertinoApp(
onGenerateTitle: (context) => 'Test Title',
pages: [
StandardPageFactory<TestPageA, void>(
create: (data) => TestPageA(),
),
StandardPageFactory<TestPageC, TestPageData>(
pageKey: (_) => const ValueKey('TestPageC'),
create: (data) => TestPageC(),
),
],
),
);

tApp.run();

await tApp.runProcess(() async {
await tester.pumpAndSettle();

await tester.tap(find.byKey(const ValueKey(kGoPageDataButton)));

await tester.pumpAndSettle();

expect(find.text('Test Link Data is 10'), findsOneWidget);

await tester.tap(find.byKey(const ValueKey(kGoPageDataButton)));

await tester.pumpAndSettle();

expect(find.text('Test Link Data is 20'), findsOneWidget);
});

tApp.dispose();
});

testWidgets("Standard Page PageData Test. pageDataWhenNull",
(WidgetTester tester) async {
final App tApp = createApp(
Expand Down

0 comments on commit b0dd00b

Please sign in to comment.