Skip to content

Instantly share code, notes, and snippets.

@chunhtai
Created April 2, 2021 18:14
Show Gist options
  • Save chunhtai/b666b3192e2356d315f9cb434d780811 to your computer and use it in GitHub Desktop.
Save chunhtai/b666b3192e2356d315f9cb434d780811 to your computer and use it in GitHub Desktop.
testWidgets('BackButtonListener takes priority over root back dispatcher', (WidgetTester tester) async {
final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider();
provider.value = const RouteInformation(
location: 'initial',
);
final BackButtonDispatcher outerDispatcher = RootBackButtonDispatcher();
await tester.pumpWidget(buildBoilerPlate(
Router<RouteInformation>(
backButtonDispatcher: outerDispatcher,
routeInformationProvider: provider,
routeInformationParser: SimpleRouteInformationParser(),
routerDelegate: SimpleRouterDelegate(
builder: (BuildContext context, RouteInformation? information) {
// Creates the sub-router.
return Column(
children: <Widget>[
Text(information!.location!),
BackButtonListener(
child: Container(),
onBackPressed: () {
provider.value = const RouteInformation(
location: 'popped inner1',
);
return SynchronousFuture<bool>(true);
},
),
],
);
},
onPopRoute: () {
provider.value = const RouteInformation(
location: 'popped outter',
);
return SynchronousFuture<bool>(true);
}
),
)
));
await tester.pumpWidget(buildBoilerPlate(
Router<RouteInformation>(
backButtonDispatcher: outerDispatcher,
routeInformationProvider: provider,
routeInformationParser: SimpleRouteInformationParser(),
routerDelegate: SimpleRouterDelegate(
builder: (BuildContext context, RouteInformation? information) {
// Creates the sub-router.
return Column(
children: <Widget>[
Text(information!.location!),
BackButtonListener(
child: Container(),
onBackPressed: () {
provider.value = const RouteInformation(
location: 'a different call back',
);
return SynchronousFuture<bool>(true);
},
),
],
);
},
onPopRoute: () {
provider.value = const RouteInformation(
location: 'popped outter',
);
return SynchronousFuture<bool>(true);
}
),
)
));
// This should trigger didUpdateWidget
await tester.pump();
await tester.pumpWidget(buildBoilerPlate(
Router<RouteInformation>(
backButtonDispatcher: outerDispatcher,
routeInformationProvider: provider,
routeInformationParser: SimpleRouteInformationParser(),
routerDelegate: SimpleRouterDelegate(
builder: (BuildContext context, RouteInformation? information) {
// Creates the sub-router.
return Column(
children: <Widget>[
Text(information!.location!),
Text('a different widget'),
],
);
},
onPopRoute: () {
provider.value = const RouteInformation(
location: 'popped outter',
);
return SynchronousFuture<bool>(true);
}
),
)
));
// This should trigger dispose
await tester.pump();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment