mirror of
https://github.com/flutter/samples.git
synced 2026-06-04 05:19:35 +00:00
Add provider_shopper (#87)
This is the code for the sample at https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple.
This commit is contained in:
83
provider_shopper/lib/screens/cart.dart
Normal file
83
provider_shopper/lib/screens/cart.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2019 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/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider_shopper/models/cart.dart';
|
||||
|
||||
class MyCart extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Cart', style: Theme.of(context).textTheme.display4),
|
||||
backgroundColor: Colors.white,
|
||||
),
|
||||
body: Container(
|
||||
color: Colors.yellow,
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: _CartList(),
|
||||
),
|
||||
),
|
||||
Divider(height: 4, color: Colors.black),
|
||||
_CartTotal()
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CartList extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var itemNameStyle = Theme.of(context).textTheme.title;
|
||||
var cart = Provider.of<CartModel>(context);
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: cart.items.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: Icon(Icons.done),
|
||||
title: Text(
|
||||
cart.items[index].name,
|
||||
style: itemNameStyle,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CartTotal extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var hugeStyle = Theme.of(context).textTheme.display4.copyWith(fontSize: 48);
|
||||
|
||||
return SizedBox(
|
||||
height: 200,
|
||||
child: Center(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Consumer<CartModel>(
|
||||
builder: (context, cart, child) =>
|
||||
Text('\$${cart.totalPrice}', style: hugeStyle)),
|
||||
SizedBox(width: 24),
|
||||
FlatButton(
|
||||
onPressed: () {
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Buying not supported yet.')));
|
||||
},
|
||||
color: Colors.white,
|
||||
child: Text('BUY'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
97
provider_shopper/lib/screens/catalog.dart
Normal file
97
provider_shopper/lib/screens/catalog.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright 2019 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/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:provider_shopper/models/cart.dart';
|
||||
import 'package:provider_shopper/models/catalog.dart';
|
||||
|
||||
class MyCatalog extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: CustomScrollView(
|
||||
slivers: [
|
||||
_MyAppBar(),
|
||||
SliverToBoxAdapter(child: SizedBox(height: 12)),
|
||||
SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) => _MyListItem(index)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AddButton extends StatelessWidget {
|
||||
final Item item;
|
||||
|
||||
const _AddButton({Key key, @required this.item}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var cart = Provider.of<CartModel>(context);
|
||||
|
||||
return FlatButton(
|
||||
onPressed: cart.items.contains(item) ? null : () => cart.add(item),
|
||||
splashColor: Theme.of(context).primaryColor,
|
||||
child: cart.items.contains(item)
|
||||
? Icon(Icons.check, semanticLabel: 'ADDED')
|
||||
: Text('ADD'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MyAppBar extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SliverAppBar(
|
||||
title: Text('Catalog', style: Theme.of(context).textTheme.display4),
|
||||
floating: true,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(Icons.shopping_cart),
|
||||
onPressed: () => Navigator.pushNamed(context, '/cart'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MyListItem extends StatelessWidget {
|
||||
final int index;
|
||||
|
||||
_MyListItem(this.index, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var catalog = Provider.of<CatalogModel>(context);
|
||||
var item = catalog.getByPosition(index);
|
||||
var textTheme = Theme.of(context).textTheme.title;
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: LimitedBox(
|
||||
maxHeight: 48,
|
||||
child: Row(
|
||||
children: [
|
||||
AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Container(
|
||||
color: item.color,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 24),
|
||||
Expanded(
|
||||
child: Text(item.name, style: textTheme),
|
||||
),
|
||||
SizedBox(width: 24),
|
||||
_AddButton(item: item),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user