Unverified Commit 97fd3ce9 authored by xubaolin's avatar xubaolin Committed by GitHub

fix a BackButtonListener bug (#80355)

parent e7b7ebc0
......@@ -1052,6 +1052,7 @@ class _BackButtonListenerState extends State<BackButtonListener> {
if (oldWidget.onBackButtonPressed != widget.onBackButtonPressed) {
dispatcher?.removeCallback(oldWidget.onBackButtonPressed);
dispatcher?.addCallback(widget.onBackButtonPressed);
dispatcher?.takePriority();
}
super.didUpdateWidget(oldWidget);
}
......
......@@ -1032,6 +1032,64 @@ testWidgets('ChildBackButtonDispatcher take priority recursively', (WidgetTester
await tester.pump();
expect(find.text('popped inner1'), findsOneWidget);
});
testWidgets('`didUpdateWidget` test', (WidgetTester tester) async {
final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider();
provider.value = const RouteInformation(
location: 'initial',
);
final BackButtonDispatcher outerDispatcher = RootBackButtonDispatcher();
late StateSetter setState;
String location = 'first callback';
final SimpleRouterDelegate routerDelegate = SimpleRouterDelegate()
..builder = (BuildContext context, RouteInformation? information) {
// Creates the sub-router.
return Column(
children: <Widget>[
Text(information!.location!),
StatefulBuilder(
builder: (BuildContext context, StateSetter setter) {
setState = setter;
return BackButtonListener(
child: Container(),
onBackButtonPressed: () {
provider.value = RouteInformation(
location: location,
);
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: routerDelegate
)
));
// Only update BackButtonListener widget.
setState((){
location = 'second callback';
});
await tester.pump();
await outerDispatcher.invokeCallback(SynchronousFuture<bool>(false));
await tester.pump();
expect(find.text('second callback'), findsOneWidget);
});
}
Widget buildBoilerPlate(Widget child) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment