Fixing unchangeable Shrine categories. (#9)

pull/11/head
Andrew Brogdon 6 years ago committed by GitHub
parent 2681473884
commit 370a72caa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,6 @@ import 'category_menu_page.dart';
import 'colors.dart'; import 'colors.dart';
import 'home.dart'; import 'home.dart';
import 'login.dart'; import 'login.dart';
import 'model/product.dart';
import 'supplemental/cut_corners_border.dart'; import 'supplemental/cut_corners_border.dart';
class ShrineApp extends StatefulWidget { class ShrineApp extends StatefulWidget {
@ -28,19 +27,13 @@ class ShrineApp extends StatefulWidget {
} }
class _ShrineAppState extends State<ShrineApp> { class _ShrineAppState extends State<ShrineApp> {
Category _currentCategory = Category.all;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Shrine', title: 'Shrine',
home: Backdrop( home: Backdrop(
currentCategory: _currentCategory, frontLayer: HomePage(),
frontLayer: HomePage(category: _currentCategory), backLayer: CategoryMenuPage(),
backLayer: CategoryMenuPage(
currentCategory: _currentCategory,
onCategoryTap: _onCategoryTap,
),
frontTitle: Text('SHRINE'), frontTitle: Text('SHRINE'),
backTitle: Text('MENU'), backTitle: Text('MENU'),
), ),
@ -49,13 +42,6 @@ class _ShrineAppState extends State<ShrineApp> {
theme: _kShrineTheme, theme: _kShrineTheme,
); );
} }
/// Function to call when a [Category] is tapped.
void _onCategoryTap(Category category) {
setState(() {
_currentCategory = category;
});
}
} }
Route<dynamic> _getRoute(RouteSettings settings) { Route<dynamic> _getRoute(RouteSettings settings) {

@ -16,7 +16,6 @@ import 'package:flutter/material.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'login.dart'; import 'login.dart';
import 'model/product.dart';
import 'shopping_cart.dart'; import 'shopping_cart.dart';
const double _kFlingVelocity = 2.0; const double _kFlingVelocity = 2.0;
@ -147,20 +146,17 @@ class _BackdropTitle extends AnimatedWidget {
/// can make a selection. The user can also configure the titles for when the /// can make a selection. The user can also configure the titles for when the
/// front or back layer is showing. /// front or back layer is showing.
class Backdrop extends StatefulWidget { class Backdrop extends StatefulWidget {
final Category currentCategory;
final Widget frontLayer; final Widget frontLayer;
final Widget backLayer; final Widget backLayer;
final Widget frontTitle; final Widget frontTitle;
final Widget backTitle; final Widget backTitle;
const Backdrop({ const Backdrop({
@required this.currentCategory,
@required this.frontLayer, @required this.frontLayer,
@required this.backLayer, @required this.backLayer,
@required this.frontTitle, @required this.frontTitle,
@required this.backTitle, @required this.backTitle,
}) : assert(currentCategory != null), }) : assert(frontLayer != null),
assert(frontLayer != null),
assert(backLayer != null), assert(backLayer != null),
assert(frontTitle != null), assert(frontTitle != null),
assert(backTitle != null); assert(backTitle != null);
@ -184,17 +180,6 @@ class _BackdropState extends State<Backdrop>
); );
} }
@override
void didUpdateWidget(Backdrop old) {
super.didUpdateWidget(old);
if (widget.currentCategory != old.currentCategory) {
_toggleBackdropLayerVisibility();
} else if (!_frontLayerVisible) {
_controller.fling(velocity: _kFlingVelocity);
}
}
@override @override
void dispose() { void dispose() {
_controller.dispose(); _controller.dispose();

@ -13,30 +13,27 @@
// limitations under the License. // limitations under the License.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:meta/meta.dart'; import 'package:scoped_model/scoped_model.dart';
import 'colors.dart'; import 'colors.dart';
import 'model/app_state_model.dart';
import 'model/product.dart'; import 'model/product.dart';
class CategoryMenuPage extends StatelessWidget { class CategoryMenuPage extends StatelessWidget {
final Category currentCategory;
final ValueChanged<Category> onCategoryTap;
final List<Category> _categories = Category.values; final List<Category> _categories = Category.values;
const CategoryMenuPage({ const CategoryMenuPage({
Key key, Key key,
@required this.currentCategory, });
@required this.onCategoryTap,
}) : assert(currentCategory != null),
assert(onCategoryTap != null);
Widget _buildCategory(Category category, BuildContext context) { Widget _buildCategory(Category category, BuildContext context) {
final categoryString = final categoryString =
category.toString().replaceAll('Category.', '').toUpperCase(); category.toString().replaceAll('Category.', '').toUpperCase();
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
return GestureDetector( return ScopedModelDescendant<AppStateModel>(
onTap: () => onCategoryTap(category), builder: (context, child, model) => GestureDetector(
child: category == currentCategory onTap: () => model.setCategory(category),
child: model.selectedCategory == category
? Column( ? Column(
children: <Widget>[ children: <Widget>[
SizedBox(height: 16.0), SizedBox(height: 16.0),
@ -62,6 +59,7 @@ class CategoryMenuPage extends StatelessWidget {
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
), ),
),
); );
} }

@ -13,12 +13,16 @@
// limitations under the License. // limitations under the License.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scoped_model/scoped_model.dart'; import 'package:scoped_model/scoped_model.dart';
import 'app.dart'; import 'app.dart';
import 'model/app_state_model.dart'; import 'model/app_state_model.dart';
void main() { void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
AppStateModel model = AppStateModel(); AppStateModel model = AppStateModel();
model.loadProducts(); model.loadProducts();

@ -60,7 +60,9 @@ class AppStateModel extends Model {
if (_selectedCategory == Category.all) { if (_selectedCategory == Category.all) {
return List.from(_availableProducts); return List.from(_availableProducts);
} else { } else {
return _availableProducts.where((p) => p.category == _selectedCategory); return _availableProducts
.where((p) => p.category == _selectedCategory)
.toList();
} }
} }
@ -104,4 +106,9 @@ class AppStateModel extends Model {
_availableProducts = ProductsRepository.loadProducts(); _availableProducts = ProductsRepository.loadProducts();
notifyListeners(); notifyListeners();
} }
void setCategory(Category newCategory) {
_selectedCategory = newCategory;
notifyListeners();
}
} }

Loading…
Cancel
Save