Skip to content

Commit

Permalink
added providers menu and cart #1
Browse files Browse the repository at this point in the history
  • Loading branch information
gelo-ussi committed Jul 16, 2020
1 parent 31a35f7 commit 00ad483
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 137 deletions.
42 changes: 38 additions & 4 deletions lib/app/home/cart/cart_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ecommerce_flutter_firebase/app/home/models/cart.dart';

class CartPage extends StatelessWidget {
@override
Expand Down Expand Up @@ -30,17 +32,49 @@ class CartPage extends StatelessWidget {
class _CartList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'CartList',
var itemNameStyle = Theme.of(context).textTheme.headline6;
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) {
return Text(
'CartTotal',
var hugeStyle =
Theme.of(context).textTheme.headline1.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'),
),
],
),
),
);
}
}
121 changes: 82 additions & 39 deletions lib/app/home/map/map_page.dart
Original file line number Diff line number Diff line change
@@ -1,54 +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:ecommerce_flutter_firebase/constants/strings.dart';
import 'package:provider/provider.dart';
import 'package:ecommerce_flutter_firebase/app/home/models/menu.dart';
import 'package:ecommerce_flutter_firebase/app/home/models/cart.dart';

class MapPage extends StatelessWidget {
void _captureFood() {
print('capture food button tapped');
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(Strings.homePage),
body: CustomScrollView(
slivers: [
_MyAppBar(),
SliverToBoxAdapter(child: SizedBox(height: 12)),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => _MyListItem(index)),
),
],
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'<Insert Google Maps Here>',
);
}
}

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('Menu', style: Theme.of(context).textTheme.headline1),
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<MenuModel>(context);
var item = catalog.getByPosition(index);
var textTheme = Theme.of(context).textTheme.headline6;

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,
),
),
Text(
'Food map',
style: Theme.of(context).textTheme.headline4,
SizedBox(width: 24),
Expanded(
child: Text(item.name, style: textTheme),
),
SizedBox(width: 24),
_AddButton(item: item),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _captureFood,
tooltip: 'Capture Food',
child: Icon(Icons.camera_alt),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
28 changes: 28 additions & 0 deletions lib/app/home/models/cart.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter/foundation.dart';
import 'package:ecommerce_flutter_firebase/app/home/models/menu.dart';

class CartModel extends ChangeNotifier {
MenuModel _menu;

final List<int> _itemIds = [];

MenuModel get menu => _menu;

set menu(MenuModel newMenu) {
assert(newMenu != null);
assert(_itemIds.every((id) => newMenu.getById(id) != null),
'The menu $newMenu does not have one of $_itemIds in it.');
_menu = newMenu;
notifyListeners();
}

List<Item> get items => _itemIds.map((id) => _menu.getById(id)).toList();

int get totalPrice =>
items.fold(0, (total, current) => total + current.price);

void add(Item item) {
_itemIds.add(item.id);
notifyListeners();
}
}
47 changes: 47 additions & 0 deletions lib/app/home/models/menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:flutter/material.dart';
import 'dart:math';

class MenuModel {
static List<String> itemNames = [
'Banana',
'Orange',
'Lemon',
'Carrots',
'Cucumbers',
'Tomatoes',
'Greenpepper',
'Redpepper',
'Sweetpotatoes',
'Whitepotatoes',
'Garlic',
'Onion',
'Romainelettuce',
'Spinach',
'Freshparsley',
'Freshcilantro',
];
Random rng = new Random();
Item getById(int id) =>
Item(id, itemNames[id % itemNames.length], rng.nextInt(90) + 10);

Item getByPosition(int position) {
return getById(position);
}
}

@immutable
class Item {
final int id;
final String name;
final Color color;
final int price;

Item(this.id, this.name, this.price)
: color = Colors.primaries[id % Colors.primaries.length];

@override
int get hashCode => id;

@override
bool operator ==(Object other) => other is Item && other.id == id;
}
Loading

0 comments on commit 00ad483

Please sign in to comment.