When i display a modal dialog in Flutter Web, i would like to make the back button close the current dialog instead of go back to the previous route. After some search, i found PopScope but i can't make it working: the method onPopInvokedWithResult is called only when i click "Close" button in the modal, when i click "Previous" the browser goes back to page one.
Of course i tried setting canPop to true/false: the behavior is the same. I tried with WillPopScope or onPopInvoked (both are deprecated) but it didn't work.
I use go_router in version 14.6.3
I join a minimal reproduction case:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const Page1(),
),
GoRoute(
path: '/page2',
builder: (context, state) => const Page2(),
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class Page1 extends StatelessWidget {
const Page1({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Page 1')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/page2');
},
child: const Text('goto Page 2'),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Page 2')),
body: Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return PopScope(
canPop: true,
onPopInvokedWithResult: (didPop, result) {
//Section called only after i click Close button
print('onPopInvokedWithResult');
print('$didPop');
//perform some Navigator.of(context).pop();
},
child: AlertDialog(
title: const Text('Dialog'),
content: const Text('Dialog content'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
print('Close button clicked');
},
child: const Text('Close'),
),
],
),
);
},
);
},
child: const Text('Open dialog'),
),
),
);
}
}