-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
234 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.