mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +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:
38
provider_shopper/lib/models/cart.dart
Normal file
38
provider_shopper/lib/models/cart.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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/foundation.dart';
|
||||
import 'package:provider_shopper/models/catalog.dart';
|
||||
|
||||
class CartModel extends ChangeNotifier {
|
||||
/// The current catalog. Used to construct items from numeric ids.
|
||||
final CatalogModel _catalog;
|
||||
|
||||
/// Internal, private state of the cart. Stores the ids of each item.
|
||||
final List<int> _itemIds;
|
||||
|
||||
/// Construct a CartModel instance that is backed by a [CatalogModel] and
|
||||
/// an optional previous state of the cart.
|
||||
///
|
||||
/// If [previous] is not `null`, it's items are copied to the newly
|
||||
/// constructed instance.
|
||||
CartModel(this._catalog, CartModel previous)
|
||||
: assert(_catalog != null),
|
||||
_itemIds = previous?._itemIds ?? [];
|
||||
|
||||
/// List of items in the cart.
|
||||
List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
|
||||
|
||||
/// The current total price of all items.
|
||||
int get totalPrice =>
|
||||
items.fold(0, (total, current) => total + current.price);
|
||||
|
||||
/// Adds [item] to cart. This is the only way to modify the cart from outside.
|
||||
void add(Item item) {
|
||||
_itemIds.add(item.id);
|
||||
// This line tells [Model] that it should rebuild the widgets that
|
||||
// depend on it.
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
63
provider_shopper/lib/models/catalog.dart
Normal file
63
provider_shopper/lib/models/catalog.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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';
|
||||
|
||||
/// A proxy of the catalog of items the user can buy.
|
||||
///
|
||||
/// In a real app, this might be backed by a backend and cached on device.
|
||||
/// In this sample app, the catalog is procedurally generated and infinite.
|
||||
///
|
||||
/// For simplicity, the catalog is expected to be immutable (no products are
|
||||
/// expected to be added, removed or changed during the execution of the app).
|
||||
class CatalogModel {
|
||||
static const _itemNames = [
|
||||
'Code Smell',
|
||||
'Control Flow',
|
||||
'Interpreter',
|
||||
'Recursion',
|
||||
'Sprint',
|
||||
'Heisenbug',
|
||||
'Spaghetti',
|
||||
'Hydra Code',
|
||||
'Off-By-One',
|
||||
'Scope',
|
||||
'Callback',
|
||||
'Closure',
|
||||
'Automata',
|
||||
'Bit Shift',
|
||||
'Currying',
|
||||
];
|
||||
|
||||
/// Get item by [id].
|
||||
///
|
||||
/// In this sample, the catalog is infinite, looping over [_itemNames].
|
||||
Item getById(int id) => Item(id, _itemNames[id % _itemNames.length]);
|
||||
|
||||
/// Get item by its position in the catalog.
|
||||
Item getByPosition(int position) {
|
||||
// In this simplified case, an item's position in the catalog
|
||||
// is also its id.
|
||||
return getById(position);
|
||||
}
|
||||
}
|
||||
|
||||
@immutable
|
||||
class Item {
|
||||
final int id;
|
||||
final String name;
|
||||
final Color color;
|
||||
final int price = 42;
|
||||
|
||||
Item(this.id, this.name)
|
||||
// To make the sample app look nicer, each item is given one of the
|
||||
// Material Design primary colors.
|
||||
: color = Colors.primaries[id % Colors.primaries.length];
|
||||
|
||||
@override
|
||||
int get hashCode => id;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => other is Item && other.id == id;
|
||||
}
|
||||
Reference in New Issue
Block a user