-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State Restoration support for Veggie Seasons app (#433)
- Loading branch information
1 parent
d30bfd5
commit ed15031
Showing
138 changed files
with
817 additions
and
431 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Veggie Seasons | ||
|
||
An iOS app that shows which fruits and vegetables are currently in season. It | ||
showcases Flutter's Cupertino package. | ||
|
||
[Available now in the App Store!](https://itunes.apple.com/is/app/veggie-seasons/id1450855435) | ||
|
||
**NOTE:** While Flutter supports many platforms, this application is designed | ||
specifically for iOS. It's not intended to be run on Android, web, or desktop. | ||
|
||
## Goals | ||
|
||
* Show how to build an interface that iOS users will feel right at home | ||
with. | ||
* Show how Flutter's Cupertino widgets work together. | ||
|
||
## The important bits | ||
|
||
### `/screens/*` | ||
|
||
These are the screens presented in the app, roughly analogous to | ||
UIViewControllers. `HomeScreen` is the root, and the others are shown | ||
as the user navigates. | ||
|
||
### `/widgets/search_bar.dart` | ||
|
||
An example of how to construct an Cupertino-style search bar. The | ||
Flutter team [is working on an official widget](https://github.com/flutter/flutter/issues/9784) | ||
for this. Once that effort is complete, developers will not need to roll | ||
their own search bars, so to speak. | ||
|
||
## Questions/issues | ||
|
||
If you have a general question about any of the techniques you see in | ||
the sample, the best places to go are: | ||
|
||
* [The FlutterDev Google Group](https://groups.google.com/forum/#!forum/flutter-dev) | ||
* [The Flutter Gitter channel](https://gitter.im/flutter/flutter) | ||
* [StackOverflow](https://stackoverflow.com/questions/tagged/flutter) | ||
|
||
If you run into an issue with the sample itself, please file an issue | ||
in the [main Flutter repo](https://github.com/flutter/flutter/issues). |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome; | ||
import 'package:provider/provider.dart'; | ||
import 'package:veggieseasons/data/app_state.dart'; | ||
import 'package:veggieseasons/data/preferences.dart'; | ||
import 'package:veggieseasons/screens/home.dart'; | ||
|
||
void main() { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
SystemChrome.setPreferredOrientations([ | ||
DeviceOrientation.portraitUp, | ||
DeviceOrientation.portraitDown, | ||
]); | ||
|
||
runApp( | ||
RootRestorationScope( | ||
restorationId: 'root', | ||
child: VeggieApp(), | ||
), | ||
); | ||
} | ||
|
||
class VeggieApp extends StatefulWidget { | ||
@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( | ||
debugShowCheckedModeBanner: false, | ||
home: HomeScreen(restorationId: 'home'), | ||
restorationScopeId: 'app', | ||
), | ||
); | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/widgets.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'; | ||
|
||
class HomeScreen extends StatelessWidget { | ||
HomeScreen({Key key, this.restorationId}) : super(key: key); | ||
|
||
final String restorationId; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return RestorationScope( | ||
restorationId: restorationId, | ||
child: CupertinoTabScaffold( | ||
restorationId: 'scaffold', | ||
tabBar: CupertinoTabBar(items: [ | ||
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 ListScreen(restorationId: 'list'); | ||
} else if (index == 1) { | ||
return FavoritesScreen(restorationId: 'favorites'); | ||
} else if (index == 2) { | ||
return SearchScreen(restorationId: 'search'); | ||
} else { | ||
return SettingsScreen(restorationId: 'settings'); | ||
} | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.