1
0
mirror of https://github.com/flutter/samples.git synced 2026-07-14 04:51:29 +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:
Miguel Beltran
2023-02-24 08:18:03 +01:00
committed by GitHub
parent d53a8ee93f
commit 8c06626190
9 changed files with 281 additions and 77 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
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';
@@ -240,19 +241,6 @@ class DetailsScreen extends StatefulWidget {
const DetailsScreen({this.id, this.restorationId, super.key});
static String show(NavigatorState navigator, int veggieId) {
return navigator.restorablePush<void>(_routeBuilder, arguments: veggieId);
}
static Route<void> _routeBuilder(BuildContext context, Object? arguments) {
final veggieId = arguments as int?;
return CupertinoPageRoute(
builder: (context) =>
DetailsScreen(id: veggieId, restorationId: 'details'),
fullscreenDialog: true,
);
}
@override
State<DetailsScreen> createState() => _DetailsScreenState();
}
@@ -295,7 +283,7 @@ class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
left: 16,
child: SafeArea(
child: CloseButton(() {
Navigator.of(context).pop();
context.pop();
}),
),
),

View File

@@ -3,52 +3,62 @@
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:veggieseasons/screens/favorites.dart';
import 'package:veggieseasons/screens/list.dart';
import 'package:veggieseasons/screens/search.dart';
import 'package:veggieseasons/screens/settings.dart';
import 'package:go_router/go_router.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key, this.restorationId});
const HomeScreen({
super.key,
this.restorationId,
required this.child,
required this.onTap,
});
final String? restorationId;
final Widget child;
final void Function(int) onTap;
@override
Widget build(BuildContext context) {
final index = _getSelectedIndex(GoRouter.of(context).location);
return RestorationScope(
restorationId: restorationId,
child: CupertinoTabScaffold(
restorationId: 'scaffold',
tabBar: CupertinoTabBar(items: const [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.book),
label: 'My Garden',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.settings),
label: 'Settings',
),
]),
tabBuilder: (context, index) {
if (index == 0) {
return const ListScreen(restorationId: 'list');
} else if (index == 1) {
return const FavoritesScreen(restorationId: 'favorites');
} else if (index == 2) {
return const SearchScreen(restorationId: 'search');
} else {
return const SettingsScreen(restorationId: 'settings');
}
},
child: CupertinoPageScaffold(
child: Column(
children: [
Expanded(child: child),
CupertinoTabBar(
currentIndex: index,
items: const [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.book),
label: 'My Garden',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.settings),
label: 'Settings',
),
],
onTap: onTap,
),
],
),
),
);
}
int _getSelectedIndex(String location) {
if (location.startsWith('/list')) return 0;
if (location.startsWith('/favorites')) return 1;
if (location.startsWith('/search')) return 2;
if (location.startsWith('/settings')) return 3;
return 0;
}
}

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:veggieseasons/data/preferences.dart';
import 'package:veggieseasons/data/veggie.dart';
@@ -15,14 +16,10 @@ class VeggieCategorySettingsScreen extends StatelessWidget {
final String? restorationId;
static String show(NavigatorState navigator) {
return navigator.restorablePush(_routeBuilder);
}
static Route<void> _routeBuilder(BuildContext context, Object? argument) {
return CupertinoPageRoute(
builder: (context) =>
const VeggieCategorySettingsScreen(restorationId: 'category'),
static Page<void> pageBuilder(BuildContext context) {
return const CupertinoPage(
restorationId: 'router.categories',
child: VeggieCategorySettingsScreen(restorationId: 'category'),
title: 'Preferred Categories',
);
}
@@ -99,14 +96,10 @@ class CalorieSettingsScreen extends StatelessWidget {
static const min = 2600;
static const step = 200;
static String show(NavigatorState navigator) {
return navigator.restorablePush(_routeBuilder);
}
static Route<void> _routeBuilder(BuildContext context, Object? argument) {
return CupertinoPageRoute<void>(
builder: (context) =>
const CalorieSettingsScreen(restorationId: 'calorie'),
static Page<void> pageBuilder(BuildContext context) {
return const CupertinoPage<void>(
restorationId: 'router.calorie',
child: CalorieSettingsScreen(restorationId: 'calorie'),
title: 'Calorie Target',
);
}
@@ -198,7 +191,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
},
),
onPress: () {
CalorieSettingsScreen.show(Navigator.of(context));
context.go('/settings/calories');
},
);
}
@@ -213,7 +206,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
content: const SettingsNavigationIndicator(),
onPress: () {
VeggieCategorySettingsScreen.show(Navigator.of(context));
context.go('/settings/categories');
},
);
}
@@ -242,13 +235,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
onPressed: () async {
await prefs.restoreDefaults();
if (!mounted) return;
Navigator.pop(context);
context.pop();
},
),
CupertinoDialogAction(
isDefaultAction: true,
child: const Text('No'),
onPressed: () => Navigator.pop(context),
onPressed: () => context.pop(),
)
],
),