You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
samples/veggieseasons/lib/main.dart

251 lines
7.4 KiB

// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io' show Platform;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome;
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:window_size/window_size.dart';
import 'data/app_state.dart';
import 'data/preferences.dart';
import 'screens/details.dart';
import 'screens/favorites.dart';
import 'screens/home.dart';
import 'screens/list.dart';
import 'screens/search.dart';
import 'screens/settings.dart';
import 'styles.dart';
import 'widgets/veggie_seasons_page.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
setupWindow();
runApp(
const RootRestorationScope(
restorationId: 'root',
child: VeggieApp(),
),
);
}
const double windowWidth = 480;
const double windowHeight = 854;
void setupWindow() {
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
setWindowTitle('Veggie Seasons');
setWindowMinSize(const Size(windowWidth, windowHeight));
setWindowMaxSize(const Size(windowWidth, windowHeight));
getCurrentScreen().then((screen) {
setWindowFrame(Rect.fromCenter(
center: screen!.frame.center,
width: windowWidth,
height: windowHeight,
));
});
}
}
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorKey = GlobalKey<NavigatorState>();
class VeggieApp extends StatefulWidget {
const VeggieApp({super.key});
@override
State<StatefulWidget> createState() => _VeggieAppState();
}
class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
final _RestorableAppState _appState = _RestorableAppState();
@override
String get restorationId => 'wrapper';
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_appState, 'state');
}
@override
void dispose() {
_appState.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(
value: _appState.value,
),
ChangeNotifierProvider(
create: (_) => Preferences()..load(),
),
],
child: CupertinoApp.router(
theme: Styles.veggieThemeData,
debugShowCheckedModeBanner: false,
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 VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.list',
child: const ListScreen(restorationId: 'list'),
);
},
routes: [
_buildDetailsRoute(),
],
),
GoRoute(
path: '/favorites',
pageBuilder: (context, state) {
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.favorites',
child: const FavoritesScreen(restorationId: 'favorites'),
);
},
routes: [
_buildDetailsRoute(),
],
),
GoRoute(
path: '/search',
pageBuilder: (context, state) {
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.search',
child: const SearchScreen(restorationId: 'search'),
);
},
routes: [
_buildDetailsRoute(),
],
),
GoRoute(
path: '/settings',
pageBuilder: (context, state) {
return VeggieSeasonsPage(
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) {
Bump go_router from 6.5.9 to 7.0.0 in /veggieseasons (#1762) Bumps [go_router](https://github.com/flutter/packages/tree/main/packages) from 6.5.9 to 7.0.0. <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/flutter/packages/commit/b01736711cc7c2034baa4254ed95566a8c70bb83"><code>b017367</code></a> [go_router] Cleans up route match API and introduces dart fix (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3819">#3819</a>)</li> <li><a href="https://github.com/flutter/packages/commit/2f95ecda81649d667871420ff8049b7f09e14bef"><code>2f95ecd</code></a> [camerax] Add <code>LifecycleOwner</code> Proxy (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3837">#3837</a>)</li> <li><a href="https://github.com/flutter/packages/commit/815cfca847dfdb340fd947538f0ba234f81f61b6"><code>815cfca</code></a> [file_selector] Add getDirectoryPaths implementation for Windows (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3704">#3704</a>)</li> <li><a href="https://github.com/flutter/packages/commit/6dc28c9c3f0f5098ce62dd5c87cf77ea0dab08d4"><code>6dc28c9</code></a> [various] Update Android example min SDKs (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3847">#3847</a>)</li> <li><a href="https://github.com/flutter/packages/commit/1a90429ac139d4471543925ecc67a3bf28592df2"><code>1a90429</code></a> [camerax] Implement Image Streaming (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3454">#3454</a>)</li> <li><a href="https://github.com/flutter/packages/commit/124e330a2bdd45f8b4a98089aff717e34a14b8f8"><code>124e330</code></a> [various] update agp and gradle for all examples in packages (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3822">#3822</a>)</li> <li><a href="https://github.com/flutter/packages/commit/c71747df2c11c54ccc03dc4c3b9b470153ede43c"><code>c71747d</code></a> Update xcode to 14c18 (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3774">#3774</a>)</li> <li><a href="https://github.com/flutter/packages/commit/3dc59f914df812da4790b1a6b36d7ec6b0df2d7c"><code>3dc59f9</code></a> [camera_android] Add NV21 as an image stream format <a href="https://github.com/flutter/packages/tree/main/packages/issues/3277">#3277</a> (<a href="https://github.com/flutter/packages/tree/main/packages/issues/3639">#3639</a>)</li> <li>See full diff in <a href="https://github.com/flutter/packages/commits/go_router-v7.0.0/packages">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=go_router&package-manager=pub&previous-version=6.5.9&new-version=7.0.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Brett Morgan <brettmorgan@google.com>
2 years ago
final veggieId = int.parse(state.pathParameters['id']!);
return CupertinoPage(
restorationId: 'route.details',
child: DetailsScreen(
id: veggieId,
restorationId: 'details',
),
);
},
);
}
}
class _RestorableAppState extends RestorableListenable<AppState> {
@override
AppState createDefaultValue() {
return AppState();
}
@override
AppState fromPrimitives(Object? data) {
final appState = AppState();
final favorites = (data as List<dynamic>).cast<int>();
for (var id in favorites) {
appState.setFavorite(id, true);
}
return appState;
}
@override
Object toPrimitives() {
return value.favoriteVeggies.map((veggie) => veggie.id).toList();
}
}