mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Migrate veggieseasons to go_router (#1544)
* add go_router * wip migration to go_router * small fixes * home screen cleanup * remove unused * small fixes * details should be fullscreen dialog * remove comment * fix navigation outside the shell by using the correct navigation keys * add restoration id to all pages * test passing, but parts are commented out, wip * uncommented more test code * Add TODOs * fix lint issues * fix tests * use FadeTransitionPage * remove unnecessary builders * FadeTransitionPage same as CustomTransitionPage * add comments regarding relative routes * add missing pageKey * add missing const --------- Co-authored-by: Brett Morgan <brettmorgan@google.com>
This commit is contained in:
@@ -7,13 +7,21 @@ import 'dart:io';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome;
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:veggieseasons/data/app_state.dart';
|
||||
import 'package:veggieseasons/data/preferences.dart';
|
||||
import 'package:veggieseasons/screens/home.dart';
|
||||
import 'package:veggieseasons/styles.dart';
|
||||
import 'package:veggieseasons/widgets/fade_transition_page.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
import 'screens/details.dart';
|
||||
import 'screens/favorites.dart';
|
||||
import 'screens/list.dart';
|
||||
import 'screens/search.dart';
|
||||
import 'screens/settings.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
SystemChrome.setPreferredOrientations([
|
||||
@@ -48,6 +56,9 @@ void setupWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
final _rootNavigatorKey = GlobalKey<NavigatorState>();
|
||||
final _shellNavigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
class VeggieApp extends StatefulWidget {
|
||||
const VeggieApp({super.key});
|
||||
|
||||
@@ -83,14 +94,138 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
|
||||
create: (_) => Preferences()..load(),
|
||||
),
|
||||
],
|
||||
child: CupertinoApp(
|
||||
child: CupertinoApp.router(
|
||||
theme: Styles.veggieThemeData,
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: const HomeScreen(restorationId: 'home'),
|
||||
restorationScopeId: 'app',
|
||||
routerConfig: GoRouter(
|
||||
navigatorKey: _rootNavigatorKey,
|
||||
restorationScopeId: 'router',
|
||||
initialLocation: '/list',
|
||||
redirect: (context, state) {
|
||||
if (state.path == '/') {
|
||||
return '/list';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
debugLogDiagnostics: true,
|
||||
routes: [
|
||||
ShellRoute(
|
||||
navigatorKey: _shellNavigatorKey,
|
||||
pageBuilder: (context, state, child) {
|
||||
return CupertinoPage(
|
||||
restorationId: 'router.shell',
|
||||
child: HomeScreen(
|
||||
restorationId: 'home',
|
||||
child: child,
|
||||
onTap: (index) {
|
||||
if (index == 0) {
|
||||
context.go('/list');
|
||||
} else if (index == 1) {
|
||||
context.go('/favorites');
|
||||
} else if (index == 2) {
|
||||
context.go('/search');
|
||||
} else {
|
||||
context.go('/settings');
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/list',
|
||||
pageBuilder: (context, state) {
|
||||
return FadeTransitionPage(
|
||||
key: state.pageKey,
|
||||
restorationId: 'route.list',
|
||||
child: const ListScreen(restorationId: 'list'),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
_buildDetailsRoute(),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: '/favorites',
|
||||
pageBuilder: (context, state) {
|
||||
return FadeTransitionPage(
|
||||
key: state.pageKey,
|
||||
restorationId: 'route.favorites',
|
||||
child: const FavoritesScreen(restorationId: 'favorites'),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
_buildDetailsRoute(),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: '/search',
|
||||
pageBuilder: (context, state) {
|
||||
return FadeTransitionPage(
|
||||
key: state.pageKey,
|
||||
restorationId: 'route.search',
|
||||
child: const SearchScreen(restorationId: 'search'),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
_buildDetailsRoute(),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: '/settings',
|
||||
pageBuilder: (context, state) {
|
||||
return FadeTransitionPage(
|
||||
key: state.pageKey,
|
||||
restorationId: 'route.settings',
|
||||
child: const SettingsScreen(restorationId: 'settings'),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
parentNavigatorKey: _rootNavigatorKey,
|
||||
path: 'categories',
|
||||
pageBuilder: (context, state) {
|
||||
return VeggieCategorySettingsScreen.pageBuilder(
|
||||
context);
|
||||
},
|
||||
),
|
||||
GoRoute(
|
||||
parentNavigatorKey: _rootNavigatorKey,
|
||||
path: 'calories',
|
||||
pageBuilder: (context, state) {
|
||||
return CalorieSettingsScreen.pageBuilder(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// GoRouter does not support relative routes,
|
||||
// see https://github.com/flutter/flutter/issues/108177
|
||||
GoRoute _buildDetailsRoute() {
|
||||
return GoRoute(
|
||||
parentNavigatorKey: _rootNavigatorKey,
|
||||
path: 'details/:id',
|
||||
pageBuilder: (context, state) {
|
||||
final veggieId = int.parse(state.params['id']!);
|
||||
return CupertinoPage(
|
||||
restorationId: 'route.details',
|
||||
fullscreenDialog: true,
|
||||
child: DetailsScreen(
|
||||
id: veggieId,
|
||||
restorationId: 'details',
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RestorableAppState extends RestorableListenable<AppState> {
|
||||
|
||||
Reference in New Issue
Block a user