Skip to content

Commit bee21b5

Browse files
Cleaning up Veggie Seasons (#2416)
## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md
1 parent 61fed76 commit bee21b5

20 files changed

+215
-546
lines changed

veggieseasons/analysis_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
include: package:analysis_defaults/flutter.yaml
2+
3+
linter:
4+
rules:
5+
- prefer_relative_imports

veggieseasons/lib/data/app_state.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/foundation.dart';
6-
import 'package:veggieseasons/data/local_veggie_provider.dart';
7-
import 'package:veggieseasons/data/veggie.dart';
6+
import 'local_veggie_provider.dart';
7+
import 'veggie.dart';
88

99
class AppState extends ChangeNotifier {
1010
final List<Veggie> _veggies;

veggieseasons/lib/data/local_veggie_provider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/cupertino.dart';
6-
import 'package:veggieseasons/data/veggie.dart';
6+
import 'veggie.dart';
77

88
class LocalVeggieProvider {
99
static List<Veggie> veggies = [

veggieseasons/lib/data/preferences.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:async';
66

77
import 'package:flutter/cupertino.dart';
88
import 'package:shared_preferences/shared_preferences.dart';
9-
import 'package:veggieseasons/data/veggie.dart';
9+
import 'veggie.dart';
1010

1111
/// A model class that mirrors the options in [SettingsScreen] and stores data
1212
/// in shared preferences.

veggieseasons/lib/main.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import 'package:flutter/foundation.dart' show kIsWeb;
99
import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome;
1010
import 'package:go_router/go_router.dart';
1111
import 'package:provider/provider.dart';
12-
import 'package:veggieseasons/data/app_state.dart';
13-
import 'package:veggieseasons/data/preferences.dart';
14-
import 'package:veggieseasons/screens/home.dart';
15-
import 'package:veggieseasons/styles.dart';
16-
import 'package:veggieseasons/widgets/fade_transition_page.dart';
1712
import 'package:window_size/window_size.dart';
1813

14+
import 'data/app_state.dart';
15+
import 'data/preferences.dart';
1916
import 'screens/details.dart';
2017
import 'screens/favorites.dart';
18+
import 'screens/home.dart';
2119
import 'screens/list.dart';
2220
import 'screens/search.dart';
2321
import 'screens/settings.dart';
22+
import 'styles.dart';
23+
import 'widgets/veggie_seasons_page.dart';
2424

2525
void main() {
2626
WidgetsFlutterBinding.ensureInitialized();
@@ -136,7 +136,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
136136
GoRoute(
137137
path: '/list',
138138
pageBuilder: (context, state) {
139-
return FadeTransitionPage(
139+
return VeggieSeasonsPage(
140140
key: state.pageKey,
141141
restorationId: 'route.list',
142142
child: const ListScreen(restorationId: 'list'),
@@ -149,7 +149,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
149149
GoRoute(
150150
path: '/favorites',
151151
pageBuilder: (context, state) {
152-
return FadeTransitionPage(
152+
return VeggieSeasonsPage(
153153
key: state.pageKey,
154154
restorationId: 'route.favorites',
155155
child: const FavoritesScreen(restorationId: 'favorites'),
@@ -162,7 +162,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
162162
GoRoute(
163163
path: '/search',
164164
pageBuilder: (context, state) {
165-
return FadeTransitionPage(
165+
return VeggieSeasonsPage(
166166
key: state.pageKey,
167167
restorationId: 'route.search',
168168
child: const SearchScreen(restorationId: 'search'),
@@ -175,7 +175,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
175175
GoRoute(
176176
path: '/settings',
177177
pageBuilder: (context, state) {
178-
return FadeTransitionPage(
178+
return VeggieSeasonsPage(
179179
key: state.pageKey,
180180
restorationId: 'route.settings',
181181
child: const SettingsScreen(restorationId: 'settings'),
@@ -217,7 +217,6 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
217217
final veggieId = int.parse(state.pathParameters['id']!);
218218
return CupertinoPage(
219219
restorationId: 'route.details',
220-
fullscreenDialog: true,
221220
child: DetailsScreen(
222221
id: veggieId,
223222
restorationId: 'details',

veggieseasons/lib/screens/details.dart

Lines changed: 65 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import 'package:flutter/cupertino.dart';
66
import 'package:go_router/go_router.dart';
77
import 'package:provider/provider.dart';
8-
import 'package:veggieseasons/data/app_state.dart';
9-
import 'package:veggieseasons/data/preferences.dart';
10-
import 'package:veggieseasons/data/veggie.dart';
11-
import 'package:veggieseasons/styles.dart';
12-
import 'package:veggieseasons/widgets/close_button.dart';
13-
import 'package:veggieseasons/widgets/trivia.dart';
8+
import '../data/app_state.dart';
9+
import '../data/preferences.dart';
10+
import '../data/veggie.dart';
11+
import '../styles.dart';
12+
import '../widgets/detail_buttons.dart';
1413

1514
class ServingInfoChart extends StatelessWidget {
1615
const ServingInfoChart(this.veggie, this.prefs, {super.key});
@@ -44,23 +43,7 @@ class ServingInfoChart extends StatelessWidget {
4443
return Column(
4544
children: [
4645
const SizedBox(height: 16),
47-
Align(
48-
alignment: Alignment.centerLeft,
49-
child: Padding(
50-
padding: const EdgeInsets.only(
51-
left: 9,
52-
bottom: 4,
53-
),
54-
child: Text(
55-
'Serving info',
56-
style: CupertinoTheme.of(context).textTheme.textStyle,
57-
),
58-
),
59-
),
6046
Container(
61-
decoration: BoxDecoration(
62-
border: Border.all(color: Styles.servingInfoBorderColor),
63-
),
6447
padding: const EdgeInsets.all(8),
6548
child: Column(
6649
children: [
@@ -212,61 +195,23 @@ class InfoView extends StatelessWidget {
212195
style: CupertinoTheme.of(context).textTheme.textStyle,
213196
),
214197
ServingInfoChart(veggie, prefs),
215-
const SizedBox(height: 24),
216-
Row(
217-
mainAxisSize: MainAxisSize.min,
218-
children: [
219-
CupertinoSwitch(
220-
value: veggie.isFavorite,
221-
onChanged: (value) {
222-
appState.setFavorite(id, value);
223-
},
224-
),
225-
const SizedBox(width: 8),
226-
Text(
227-
'Save to Garden',
228-
style: CupertinoTheme.of(context).textTheme.textStyle,
229-
),
230-
],
231-
),
232198
],
233199
),
234200
);
235201
}
236202
}
237203

238-
class DetailsScreen extends StatefulWidget {
204+
class DetailsScreen extends StatelessWidget {
239205
final int? id;
240206
final String? restorationId;
241207

242208
const DetailsScreen({this.id, this.restorationId, super.key});
243209

244-
@override
245-
State<DetailsScreen> createState() => _DetailsScreenState();
246-
}
247-
248-
class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
249-
final RestorableInt _selectedViewIndex = RestorableInt(0);
250-
251-
@override
252-
String? get restorationId => widget.restorationId;
253-
254-
@override
255-
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
256-
registerForRestoration(_selectedViewIndex, 'tab');
257-
}
258-
259-
@override
260-
void dispose() {
261-
_selectedViewIndex.dispose();
262-
super.dispose();
263-
}
264-
265210
Widget _buildHeader(BuildContext context, AppState model) {
266-
final veggie = model.getVeggie(widget.id);
211+
final veggie = model.getVeggie(id);
267212

268213
return SizedBox(
269-
height: 150,
214+
height: 240,
270215
child: Stack(
271216
children: [
272217
Positioned(
@@ -287,6 +232,48 @@ class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
287232
}),
288233
),
289234
),
235+
Positioned(
236+
top: 16,
237+
right: 16,
238+
child: SafeArea(
239+
child: Row(
240+
mainAxisSize: MainAxisSize.min,
241+
children: [
242+
ShareButton(
243+
() {
244+
showCupertinoModalPopup<void>(
245+
context: context,
246+
builder: (context) {
247+
return CupertinoActionSheet(
248+
title: Text('Share ${veggie.name}'),
249+
message: Text(veggie.shortDescription),
250+
actions: [
251+
CupertinoActionSheetAction(
252+
onPressed: () {
253+
Navigator.pop(context);
254+
},
255+
child: const Text('OK'),
256+
),
257+
],
258+
);
259+
},
260+
);
261+
},
262+
),
263+
const SizedBox(width: 8),
264+
Builder(builder: (context) {
265+
final appState = Provider.of<AppState>(context);
266+
final veggie = appState.getVeggie(id);
267+
268+
return FavoriteButton(
269+
() => appState.setFavorite(id, !veggie.isFavorite),
270+
veggie.isFavorite,
271+
);
272+
}),
273+
],
274+
),
275+
),
276+
),
290277
],
291278
),
292279
);
@@ -296,41 +283,22 @@ class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
296283
Widget build(BuildContext context) {
297284
final appState = Provider.of<AppState>(context);
298285

299-
return UnmanagedRestorationScope(
300-
bucket: bucket,
301-
child: CupertinoPageScaffold(
302-
child: Column(
303-
crossAxisAlignment: CrossAxisAlignment.stretch,
304-
mainAxisSize: MainAxisSize.min,
305-
children: [
306-
Expanded(
307-
child: ListView(
308-
restorationId: 'list',
309-
children: [
310-
_buildHeader(context, appState),
311-
const SizedBox(height: 20),
312-
CupertinoSegmentedControl<int>(
313-
children: const {
314-
0: Text(
315-
'Facts & Info',
316-
),
317-
1: Text(
318-
'Trivia',
319-
)
320-
},
321-
groupValue: _selectedViewIndex.value,
322-
onValueChanged: (value) {
323-
setState(() => _selectedViewIndex.value = value);
324-
},
325-
),
326-
_selectedViewIndex.value == 0
327-
? InfoView(widget.id)
328-
: TriviaView(id: widget.id, restorationId: 'trivia'),
329-
],
330-
),
286+
return CupertinoPageScaffold(
287+
child: Column(
288+
crossAxisAlignment: CrossAxisAlignment.stretch,
289+
mainAxisSize: MainAxisSize.min,
290+
children: [
291+
Expanded(
292+
child: ListView(
293+
restorationId: 'list',
294+
children: [
295+
_buildHeader(context, appState),
296+
const SizedBox(height: 20),
297+
InfoView(id),
298+
],
331299
),
332-
],
333-
),
300+
),
301+
],
334302
),
335303
);
336304
}

veggieseasons/lib/screens/favorites.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import 'package:flutter/cupertino.dart';
66
import 'package:provider/provider.dart';
7-
import 'package:veggieseasons/data/app_state.dart';
8-
import 'package:veggieseasons/data/veggie.dart';
9-
import 'package:veggieseasons/widgets/veggie_headline.dart';
7+
import '../data/app_state.dart';
8+
import '../data/veggie.dart';
9+
import '../widgets/veggie_headline.dart';
1010

1111
class FavoritesScreen extends StatelessWidget {
1212
const FavoritesScreen({this.restorationId, super.key});

veggieseasons/lib/screens/home.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'package:flutter/cupertino.dart';
66
import 'package:go_router/go_router.dart';
77

8+
const _bottomNavigationBarItemIconPadding = EdgeInsets.only(top: 4.0);
9+
810
class HomeScreen extends StatelessWidget {
911
const HomeScreen({
1012
super.key,
@@ -30,19 +32,31 @@ class HomeScreen extends StatelessWidget {
3032
currentIndex: index,
3133
items: const [
3234
BottomNavigationBarItem(
33-
icon: Icon(CupertinoIcons.home),
35+
icon: Padding(
36+
padding: _bottomNavigationBarItemIconPadding,
37+
child: Icon(CupertinoIcons.home),
38+
),
3439
label: 'Home',
3540
),
3641
BottomNavigationBarItem(
37-
icon: Icon(CupertinoIcons.book),
42+
icon: Padding(
43+
padding: _bottomNavigationBarItemIconPadding,
44+
child: Icon(CupertinoIcons.book),
45+
),
3846
label: 'My Garden',
3947
),
4048
BottomNavigationBarItem(
41-
icon: Icon(CupertinoIcons.search),
49+
icon: Padding(
50+
padding: _bottomNavigationBarItemIconPadding,
51+
child: Icon(CupertinoIcons.search),
52+
),
4253
label: 'Search',
4354
),
4455
BottomNavigationBarItem(
45-
icon: Icon(CupertinoIcons.settings),
56+
icon: Padding(
57+
padding: _bottomNavigationBarItemIconPadding,
58+
child: Icon(CupertinoIcons.settings),
59+
),
4660
label: 'Settings',
4761
),
4862
],

0 commit comments

Comments
 (0)