mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
153 lines
4.5 KiB
Dart
153 lines
4.5 KiB
Dart
// Copyright 2016 The Chromium Authors. 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:flutter/material.dart';
|
|
|
|
import 'shrine_theme.dart';
|
|
import 'shrine_types.dart';
|
|
|
|
enum ShrineAction {
|
|
sortByPrice,
|
|
sortByProduct,
|
|
emptyCart
|
|
}
|
|
|
|
class ShrinePage extends StatefulWidget {
|
|
ShrinePage({
|
|
Key key,
|
|
@required this.scaffoldKey,
|
|
@required this.body,
|
|
this.floatingActionButton,
|
|
this.products,
|
|
this.shoppingCart
|
|
}) : super(key: key) {
|
|
assert(body != null);
|
|
assert(scaffoldKey != null);
|
|
}
|
|
|
|
final GlobalKey<ScaffoldState> scaffoldKey;
|
|
final Widget body;
|
|
final Widget floatingActionButton;
|
|
final List<Product> products;
|
|
final Map<Product, Order> shoppingCart;
|
|
|
|
@override
|
|
ShrinePageState createState() => new ShrinePageState();
|
|
}
|
|
|
|
/// Defines the Scaffold, AppBar, etc that the demo pages have in common.
|
|
class ShrinePageState extends State<ShrinePage> {
|
|
int _appBarElevation = 0;
|
|
|
|
bool _handleScrollNotification(ScrollNotification notification) {
|
|
final int elevation = notification.metrics.extentBefore <= 0.0 ? 0 : 1;
|
|
if (elevation != _appBarElevation) {
|
|
setState(() {
|
|
_appBarElevation = elevation;
|
|
});
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void _showShoppingCart() {
|
|
showModalBottomSheet<Null>(context: context, builder: (BuildContext context) {
|
|
if (config.shoppingCart.isEmpty) {
|
|
return const Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: const Text('The shopping cart is empty')
|
|
);
|
|
}
|
|
return new ListView(
|
|
padding: kMaterialListPadding,
|
|
children: config.shoppingCart.values.map((Order order) {
|
|
return new ListTile(
|
|
title: new Text(order.product.name),
|
|
leading: new Text('${order.quantity}'),
|
|
subtitle: new Text(order.product.vendor.name)
|
|
);
|
|
}).toList(),
|
|
);
|
|
});
|
|
}
|
|
|
|
void _sortByPrice() {
|
|
config.products.sort((Product a, Product b) => a.price.compareTo(b.price));
|
|
}
|
|
|
|
void _sortByProduct() {
|
|
config.products.sort((Product a, Product b) => a.name.compareTo(b.name));
|
|
}
|
|
|
|
void _emptyCart() {
|
|
config.shoppingCart.clear();
|
|
config.scaffoldKey.currentState.showSnackBar(new SnackBar(content: const Text('Shopping cart is empty')));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final ShrineTheme theme = ShrineTheme.of(context);
|
|
return new Scaffold(
|
|
key: config.scaffoldKey,
|
|
appBar: new AppBar(
|
|
elevation: _appBarElevation,
|
|
backgroundColor: theme.appBarBackgroundColor,
|
|
iconTheme: Theme.of(context).iconTheme,
|
|
brightness: Brightness.light,
|
|
flexibleSpace: new Container(
|
|
decoration: new BoxDecoration(
|
|
border: new Border(
|
|
bottom: new BorderSide(color: theme.dividerColor)
|
|
)
|
|
)
|
|
),
|
|
title: new Center(
|
|
child: new Text('SHRINE', style: ShrineTheme.of(context).appBarTitleStyle)
|
|
),
|
|
actions: <Widget>[
|
|
new IconButton(
|
|
icon: const Icon(Icons.shopping_cart),
|
|
tooltip: 'Shopping cart',
|
|
onPressed: _showShoppingCart
|
|
),
|
|
new PopupMenuButton<ShrineAction>(
|
|
itemBuilder: (BuildContext context) => <PopupMenuItem<ShrineAction>>[
|
|
new PopupMenuItem<ShrineAction>(
|
|
value: ShrineAction.sortByPrice,
|
|
child: const Text('Sort by price')
|
|
),
|
|
new PopupMenuItem<ShrineAction>(
|
|
value: ShrineAction.sortByProduct,
|
|
child: const Text('Sort by product')
|
|
),
|
|
new PopupMenuItem<ShrineAction>(
|
|
value: ShrineAction.emptyCart,
|
|
child: const Text('Empty shopping cart')
|
|
)
|
|
],
|
|
onSelected: (ShrineAction action) {
|
|
switch (action) {
|
|
case ShrineAction.sortByPrice:
|
|
setState(_sortByPrice);
|
|
break;
|
|
case ShrineAction.sortByProduct:
|
|
setState(_sortByProduct);
|
|
break;
|
|
case ShrineAction.emptyCart:
|
|
setState(_emptyCart);
|
|
break;
|
|
}
|
|
}
|
|
)
|
|
]
|
|
),
|
|
floatingActionButton: config.floatingActionButton,
|
|
body: new NotificationListener<ScrollNotification>(
|
|
onNotification: _handleScrollNotification,
|
|
child: config.body
|
|
)
|
|
);
|
|
}
|
|
}
|