Skip to content

Instantly share code, notes, and snippets.

@alphamikle
Created January 1, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphamikle/260f0dd3bd7012f0afc49a89d974a7d5 to your computer and use it in GitHub Desktop.
Save alphamikle/260f0dd3bd7012f0afc49a89d974a7d5 to your computer and use it in GitHub Desktop.
Example of Navigator 2.0 Pages methods bug
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
final parser = RootRouterParser();
final delegate = RootRouterDelegate();
const indexes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
int index = 0;
bool overridePages = false;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
class MyApp extends StatelessWidget {
const MyApp({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp.router(
title: 'Navigator 2.0 Example',
routeInformationParser: parser,
routerDelegate: delegate,
);
}
class RootRouterParser extends RouteInformationParser<dynamic> {
@override
Future<dynamic> parseRouteInformation(RouteInformation routeInformation) async => '';
}
class RootRouterDelegate extends RouterDelegate<dynamic> with ChangeNotifier, PopNavigatorRouterDelegateMixin<dynamic> {
RootRouterDelegate() : navigatorKey = GlobalKey();
@override
final GlobalKey<NavigatorState> navigatorKey;
List<Page> _pages = [
buildMaterialPage<void>(EmptyPage(id: 'First', index: index)),
];
@override
Widget build(BuildContext context) => Navigator(
key: navigatorKey,
pages: _pages,
onPopPage: _onPopPage,
);
void addPage() {
index++;
final String id = index >= indexes.length ? '${index + 1}' : indexes[index];
if (overridePages) {
_pages = [..._pages, buildMaterialPage<void>(EmptyPage(id: id, index: index))];
print('Add new page with overrides');
scaffoldKey.currentState.showSnackBar(const SnackBar(content: Text('Add new page with overrides')));
} else {
_pages.add(buildMaterialPage<void>(EmptyPage(id: id, index: index)));
print('Add new page without overrides');
scaffoldKey.currentState.showSnackBar(const SnackBar(content: Text('Add new page without overrides')));
}
notifyListeners();
}
bool _onPopPage(Route<dynamic> route, dynamic data) => true;
@override
Future<void> setNewRoutePath(dynamic configuration) async {}
}
class EmptyPage extends StatelessWidget {
const EmptyPage({
@required this.id,
@required this.index,
Key key,
}) : super(key: key);
final String id;
final int index;
@override
Widget build(BuildContext context) => Scaffold(
key: index == 0 ? scaffoldKey : ValueKey(id),
appBar: AppBar(title: Text('Title - $id')),
body: Center(child: Text('Empty page - $id')),
floatingActionButton: FloatingActionButton(
onPressed: delegate.addPage,
tooltip: 'Add page',
child: const Icon(Icons.add),
),
);
}
Page buildMaterialPage<T>(
Widget child, {
String name,
Object arguments,
}) =>
MaterialPage<T>(
child: child,
name: name,
arguments: arguments,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment