Skip to content

Instantly share code, notes, and snippets.

@eximius313
Last active January 19, 2023 23:19
Show Gist options
  • Save eximius313/f34bbf73b9abae0d86bf222d1373ca05 to your computer and use it in GitHub Desktop.
Save eximius313/f34bbf73b9abae0d86bf222d1373ca05 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MainApp());
}
class MainApp extends StatelessWidget {
final RouterDelegate<AppState> _routerDelegate = MainRouterDelegate();
MainApp({super.key});
@override
Widget build(BuildContext context) =>
MaterialApp.router(
routerDelegate: _routerDelegate,
backButtonDispatcher: RootBackButtonDispatcher(),
);
}
class MainRouterDelegate extends RouterDelegate<AppState>
with ChangeNotifier, PopNavigatorRouterDelegateMixin<AppState> {
@override
final GlobalKey<NavigatorState> navigatorKey;
MainRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) => Navigator(
key: navigatorKey,
pages: const [
MaterialPage(child: InnerScreen(text: "Inner screen 1",)),
//Uncomment following line:
// MaterialPage(child: InnerScreen(text: "Inner screen 2",))
],
onPopPage: (route, result) {
print('onPopPage: ${DateTime.now()}\n');
if (!route.didPop(result)) {
return false;
}
return false;
},
);
@override
Future<void> setNewRoutePath(final AppState configuration) async {
}
}
class AppState extends ChangeNotifier {
}
class InnerScreen extends StatelessWidget {
const InnerScreen({super.key, required this.text});
final String text;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: [
Text(text),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment