|
import 'package:flutter/material.dart'; |
|
|
|
void main() { |
|
runApp(MyApp(),); |
|
} |
|
|
|
class MyApp extends StatelessWidget { |
|
MyApp({super.key}) : _routerDelegate = AppRouterDelegate(); |
|
final RouterDelegate<AppState> _routerDelegate; |
|
|
|
@override |
|
Widget build(BuildContext context) => |
|
MaterialApp.router( |
|
title: 'Test', |
|
routerDelegate: _routerDelegate, |
|
routeInformationParser: AppRouteInformationParser(), |
|
backButtonDispatcher: RootBackButtonDispatcher(), |
|
); |
|
} |
|
|
|
class AppRouteInformationParser extends RouteInformationParser<AppState> { |
|
|
|
@override |
|
Future<AppState> parseRouteInformation(RouteInformation routeInformation) { |
|
print("#parseRouteInformation. Location is: ${routeInformation.location}"); |
|
final AppState configuration = AppState(); |
|
if(routeInformation.location == '/') { |
|
configuration.home = null; |
|
} else if(routeInformation.location == '/true') { |
|
configuration.home = true; |
|
} else { |
|
configuration.home = false; |
|
} |
|
return Future.value(configuration); |
|
} |
|
|
|
@override |
|
RouteInformation? restoreRouteInformation(AppState configuration) { |
|
print("#restoreRouteInformation. Home is: ${configuration.home}"); |
|
return RouteInformation( |
|
location: configuration.home == null ? "/" : "/${configuration.home}", |
|
); |
|
} |
|
} |
|
|
|
class AppRouterDelegate extends RouterDelegate<AppState> with ChangeNotifier, PopNavigatorRouterDelegateMixin<AppState> { |
|
|
|
@override |
|
final GlobalKey<NavigatorState> navigatorKey; |
|
|
|
AppRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>(); |
|
|
|
@override |
|
Widget build(final BuildContext context) { |
|
print("AppRouterDelegate#build"); |
|
return Navigator( |
|
key: navigatorKey, |
|
pages: [ |
|
const MaterialPage(child: SomePage("Empty")), |
|
if(_configuration.home ?? false) |
|
const MaterialPage(child: SomePage("Home")), |
|
], |
|
onPopPage: (route, result) { |
|
if (!route.didPop(result)) { |
|
return false; |
|
} |
|
return true; |
|
}, |
|
); |
|
} |
|
|
|
AppState _configuration = AppState(); |
|
@override |
|
AppState get currentConfiguration { |
|
print("#currentConfiguration. Home is: ${_configuration.home}"); |
|
return _configuration; |
|
} |
|
|
|
@override |
|
Future<void> setNewRoutePath(final AppState configuration) async { |
|
print("#setNewRoutePath. Home is: ${configuration.home}"); |
|
_configuration = configuration; |
|
} |
|
} |
|
|
|
class SomePage extends StatelessWidget { |
|
const SomePage(this.text, {super.key}); |
|
final String text; |
|
@override |
|
Widget build(BuildContext context) { |
|
return Text(text); |
|
} |
|
} |
|
class AppState extends ChangeNotifier { |
|
bool? home = false; |
|
} |