mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
Moved veggieseasons out of experimental (#752)
This commit is contained in:
80
veggieseasons/lib/data/app_state.dart
Normal file
80
veggieseasons/lib/data/app_state.dart
Normal file
@@ -0,0 +1,80 @@
|
||||
// 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/foundation.dart';
|
||||
import 'package:veggieseasons/data/local_veggie_provider.dart';
|
||||
import 'package:veggieseasons/data/veggie.dart';
|
||||
|
||||
class AppState extends ChangeNotifier {
|
||||
final List<Veggie> _veggies;
|
||||
|
||||
AppState() : _veggies = LocalVeggieProvider.veggies;
|
||||
|
||||
List<Veggie> get allVeggies => List<Veggie>.from(_veggies);
|
||||
|
||||
List<Veggie> get availableVeggies {
|
||||
var currentSeason = _getSeasonForDate(DateTime.now());
|
||||
return _veggies.where((v) => v.seasons.contains(currentSeason)).toList();
|
||||
}
|
||||
|
||||
List<Veggie> get favoriteVeggies =>
|
||||
_veggies.where((v) => v.isFavorite).toList();
|
||||
|
||||
List<Veggie> get unavailableVeggies {
|
||||
var currentSeason = _getSeasonForDate(DateTime.now());
|
||||
return _veggies.where((v) => !v.seasons.contains(currentSeason)).toList();
|
||||
}
|
||||
|
||||
Veggie getVeggie(int id) => _veggies.singleWhere((v) => v.id == id);
|
||||
|
||||
List<Veggie> searchVeggies(String terms) => _veggies
|
||||
.where((v) => v.name.toLowerCase().contains(terms.toLowerCase()))
|
||||
.toList();
|
||||
|
||||
void setFavorite(int id, bool isFavorite) {
|
||||
var veggie = getVeggie(id);
|
||||
veggie.isFavorite = isFavorite;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Used in tests to set the season independent of the current date.
|
||||
static Season debugCurrentSeason;
|
||||
|
||||
static Season _getSeasonForDate(DateTime date) {
|
||||
if (debugCurrentSeason != null) {
|
||||
return debugCurrentSeason;
|
||||
}
|
||||
|
||||
// Technically the start and end dates of seasons can vary by a day or so,
|
||||
// but this is close enough for produce.
|
||||
switch (date.month) {
|
||||
case 1:
|
||||
return Season.winter;
|
||||
case 2:
|
||||
return Season.winter;
|
||||
case 3:
|
||||
return date.day < 21 ? Season.winter : Season.spring;
|
||||
case 4:
|
||||
return Season.spring;
|
||||
case 5:
|
||||
return Season.spring;
|
||||
case 6:
|
||||
return date.day < 21 ? Season.spring : Season.summer;
|
||||
case 7:
|
||||
return Season.summer;
|
||||
case 8:
|
||||
return Season.summer;
|
||||
case 9:
|
||||
return date.day < 22 ? Season.autumn : Season.winter;
|
||||
case 10:
|
||||
return Season.autumn;
|
||||
case 11:
|
||||
return Season.autumn;
|
||||
case 12:
|
||||
return date.day < 22 ? Season.autumn : Season.winter;
|
||||
default:
|
||||
throw ArgumentError('Can\'t return a season for month #${date.month}.');
|
||||
}
|
||||
}
|
||||
}
|
||||
1398
veggieseasons/lib/data/local_veggie_provider.dart
Normal file
1398
veggieseasons/lib/data/local_veggie_provider.dart
Normal file
File diff suppressed because it is too large
Load Diff
90
veggieseasons/lib/data/preferences.dart
Normal file
90
veggieseasons/lib/data/preferences.dart
Normal file
@@ -0,0 +1,90 @@
|
||||
// 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:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:veggieseasons/data/veggie.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// A model class that mirrors the options in [SettingsScreen] and stores data
|
||||
/// in shared preferences.
|
||||
class Preferences extends ChangeNotifier {
|
||||
// Keys to use with shared preferences.
|
||||
static const _caloriesKey = 'calories';
|
||||
static const _preferredCategoriesKey = 'preferredCategories';
|
||||
|
||||
// Indicates whether a call to [_loadFromSharedPrefs] is in progress;
|
||||
Future<void> _loading;
|
||||
|
||||
int _desiredCalories = 2000;
|
||||
|
||||
final Set<VeggieCategory> _preferredCategories = <VeggieCategory>{};
|
||||
|
||||
Future<int> get desiredCalories async {
|
||||
await _loading;
|
||||
return _desiredCalories;
|
||||
}
|
||||
|
||||
Future<Set<VeggieCategory>> get preferredCategories async {
|
||||
await _loading;
|
||||
return Set.from(_preferredCategories);
|
||||
}
|
||||
|
||||
Future<void> addPreferredCategory(VeggieCategory category) async {
|
||||
_preferredCategories.add(category);
|
||||
await _saveToSharedPrefs();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> removePreferredCategory(VeggieCategory category) async {
|
||||
_preferredCategories.remove(category);
|
||||
await _saveToSharedPrefs();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setDesiredCalories(int calories) async {
|
||||
_desiredCalories = calories;
|
||||
await _saveToSharedPrefs();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> restoreDefaults() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.clear();
|
||||
load();
|
||||
}
|
||||
|
||||
void load() {
|
||||
_loading = _loadFromSharedPrefs();
|
||||
}
|
||||
|
||||
Future<void> _saveToSharedPrefs() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_caloriesKey, _desiredCalories);
|
||||
|
||||
// Store preferred categories as a comma-separated string containing their
|
||||
// indices.
|
||||
await prefs.setString(_preferredCategoriesKey,
|
||||
_preferredCategories.map((c) => c.index.toString()).join(','));
|
||||
}
|
||||
|
||||
Future<void> _loadFromSharedPrefs() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_desiredCalories = prefs.getInt(_caloriesKey) ?? 2000;
|
||||
_preferredCategories.clear();
|
||||
final names = prefs.getString(_preferredCategoriesKey);
|
||||
|
||||
if (names != null && names.isNotEmpty) {
|
||||
for (final name in names.split(',')) {
|
||||
final index = int.tryParse(name) ?? -1;
|
||||
if (VeggieCategory.values[index] != null) {
|
||||
_preferredCategories.add(VeggieCategory.values[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
131
veggieseasons/lib/data/veggie.dart
Normal file
131
veggieseasons/lib/data/veggie.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
// 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:meta/meta.dart';
|
||||
|
||||
enum VeggieCategory {
|
||||
allium,
|
||||
berry,
|
||||
citrus,
|
||||
cruciferous,
|
||||
fern,
|
||||
flower,
|
||||
fruit,
|
||||
fungus,
|
||||
gourd,
|
||||
leafy,
|
||||
legume,
|
||||
melon,
|
||||
root,
|
||||
stealthFruit,
|
||||
stoneFruit,
|
||||
tropical,
|
||||
tuber,
|
||||
vegetable,
|
||||
}
|
||||
|
||||
enum Season {
|
||||
winter,
|
||||
spring,
|
||||
summer,
|
||||
autumn,
|
||||
}
|
||||
|
||||
class Trivia {
|
||||
final String question;
|
||||
final List<String> answers;
|
||||
final int correctAnswerIndex;
|
||||
|
||||
const Trivia(this.question, this.answers, this.correctAnswerIndex);
|
||||
}
|
||||
|
||||
const Map<VeggieCategory, String> veggieCategoryNames = {
|
||||
VeggieCategory.allium: 'Allium',
|
||||
VeggieCategory.berry: 'Berry',
|
||||
VeggieCategory.citrus: 'Citrus',
|
||||
VeggieCategory.cruciferous: 'Cruciferous',
|
||||
VeggieCategory.fern: 'Technically a fern',
|
||||
VeggieCategory.flower: 'Flower',
|
||||
VeggieCategory.fruit: 'Fruit',
|
||||
VeggieCategory.fungus: 'Fungus',
|
||||
VeggieCategory.gourd: 'Gourd',
|
||||
VeggieCategory.leafy: 'Leafy',
|
||||
VeggieCategory.legume: 'Legume',
|
||||
VeggieCategory.melon: 'Melon',
|
||||
VeggieCategory.root: 'Root vegetable',
|
||||
VeggieCategory.stealthFruit: 'Stealth fruit',
|
||||
VeggieCategory.stoneFruit: 'Stone fruit',
|
||||
VeggieCategory.tropical: 'Tropical',
|
||||
VeggieCategory.tuber: 'Tuber',
|
||||
VeggieCategory.vegetable: 'Vegetable',
|
||||
};
|
||||
|
||||
const Map<Season, String> seasonNames = {
|
||||
Season.winter: 'Winter',
|
||||
Season.spring: 'Spring',
|
||||
Season.summer: 'Summer',
|
||||
Season.autumn: 'Autumn',
|
||||
};
|
||||
|
||||
class Veggie {
|
||||
Veggie({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
@required this.imageAssetPath,
|
||||
@required this.category,
|
||||
@required this.shortDescription,
|
||||
@required this.accentColor,
|
||||
@required this.seasons,
|
||||
@required this.vitaminAPercentage,
|
||||
@required this.vitaminCPercentage,
|
||||
@required this.servingSize,
|
||||
@required this.caloriesPerServing,
|
||||
@required this.trivia,
|
||||
this.isFavorite = false,
|
||||
});
|
||||
|
||||
final int id;
|
||||
|
||||
final String name;
|
||||
|
||||
/// Each veggie has an associated image asset that's used as a background
|
||||
/// image and icon.
|
||||
final String imageAssetPath;
|
||||
|
||||
final VeggieCategory category;
|
||||
|
||||
/// A short, snappy line.
|
||||
final String shortDescription;
|
||||
|
||||
/// A color value to use when constructing UI elements to match the image
|
||||
/// found at [imageAssetPath].
|
||||
final Color accentColor;
|
||||
|
||||
/// Seasons during which a veggie is harvested.
|
||||
final List<Season> seasons;
|
||||
|
||||
/// Percentage of the FDA's recommended daily value of vitamin A for someone
|
||||
/// with a 2,000 calorie diet.
|
||||
final int vitaminAPercentage;
|
||||
|
||||
/// Percentage of the FDA's recommended daily value of vitamin C for someone
|
||||
/// with a 2,000 calorie diet.
|
||||
final int vitaminCPercentage;
|
||||
|
||||
/// A text description of a single serving (e.g. '1 apple' or '1/2 cup').
|
||||
final String servingSize;
|
||||
|
||||
/// Calories per serving (as described in [servingSize]).
|
||||
final int caloriesPerServing;
|
||||
|
||||
/// Whether or not the veggie has been saved to the user's garden (i.e. marked
|
||||
/// as a favorite).
|
||||
bool isFavorite;
|
||||
|
||||
/// A set of trivia questions and answers related to the veggie.
|
||||
final List<Trivia> trivia;
|
||||
|
||||
String get categoryName => veggieCategoryNames[category];
|
||||
}
|
||||
Reference in New Issue
Block a user