Skip to content

Cleaning up Veggie Seasons #2416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions veggieseasons/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
include: package:analysis_defaults/flutter.yaml

linter:
rules:
- prefer_relative_imports
4 changes: 2 additions & 2 deletions veggieseasons/lib/data/app_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:veggieseasons/data/local_veggie_provider.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'local_veggie_provider.dart';
import 'veggie.dart';

class AppState extends ChangeNotifier {
final List<Veggie> _veggies;
Expand Down
2 changes: 1 addition & 1 deletion veggieseasons/lib/data/local_veggie_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'veggie.dart';

class LocalVeggieProvider {
static List<Veggie> veggies = [
Expand Down
2 changes: 1 addition & 1 deletion veggieseasons/lib/data/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'veggie.dart';

/// A model class that mirrors the options in [SettingsScreen] and stores data
/// in shared preferences.
Expand Down
19 changes: 9 additions & 10 deletions veggieseasons/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ 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: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 '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();
Expand Down Expand Up @@ -136,7 +136,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
GoRoute(
path: '/list',
pageBuilder: (context, state) {
return FadeTransitionPage(
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.list',
child: const ListScreen(restorationId: 'list'),
Expand All @@ -149,7 +149,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
GoRoute(
path: '/favorites',
pageBuilder: (context, state) {
return FadeTransitionPage(
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.favorites',
child: const FavoritesScreen(restorationId: 'favorites'),
Expand All @@ -162,7 +162,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
GoRoute(
path: '/search',
pageBuilder: (context, state) {
return FadeTransitionPage(
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.search',
child: const SearchScreen(restorationId: 'search'),
Expand All @@ -175,7 +175,7 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
GoRoute(
path: '/settings',
pageBuilder: (context, state) {
return FadeTransitionPage(
return VeggieSeasonsPage(
key: state.pageKey,
restorationId: 'route.settings',
child: const SettingsScreen(restorationId: 'settings'),
Expand Down Expand Up @@ -217,7 +217,6 @@ class _VeggieAppState extends State<VeggieApp> with RestorationMixin {
final veggieId = int.parse(state.pathParameters['id']!);
return CupertinoPage(
restorationId: 'route.details',
fullscreenDialog: true,
child: DetailsScreen(
id: veggieId,
restorationId: 'details',
Expand Down
162 changes: 65 additions & 97 deletions veggieseasons/lib/screens/details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
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';
import 'package:veggieseasons/data/veggie.dart';
import 'package:veggieseasons/styles.dart';
import 'package:veggieseasons/widgets/close_button.dart';
import 'package:veggieseasons/widgets/trivia.dart';
import '../data/app_state.dart';
import '../data/preferences.dart';
import '../data/veggie.dart';
import '../styles.dart';
import '../widgets/detail_buttons.dart';

class ServingInfoChart extends StatelessWidget {
const ServingInfoChart(this.veggie, this.prefs, {super.key});
Expand Down Expand Up @@ -44,23 +43,7 @@ class ServingInfoChart extends StatelessWidget {
return Column(
children: [
const SizedBox(height: 16),
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(
left: 9,
bottom: 4,
),
child: Text(
'Serving info',
style: CupertinoTheme.of(context).textTheme.textStyle,
),
),
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Styles.servingInfoBorderColor),
),
padding: const EdgeInsets.all(8),
child: Column(
children: [
Expand Down Expand Up @@ -212,61 +195,23 @@ class InfoView extends StatelessWidget {
style: CupertinoTheme.of(context).textTheme.textStyle,
),
ServingInfoChart(veggie, prefs),
const SizedBox(height: 24),
Row(
mainAxisSize: MainAxisSize.min,
children: [
CupertinoSwitch(
value: veggie.isFavorite,
onChanged: (value) {
appState.setFavorite(id, value);
},
),
const SizedBox(width: 8),
Text(
'Save to Garden',
style: CupertinoTheme.of(context).textTheme.textStyle,
),
],
),
],
),
);
}
}

class DetailsScreen extends StatefulWidget {
class DetailsScreen extends StatelessWidget {
final int? id;
final String? restorationId;

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

@override
State<DetailsScreen> createState() => _DetailsScreenState();
}

class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
final RestorableInt _selectedViewIndex = RestorableInt(0);

@override
String? get restorationId => widget.restorationId;

@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_selectedViewIndex, 'tab');
}

@override
void dispose() {
_selectedViewIndex.dispose();
super.dispose();
}

Widget _buildHeader(BuildContext context, AppState model) {
final veggie = model.getVeggie(widget.id);
final veggie = model.getVeggie(id);

return SizedBox(
height: 150,
height: 240,
child: Stack(
children: [
Positioned(
Expand All @@ -287,6 +232,48 @@ class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
}),
),
),
Positioned(
top: 16,
right: 16,
child: SafeArea(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
ShareButton(
() {
showCupertinoModalPopup<void>(
context: context,
builder: (context) {
return CupertinoActionSheet(
title: Text('Share ${veggie.name}'),
message: Text(veggie.shortDescription),
actions: [
CupertinoActionSheetAction(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
),
],
);
},
);
},
),
const SizedBox(width: 8),
Builder(builder: (context) {
final appState = Provider.of<AppState>(context);
final veggie = appState.getVeggie(id);

return FavoriteButton(
() => appState.setFavorite(id, !veggie.isFavorite),
veggie.isFavorite,
);
}),
],
),
),
),
],
),
);
Expand All @@ -296,41 +283,22 @@ class _DetailsScreenState extends State<DetailsScreen> with RestorationMixin {
Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context);

return UnmanagedRestorationScope(
bucket: bucket,
child: CupertinoPageScaffold(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ListView(
restorationId: 'list',
children: [
_buildHeader(context, appState),
const SizedBox(height: 20),
CupertinoSegmentedControl<int>(
children: const {
0: Text(
'Facts & Info',
),
1: Text(
'Trivia',
)
},
groupValue: _selectedViewIndex.value,
onValueChanged: (value) {
setState(() => _selectedViewIndex.value = value);
},
),
_selectedViewIndex.value == 0
? InfoView(widget.id)
: TriviaView(id: widget.id, restorationId: 'trivia'),
],
),
return CupertinoPageScaffold(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ListView(
restorationId: 'list',
children: [
_buildHeader(context, appState),
const SizedBox(height: 20),
InfoView(id),
],
),
],
),
),
],
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions veggieseasons/lib/screens/favorites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:veggieseasons/data/app_state.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'package:veggieseasons/widgets/veggie_headline.dart';
import '../data/app_state.dart';
import '../data/veggie.dart';
import '../widgets/veggie_headline.dart';

class FavoritesScreen extends StatelessWidget {
const FavoritesScreen({this.restorationId, super.key});
Expand Down
22 changes: 18 additions & 4 deletions veggieseasons/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';

const _bottomNavigationBarItemIconPadding = EdgeInsets.only(top: 4.0);

class HomeScreen extends StatelessWidget {
const HomeScreen({
super.key,
Expand All @@ -30,19 +32,31 @@ class HomeScreen extends StatelessWidget {
currentIndex: index,
items: const [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
icon: Padding(
padding: _bottomNavigationBarItemIconPadding,
child: Icon(CupertinoIcons.home),
),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.book),
icon: Padding(
padding: _bottomNavigationBarItemIconPadding,
child: Icon(CupertinoIcons.book),
),
label: 'My Garden',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
icon: Padding(
padding: _bottomNavigationBarItemIconPadding,
child: Icon(CupertinoIcons.search),
),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.settings),
icon: Padding(
padding: _bottomNavigationBarItemIconPadding,
child: Icon(CupertinoIcons.settings),
),
label: 'Settings',
),
],
Expand Down
Loading
Loading