From 8e0e81da6400566e93a71f876abcfe4f1b30fe17 Mon Sep 17 00:00:00 2001 From: Abdul Aziz Ansari Date: Tue, 2 Mar 2021 18:32:40 +0530 Subject: [PATCH 1/2] migrated to latest flutter version --- android/app/build.gradle | 4 ++-- android/app/src/main/AndroidManifest.xml | 21 +++++++++--------- .../groceryshopflutter/MainActivity.java | 13 ----------- .../plugins/GeneratedPluginRegistrant.java | 22 +++++++------------ android/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- android/local.properties | 4 ++-- pubspec.yaml | 4 ++-- 8 files changed, 26 insertions(+), 46 deletions(-) delete mode 100644 android/app/src/main/java/com/example/groceryshopflutter/MainActivity.java diff --git a/android/app/build.gradle b/android/app/build.gradle index 162bb94..7a3e3ce 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 27 + compileSdkVersion 28 lintOptions { disable 'InvalidPackage' @@ -35,7 +35,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.groceryshopflutter" minSdkVersion 16 - targetSdkVersion 27 + targetSdkVersion 29 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 2983ce8..087cc06 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -5,7 +5,7 @@ flutter needs it to communicate with the running application to allow setting breakpoints, to provide hot reload, etc. --> - + + android:icon="@mipmap/ic_launcher" + android:label="grocery_shop_flutter"> + android:name="flutterEmbedding" + android:value="2" /> - - + + diff --git a/android/app/src/main/java/com/example/groceryshopflutter/MainActivity.java b/android/app/src/main/java/com/example/groceryshopflutter/MainActivity.java deleted file mode 100644 index 10c3c14..0000000 --- a/android/app/src/main/java/com/example/groceryshopflutter/MainActivity.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.groceryshopflutter; - -import android.os.Bundle; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; - -public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } -} diff --git a/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java b/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java index d007606..afa3bb6 100644 --- a/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java +++ b/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java @@ -1,23 +1,17 @@ package io.flutter.plugins; -import io.flutter.plugin.common.PluginRegistry; +import androidx.annotation.Keep; +import androidx.annotation.NonNull; + +import io.flutter.embedding.engine.FlutterEngine; /** * Generated file. Do not edit. + * This file is generated by the Flutter tool based on the + * plugins that support the Android platform. */ +@Keep public final class GeneratedPluginRegistrant { - public static void registerWith(PluginRegistry registry) { - if (alreadyRegisteredWith(registry)) { - return; - } - } - - private static boolean alreadyRegisteredWith(PluginRegistry registry) { - final String key = GeneratedPluginRegistrant.class.getCanonicalName(); - if (registry.hasPlugin(key)) { - return true; - } - registry.registrarFor(key); - return false; + public static void registerWith(@NonNull FlutterEngine flutterEngine) { } } diff --git a/android/build.gradle b/android/build.gradle index bb8a303..e0d7ae2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:3.5.0' } } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 2819f02..e063d0d 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip \ No newline at end of file diff --git a/android/local.properties b/android/local.properties index c7a3423..fca6983 100644 --- a/android/local.properties +++ b/android/local.properties @@ -1,5 +1,5 @@ -sdk.dir=C:\\Users\\willn\\AppData\\Local\\Android\\sdk -flutter.sdk=C:\\src\\flutter +sdk.dir=D:\\sdk +flutter.sdk=D:\\flutter-sdk flutter.versionName=1.0.0 flutter.versionCode=1 flutter.buildMode=debug \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 77bdd4d..d04406c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,7 +10,7 @@ description: A new Flutter project. version: 1.0.0+1 environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: ">=2.7.0 <3.0.0" dependencies: flutter: @@ -18,7 +18,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.2 + cupertino_icons: ^0.1.3 rxdart: ^0.21.0 From 71790943bc23133388b6c562baf00ddf9f39329a Mon Sep 17 00:00:00 2001 From: Abdul Aziz Ansari Date: Wed, 3 Mar 2021 14:21:52 +0530 Subject: [PATCH 2/2] removed "new" keyword --- lib/bloc/CartBloc.dart | 22 ++- lib/components/CartManager.dart | 137 ++++++++++------ lib/components/CategoryDropMenu.dart | 35 ++-- lib/components/GridShop.dart | 95 +++++++---- lib/components/MinimalCart.dart | 68 +++++--- lib/components/OrderWidget.dart | 48 ++++-- lib/components/ProductWidget.dart | 61 +++++-- lib/models/Cart.dart | 2 +- lib/repositories/ProductsRepository.dart | 21 ++- lib/views/Home.dart | 66 ++++---- lib/views/ProductView.dart | 200 +++++++++++++++-------- 11 files changed, 484 insertions(+), 271 deletions(-) diff --git a/lib/bloc/CartBloc.dart b/lib/bloc/CartBloc.dart index 4f89cbf..c4ae06f 100644 --- a/lib/bloc/CartBloc.dart +++ b/lib/bloc/CartBloc.dart @@ -1,10 +1,9 @@ -import 'package:rxdart/rxdart.dart'; -import 'package:grocery_shop_flutter/models/Product.dart'; -import 'package:grocery_shop_flutter/models/Order.dart'; import 'package:grocery_shop_flutter/models/Cart.dart'; +import 'package:grocery_shop_flutter/models/Order.dart'; +import 'package:grocery_shop_flutter/models/Product.dart'; +import 'package:rxdart/rxdart.dart'; -class CartBloc{ - +class CartBloc { static int _orderId = 0; static CartBloc _cartBloc; Cart _currentCart; @@ -13,16 +12,15 @@ class CartBloc{ PublishSubject _publishSubjectOrder; factory CartBloc(){ - if(_cartBloc == null) - _cartBloc = new CartBloc._(); + if (_cartBloc == null) _cartBloc = CartBloc._(); return _cartBloc; } - CartBloc._(){ - _currentCart = new Cart(); - _publishSubjectCart = new PublishSubject(); - _publishSubjectOrder = new PublishSubject(); + CartBloc._() { + _currentCart = Cart(); + _publishSubjectCart = PublishSubject(); + _publishSubjectOrder = PublishSubject(); } Observable get observableCart => _publishSubjectCart.stream; @@ -37,7 +35,7 @@ class CartBloc{ } void addOrderToCart(Product product, int quantity){ - _lastOrder = new Order(product, quantity, _orderId++); + _lastOrder = Order(product, quantity, _orderId++); _currentCart.addOrder(_lastOrder); _updateLastOrder(); _updateCart(); diff --git a/lib/components/CartManager.dart b/lib/components/CartManager.dart index 2817499..3ecf09b 100644 --- a/lib/components/CartManager.dart +++ b/lib/components/CartManager.dart @@ -1,65 +1,106 @@ import 'package:flutter/material.dart'; -import 'package:grocery_shop_flutter/models/Cart.dart'; import 'package:grocery_shop_flutter/bloc/CartBloc.dart'; import 'package:grocery_shop_flutter/components/OrderWidget.dart'; +import 'package:grocery_shop_flutter/models/Cart.dart'; class CartManager extends StatefulWidget { @override - _CartManager createState() => new _CartManager(); + _CartManager createState() => _CartManager(); } class _CartManager extends State { - CartBloc _cartBloc = new CartBloc(); + CartBloc _cartBloc = CartBloc(); @override Widget build(BuildContext context) { + double _gridSize = MediaQuery.of(context).size.height * 0.88; - double _gridSize = MediaQuery.of(context).size.height*0.88; - - return new Container(height: MediaQuery.of(context).size.height, child: - new Stack(children: [ - new Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ - new StreamBuilder(initialData: _cartBloc.currentCart, stream: _cartBloc.observableCart, builder: (context, AsyncSnapshot snapshot){ - return - new Container(margin: EdgeInsets.symmetric(horizontal: 20), height: _gridSize, width: double.infinity, child: - new Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Padding(padding: EdgeInsets.symmetric(vertical: 40), child: - new Text("Cart", style: TextStyle(color: Colors.white, fontSize: 40, fontWeight: FontWeight.bold)) - ), - new Container(margin: EdgeInsets.only(bottom: 10), height: _gridSize*0.60, child: - new ListView.builder(itemCount: snapshot.data.orders.length, itemBuilder: (context, index ){ - return Dismissible( - background: Container(color: Colors.transparent), - key: Key(snapshot.data.orders[index].id.toString()), - onDismissed: (_) { - _cartBloc.removerOrderOfCart(snapshot.data.orders[index]); - }, - child: new Padding(padding: EdgeInsets.symmetric(vertical: 10), child: new OrderWidget(snapshot.data.orders[index], _gridSize)), - ); - }) - ), - new Container(height: _gridSize*0.15, child: - new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - new Text("Total", style: TextStyle(color: Colors.white, fontSize: 20)), - new Text("\$${snapshot.data.totalPrice().toStringAsFixed(2)}", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 40)), - ]) - ) - ]) - ); - }) - ]), - new Align(alignment: Alignment.bottomLeft, child: - new Container(padding: EdgeInsets.only(left: 10, bottom: _gridSize*0.02), width: MediaQuery.of(context).size.width - 80, child: - new RaisedButton(color: Colors.amber, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(60)), padding: EdgeInsets.all(20), - onPressed: (){ - if(_cartBloc.currentCart.isEmpty) - Scaffold.of(context).showSnackBar(SnackBar(content: Text("Cart is empty"))); - }, - child: new Text("Next", style: TextStyle(fontWeight: FontWeight.bold)) - ) - ) - ) + return Container( + height: MediaQuery.of(context).size.height, + child: Stack(children: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + StreamBuilder( + initialData: _cartBloc.currentCart, + stream: _cartBloc.observableCart, + builder: (context, AsyncSnapshot snapshot) { + return Container( + margin: EdgeInsets.symmetric(horizontal: 20), + height: _gridSize, + width: double.infinity, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: EdgeInsets.symmetric(vertical: 40), + child: Text("Cart", + style: TextStyle( + color: Colors.white, + fontSize: 40, + fontWeight: FontWeight.bold))), + Container( + margin: EdgeInsets.only(bottom: 10), + height: _gridSize * 0.60, + child: ListView.builder( + itemCount: snapshot.data.orders.length, + itemBuilder: (context, index) { + return Dismissible( + background: Container( + color: Colors.transparent), + key: Key(snapshot + .data.orders[index].id + .toString()), + onDismissed: (_) { + _cartBloc.removerOrderOfCart( + snapshot.data.orders[index]); + }, + child: Padding( + padding: EdgeInsets.symmetric( + vertical: 10), + child: OrderWidget( + snapshot.data.orders[index], + _gridSize)), + ); + })), + Container( + height: _gridSize * 0.15, + child: Row( + mainAxisAlignment: + MainAxisAlignment.spaceBetween, + children: [ + Text("Total", + style: TextStyle( + color: Colors.white, + fontSize: 20)), + Text( + "\$${snapshot.data.totalPrice().toStringAsFixed(2)}", + style: TextStyle( + color: Colors.white, + fontWeight: FontWeight.bold, + fontSize: 40)), + ])) + ])); + }) + ]), + Align( + alignment: Alignment.bottomLeft, + child: Container( + padding: EdgeInsets.only(left: 10, bottom: _gridSize * 0.02), + width: MediaQuery.of(context).size.width - 80, + child: RaisedButton( + color: Colors.amber, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(60)), + padding: EdgeInsets.all(20), + onPressed: () { + if (_cartBloc.currentCart.isEmpty) + Scaffold.of(context).showSnackBar( + SnackBar(content: Text("Cart is empty"))); + }, + child: Text("Next", + style: TextStyle(fontWeight: FontWeight.bold))))) ]) ); } diff --git a/lib/components/CategoryDropMenu.dart b/lib/components/CategoryDropMenu.dart index e426a65..7b95fda 100644 --- a/lib/components/CategoryDropMenu.dart +++ b/lib/components/CategoryDropMenu.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; class CategoryDropMenu extends StatefulWidget { @override - _CategoryDropMenu createState() => new _CategoryDropMenu(); + _CategoryDropMenu createState() => _CategoryDropMenu(); } class _CategoryDropMenu extends State { @@ -10,20 +10,25 @@ class _CategoryDropMenu extends State { String dropdownValue = "Pasta & Noodles"; @override - Widget build(BuildContext context){ - return new DropdownButtonHideUnderline(child: - new DropdownButton( - value: dropdownValue, - onChanged: (String newValue) { - setState(() { - dropdownValue = newValue; - }); - }, - items: ['Pasta & Noodles'].map>((String value) { - return DropdownMenuItem( - value: value, - child: Text(value, style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20)), - ); + Widget build(BuildContext context) { + return DropdownButtonHideUnderline( + child: DropdownButton( + value: dropdownValue, + onChanged: (String Value) { + setState(() { + dropdownValue = Value; + }); + }, + items: ['Pasta & Noodles'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value, + style: TextStyle( + color: Colors.black, + fontWeight: FontWeight.bold, + fontSize: 20)), + ); }).toList(), )); } diff --git a/lib/components/GridShop.dart b/lib/components/GridShop.dart index dabdbeb..ba4facc 100644 --- a/lib/components/GridShop.dart +++ b/lib/components/GridShop.dart @@ -1,54 +1,79 @@ import 'package:flutter/material.dart'; -import 'package:grocery_shop_flutter/models/Product.dart'; -import 'package:grocery_shop_flutter/repositories/ProductsRepository.dart'; import 'package:grocery_shop_flutter/components/CategoryDropMenu.dart'; -import 'package:grocery_shop_flutter/components/ProductWidget.dart'; import 'package:grocery_shop_flutter/components/MinimalCart.dart'; +import 'package:grocery_shop_flutter/components/ProductWidget.dart'; +import 'package:grocery_shop_flutter/models/Product.dart'; +import 'package:grocery_shop_flutter/repositories/ProductsRepository.dart'; class GridShop extends StatefulWidget { - @override - _GridShop createState() => new _GridShop(); + @override + _GridShop createState() => _GridShop(); } class _GridShop extends State { - @override - Widget build(BuildContext context){ - - double _gridSize = MediaQuery.of(context).size.height*0.88; //88% of screen - double childAspectRatio = MediaQuery.of(context).size.width / (MediaQuery.of(context).size.height / 1.0); + Widget build(BuildContext context) { + double _gridSize = + MediaQuery.of(context).size.height * 0.88; //88% of screen + double childAspectRatio = MediaQuery.of(context).size.width / + (MediaQuery.of(context).size.height / 1.0); - List _products = new ProductsRepository().fetchAllProducts(); + List _products = ProductsRepository().fetchAllProducts(); - return new Column(children: [ - new Container(height: _gridSize, decoration: BoxDecoration(color: const Color(0xFFeeeeee), borderRadius: BorderRadius.only(bottomLeft: Radius.circular(_gridSize/10), bottomRight: Radius.circular(_gridSize/10))), padding: EdgeInsets.only(left: 10, right: 10), child: - new Column(children: [ - new Container(margin: EdgeInsets.only(top: 40), child: - new Column(children: [ - new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - new CategoryDropMenu(), - new FlatButton.icon(onPressed: (){}, icon: new Icon(Icons.filter_list), label: new Text("")) - ]), - new Container(height: _gridSize - 88, margin: EdgeInsets.only(top: 0), child: - new PhysicalModel( - color: Colors.transparent, - borderRadius: BorderRadius.only(bottomLeft: Radius.circular(_gridSize/10 - 10), bottomRight: Radius.circular(_gridSize/10 - 10)), - clipBehavior: Clip.antiAlias, - child: new GridView.builder( - itemCount: _products.length, gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: childAspectRatio), - itemBuilder: (BuildContext context, int index) { - return new Padding( - padding: EdgeInsets.only(top: index%2==0 ? 20 : 0, right: index%2==0 ? 5 : 0, left: index%2==1 ? 5 : 0, bottom: index%2==1 ? 20 : 0), - child: ProductWidget(product: _products[index])); - } - ) - ) + return Column(children: [ + Container( + height: _gridSize, + decoration: BoxDecoration( + color: const Color(0xFFeeeeee), + borderRadius: BorderRadius.only( + bottomLeft: Radius.circular(_gridSize / 10), + bottomRight: Radius.circular(_gridSize / 10))), + padding: EdgeInsets.only(left: 10, right: 10), + child: Column(children: [ + Container( + margin: EdgeInsets.only(top: 40), + child: Column(children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + CategoryDropMenu(), + FlatButton.icon( + onPressed: () {}, + icon: Icon(Icons.filter_list), + label: Text("")) + ]), + Container( + height: _gridSize - 88, + margin: EdgeInsets.only(top: 0), + child: PhysicalModel( + color: Colors.transparent, + borderRadius: BorderRadius.only( + bottomLeft: Radius.circular(_gridSize / 10 - 10), + bottomRight: + Radius.circular(_gridSize / 10 - 10)), + clipBehavior: Clip.antiAlias, + child: GridView.builder( + itemCount: _products.length, + gridDelegate: + SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2, + childAspectRatio: childAspectRatio), + itemBuilder: (BuildContext context, int index) { + return Padding( + padding: EdgeInsets.only( + top: index % 2 == 0 ? 20 : 0, + right: index % 2 == 0 ? 5 : 0, + left: index % 2 == 1 ? 5 : 0, + bottom: index % 2 == 1 ? 20 : 0), + child: ProductWidget( + product: _products[index])); + })) ) ]) ) ]) ), - new MinimalCart(_gridSize) + MinimalCart(_gridSize) ]); } diff --git a/lib/components/MinimalCart.dart b/lib/components/MinimalCart.dart index 752ea5e..6499774 100644 --- a/lib/components/MinimalCart.dart +++ b/lib/components/MinimalCart.dart @@ -1,28 +1,40 @@ import 'package:flutter/material.dart'; -import 'package:grocery_shop_flutter/models/Cart.dart'; import 'package:grocery_shop_flutter/bloc/CartBloc.dart'; +import 'package:grocery_shop_flutter/models/Cart.dart'; class MinimalCart extends StatelessWidget { final double _gridSize; - final List _listWidget = new List(); - final CartBloc _cartBloc = new CartBloc(); - static final ScrollController _scrollController = new ScrollController(); + final List _listWidget = List(); + final CartBloc _cartBloc = CartBloc(); + static final ScrollController _scrollController = ScrollController(); + MinimalCart(this._gridSize); @override Widget build(BuildContext context) { - return new StreamBuilder(initialData: _cartBloc.currentCart, stream: _cartBloc.observableCart, builder: (context, AsyncSnapshot snapshot) { + return StreamBuilder( + initialData: _cartBloc.currentCart, + stream: _cartBloc.observableCart, + builder: (context, AsyncSnapshot snapshot) { _fillList(snapshot.data, context); - var content = - new Container(margin: EdgeInsets.only(left: 10, right: 80), width: double.infinity, height: MediaQuery.of(context).size.height - _gridSize, child: - new ListView.builder(scrollDirection: Axis.horizontal, itemCount: _listWidget.length, controller: _scrollController, itemBuilder: (context, index){ - return new Align(alignment: Alignment.centerLeft, child:_listWidget[index]); - }) - ); - try{ - _scrollController.jumpTo(_scrollController.position.maxScrollExtent); - }catch(e){ + var content = Container( + margin: EdgeInsets.only(left: 10, right: 80), + width: double.infinity, + height: MediaQuery.of(context).size.height - _gridSize, + child: ListView.builder( + scrollDirection: Axis.horizontal, + itemCount: _listWidget.length, + controller: _scrollController, + itemBuilder: (context, index) { + return Align( + alignment: Alignment.centerLeft, + child: _listWidget[index]); + })); + try { + _scrollController + .jumpTo(_scrollController.position.maxScrollExtent); + } catch (e) { //TODO fix } return content; @@ -31,20 +43,26 @@ class MinimalCart extends StatelessWidget { } void _fillList(Cart cart, BuildContext context){ - _listWidget.add(new Text("Cart", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20))); - _listWidget.addAll(cart.orders.map((order){ - return new Padding(padding: EdgeInsets.symmetric(horizontal: 10), child: - new GestureDetector(child: - new Hero(tag: "tagHeroOrder${order.id}", child: - new ClipOval(child: - new Container(color: Colors.white, child: new Image.asset(order.product.urlToImage), height: (MediaQuery.of(context).size.height - _gridSize)* 0.6) - ), + _listWidget.add(Text("Cart", + style: TextStyle( + color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20))); + _listWidget.addAll(cart.orders.map((order) { + return Padding( + padding: EdgeInsets.symmetric(horizontal: 10), + child: GestureDetector( + child: Hero( + tag: "tagHeroOrder${order.id}", + child: ClipOval( + child: Container( + color: Colors.white, + child: Image.asset(order.product.urlToImage), + height: (MediaQuery.of(context).size.height - _gridSize) * + 0.6)), ), - onTap: (){ + onTap: () { //TODO }, - ) - ); + )); }).toList()); } diff --git a/lib/components/OrderWidget.dart b/lib/components/OrderWidget.dart index f0971a0..d60856b 100644 --- a/lib/components/OrderWidget.dart +++ b/lib/components/OrderWidget.dart @@ -9,23 +9,37 @@ class OrderWidget extends StatelessWidget { @override Widget build(BuildContext context) { - return new Row(crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - new ClipOval(child: - new Container(color: Colors.white, child: new Image.asset(this._order.product.urlToImage), height: (MediaQuery.of(context).size.height - _gridSize)* 0.5) - ), - new Padding(padding: EdgeInsets.symmetric(horizontal: 10), child: - new Text(this._order.quantity.toString(), style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)) - ), - new Text("x", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), - new Flexible(flex: 2, child: - new Padding(padding: EdgeInsets.symmetric(horizontal: 10), child: - new Text(this._order.product.title, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)) - ) - ), - new Flexible(flex: 1, child: - new Text("\$${this._order.orderPrice.toStringAsFixed(2)}", style: TextStyle(color: Colors.grey, fontWeight: FontWeight.bold)) - ) - ]); + return Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + ClipOval( + child: Container( + color: Colors.white, + child: Image.asset(this._order.product.urlToImage), + height: + (MediaQuery.of(context).size.height - _gridSize) * 0.5)), + Padding( + padding: EdgeInsets.symmetric(horizontal: 10), + child: Text(this._order.quantity.toString(), + style: TextStyle( + color: Colors.white, fontWeight: FontWeight.bold))), + Text("x", + style: + TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), + Flexible( + flex: 2, + child: Padding( + padding: EdgeInsets.symmetric(horizontal: 10), + child: Text(this._order.product.title, + style: TextStyle( + color: Colors.white, fontWeight: FontWeight.bold)))), + Flexible( + flex: 1, + child: Text("\$${this._order.orderPrice.toStringAsFixed(2)}", + style: TextStyle( + color: Colors.grey, fontWeight: FontWeight.bold))) + ]); } } \ No newline at end of file diff --git a/lib/components/ProductWidget.dart b/lib/components/ProductWidget.dart index 9c50ef9..ac2dc1a 100644 --- a/lib/components/ProductWidget.dart +++ b/lib/components/ProductWidget.dart @@ -14,24 +14,51 @@ class ProductWidget extends StatelessWidget { double height = MediaQuery.of(context).size.height/1.2; double fontSize = (height/24).round().toDouble(); - return new GestureDetector( - onTap: (){ - Navigator.of(context).push(MaterialPageRoute(builder: (context) => new ProductView(product: this.product))); + return GestureDetector( + onTap: () { + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => ProductView(product: this.product))); }, - child: - new Container(padding: EdgeInsets.fromLTRB(20, 20, 20, 5),decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(10))), child: - new Column(mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Center(child: new Hero(tag: "tagHero${this.product.id}", child: new Image.asset(this.product.urlToImage, fit: BoxFit.cover, height: height * 0.20))), - new Container(height: height * 0.25, margin: EdgeInsets.only(top: 10), child: - new Column(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Text("\$${this.product.price}", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: fontSize)), - new Container(margin: EdgeInsets.only(top: 20, bottom: 10), child: new Text("${this.product.title}", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: fontSize * 0.65))), - new Text("${this.product.weight}g", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey, fontSize: fontSize * 0.48)) - ]) - ) - ]) - ) - ); + child: Container( + padding: EdgeInsets.fromLTRB(20, 20, 20, 5), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.all(Radius.circular(10))), + child: Column( + mainAxisSize: MainAxisSize.max, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Center( + child: Hero( + tag: "tagHero${this.product.id}", + child: Image.asset(this.product.urlToImage, + fit: BoxFit.cover, height: height * 0.20))), + Container( + height: height * 0.25, + margin: EdgeInsets.only(top: 10), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text("\$${this.product.price}", + style: TextStyle( + fontWeight: FontWeight.bold, + color: Colors.black, + fontSize: fontSize)), + Container( + margin: EdgeInsets.only(top: 20, bottom: 10), + child: Text("${this.product.title}", + style: TextStyle( + fontWeight: FontWeight.bold, + color: Colors.black, + fontSize: fontSize * 0.65))), + Text("${this.product.weight}g", + style: TextStyle( + fontWeight: FontWeight.bold, + color: Colors.grey, + fontSize: fontSize * 0.48)) + ])) + ]))); } } \ No newline at end of file diff --git a/lib/models/Cart.dart b/lib/models/Cart.dart index 123794f..733e973 100644 --- a/lib/models/Cart.dart +++ b/lib/models/Cart.dart @@ -5,7 +5,7 @@ class Cart{ List _orders; Cart(){ - _orders = new List(); + _orders = List(); } void addOrder(Order order){ diff --git a/lib/repositories/ProductsRepository.dart b/lib/repositories/ProductsRepository.dart index 8d445f0..e624908 100644 --- a/lib/repositories/ProductsRepository.dart +++ b/lib/repositories/ProductsRepository.dart @@ -5,13 +5,20 @@ class ProductsRepository{ List fetchAllProducts() { return [ - new Product("assets/images/spelt_noodles.png", "Biona Organic Spelt Noodles", 2.99, 250, 0), - new Product("assets/images/spelt_italian.png", "Biona Organic Spelt Fusili Brown", 2.35, 500, 1), - new Product("assets/images/spelt_spaghetti.png", "Biona Organic Whole Spelt Spaghetti", 2.35, 500, 2), - new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 3), - new Product("assets/images/spelt_penne.png", "Biona Organic Whole Spelt Penne", 2.35, 500, 4), - new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 5), - new Product("assets/images/spelt_fusilli.png", "Biona Organic Spelt Fusilli Tricolore", 1.99, 250, 6), + Product("assets/images/spelt_noodles.png", "Biona Organic Spelt Noodles", + 2.99, 250, 0), + Product("assets/images/spelt_italian.png", + "Biona Organic Spelt Fusili Brown", 2.35, 500, 1), + Product("assets/images/spelt_spaghetti.png", + "Biona Organic Whole Spelt Spaghetti", 2.35, 500, 2), + Product("assets/images/spelt_tagliatelle.png", + "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 3), + Product("assets/images/spelt_penne.png", + "Biona Organic Whole Spelt Penne", 2.35, 500, 4), + Product("assets/images/spelt_tagliatelle.png", + "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 5), + Product("assets/images/spelt_fusilli.png", + "Biona Organic Spelt Fusilli Tricolore", 1.99, 250, 6), ]; } diff --git a/lib/views/Home.dart b/lib/views/Home.dart index 70aa719..c4720cd 100644 --- a/lib/views/Home.dart +++ b/lib/views/Home.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:grocery_shop_flutter/bloc/CartBloc.dart'; -import 'package:grocery_shop_flutter/components/GridShop.dart'; import 'package:grocery_shop_flutter/components/CartManager.dart'; +import 'package:grocery_shop_flutter/components/GridShop.dart'; class MyHomePage extends StatefulWidget { @override @@ -13,41 +13,49 @@ class _MyHomePageState extends State { bool _showCart = false; CartBloc _cartBloc; - ScrollController _scrollController = new ScrollController(); + ScrollController _scrollController = ScrollController(); @override - initState(){ - _scrollController = new ScrollController(); - _cartBloc = new CartBloc(); + initState() { + _scrollController = ScrollController(); + _cartBloc = CartBloc(); super.initState(); } @override Widget build(BuildContext context) { - return Scaffold(backgroundColor: Colors.black, body: - new Stack(children: [ - new CustomScrollView(physics: NeverScrollableScrollPhysics(), controller: _scrollController, slivers: [ - new SliverToBoxAdapter(child: - new GridShop() - ), - new SliverToBoxAdapter(child: - new CartManager() - ), - ]), - new Align(alignment: Alignment.bottomRight, child: - new Container(margin: EdgeInsets.only(right: 10, bottom: 10),child: - new FloatingActionButton(onPressed: (){ - if(_showCart) - _scrollController.animateTo(_scrollController.position.minScrollExtent, curve: Curves.fastOutSlowIn, duration: Duration(seconds: 2)); - else - _scrollController.animateTo(_scrollController.position.maxScrollExtent, curve: Curves.fastOutSlowIn, duration: Duration(seconds: 2)); - - setState(() { - _showCart = !_showCart; - }); - - }, backgroundColor: Colors.amber, child: new Icon(_showCart ? Icons.close : Icons.shopping_cart)) - ) + return Scaffold(backgroundColor: Colors.black, body: Stack(children: [ + CustomScrollView( + physics: NeverScrollableScrollPhysics(), + controller: _scrollController, + slivers: [ + SliverToBoxAdapter(child: GridShop()), + SliverToBoxAdapter(child: CartManager()), + ]), + Align( + alignment: Alignment.bottomRight, + child: Container( + margin: EdgeInsets.only(right: 10, bottom: 10), + child: FloatingActionButton( + onPressed: () { + if (_showCart) + _scrollController.animateTo( + _scrollController.position.minScrollExtent, + curve: Curves.fastOutSlowIn, + duration: Duration(seconds: 2)); + else + _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + curve: Curves.fastOutSlowIn, + duration: Duration(seconds: 2)); + + setState(() { + _showCart = !_showCart; + }); + }, + backgroundColor: Colors.amber, + child: + Icon(_showCart ? Icons.close : Icons.shopping_cart))) ) ]) ); diff --git a/lib/views/ProductView.dart b/lib/views/ProductView.dart index 504ba0f..244179f 100644 --- a/lib/views/ProductView.dart +++ b/lib/views/ProductView.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:grocery_shop_flutter/models/Product.dart'; import 'package:grocery_shop_flutter/bloc/CartBloc.dart'; import 'package:grocery_shop_flutter/models/Order.dart'; +import 'package:grocery_shop_flutter/models/Product.dart'; class ProductView extends StatefulWidget{ @@ -9,22 +9,22 @@ class ProductView extends StatefulWidget{ ProductView({Key key, this.product}) : super(key:key); @override - _ProductView createState() => new _ProductView(); + _ProductView createState() => _ProductView(); } class _ProductView extends State { - final CartBloc _cartBloc = new CartBloc(); + final CartBloc _cartBloc = CartBloc(); int _quantity = 1; - void _increment(){ + void _increment() { setState(() { _quantity++; }); } - void _decrement(){ - if(_quantity>1) { + void _decrement() { + if (_quantity > 1) { setState(() { _quantity--; }); @@ -33,54 +33,113 @@ class _ProductView extends State { @override Widget build(BuildContext context) { - return new Scaffold(backgroundColor: Colors.white, appBar: new AppBar(backgroundColor: Colors.white, elevation: 0, iconTheme: IconThemeData(color: Colors.black)), body: - new SafeArea(child: - new Column(children: [ - new Container(padding: EdgeInsets.symmetric(horizontal: 20), height: MediaQuery.of(context).size.height*0.73, child: - new SingleChildScrollView(child: - new Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Center(child: - new StreamBuilder(initialData: null, stream: _cartBloc.observableLastOrder, builder: (context, AsyncSnapshot snapshot){ - String tag = snapshot.data == null ? "tagHero${widget.product.id}" : "tagHeroOrder${snapshot.data.id}"; - return new Hero(tag: tag, child: - new Image.asset(widget.product.urlToImage, fit: BoxFit.cover, height: MediaQuery.of(context).size.height*0.4) - ); - }) - ), - new Container(margin: EdgeInsets.only(top: 20), child: - new Text(widget.product.title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 40, color: Colors.black)), - ), - new Container(margin: EdgeInsets.only(top: 10), child: - new Text("${widget.product.weight}g", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20, color: Colors.grey)), - ), - new Container(margin: EdgeInsets.only(top: 20), child: - new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - new Container(padding: EdgeInsets.all(10), decoration: BoxDecoration(border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(50)), child: - new Row(children: [ - new InkWell( - child: new Icon(Icons.remove, size: 15,), - onTap: _decrement, - ), - new Padding(padding: EdgeInsets.symmetric(horizontal: 20), child: new Text(_quantity.toString(), style: TextStyle(fontSize: 20))), - new InkWell( - child: new Icon(Icons.add, size: 15,), - onTap: _increment, - ), - ]), - ), - new Text("\$${(widget.product.price*_quantity).toStringAsFixed(2)}", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 35, color: Colors.black)), - ]) - ), - new Container(margin: EdgeInsets.only(top: 40, bottom: 40), child: - new Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Text("About the product:", style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20)), - new Padding(padding: EdgeInsets.only(top: 10), child: new Text(widget.product.about, style: TextStyle(color: Colors.grey, fontSize: 18))) - ]) - ) - ]) + return Scaffold( + backgroundColor: Colors.white, + appBar: AppBar( + backgroundColor: Colors.white, + elevation: 0, + iconTheme: IconThemeData(color: Colors.black)), + body: SafeArea( + child: Column(children: [ + Container( + padding: EdgeInsets.symmetric(horizontal: 20), + height: MediaQuery.of(context).size.height * 0.73, + child: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Center( + child: StreamBuilder( + initialData: null, + stream: _cartBloc.observableLastOrder, + builder: (context, AsyncSnapshot snapshot) { + String tag = snapshot.data == null + ? "tagHero${widget.product.id}" + : "tagHeroOrder${snapshot.data.id}"; + return Hero( + tag: tag, + child: Image.asset(widget.product.urlToImage, + fit: BoxFit.cover, + height: + MediaQuery.of(context).size.height * + 0.4)); + })), + Container( + margin: EdgeInsets.only(top: 20), + child: Text(widget.product.title, + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 40, + color: Colors.black)), + ), + Container( + margin: EdgeInsets.only(top: 10), + child: Text("${widget.product.weight}g", + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 20, + color: Colors.grey)), + ), + Container( + margin: EdgeInsets.only(top: 20), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Container( + padding: EdgeInsets.all(10), + decoration: BoxDecoration( + border: Border.all(color: Colors.grey), + borderRadius: BorderRadius.circular(50)), + child: Row(children: [ + InkWell( + child: Icon( + Icons.remove, + size: 15, + ), + onTap: _decrement, + ), + Padding( + padding: + EdgeInsets.symmetric(horizontal: 20), + child: Text(_quantity.toString(), + style: TextStyle(fontSize: 20))), + InkWell( + child: Icon( + Icons.add, + size: 15, + ), + onTap: _increment, + ), + ]), + ), + Text( + "\$${(widget.product.price * _quantity).toStringAsFixed(2)}", + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 35, + color: Colors.black)), + ])), + Container( + margin: EdgeInsets.only(top: 40, bottom: 40), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text("About the product:", + style: TextStyle( + color: Colors.black, + fontWeight: FontWeight.bold, + fontSize: 20)), + Padding( + padding: EdgeInsets.only(top: 10), + child: Text(widget.product.about, + style: TextStyle( + color: Colors.grey, fontSize: 18))) + ])) + ]) ) ), - new Container(decoration: BoxDecoration(boxShadow: [ + Container( + decoration: BoxDecoration(boxShadow: [ BoxShadow( color: Colors.white, blurRadius: 30.0, // has the effect of softening the shadow @@ -90,19 +149,30 @@ class _ProductView extends State { -20.0, // vertical, move down 10 ), ) - ]), padding: EdgeInsets.symmetric(horizontal: 20), height: MediaQuery.of(context).size.height*0.1, child: - new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - new FlatButton.icon(onPressed: (){}, icon: new Icon(Icons.favorite_border), label: new Text("")), - new SizedBox(width: MediaQuery.of(context).size.width * 0.6, child: - new RaisedButton(color: Colors.amber, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(60)), padding: EdgeInsets.all(20), - onPressed: (){ - _cartBloc.addOrderToCart(widget.product, _quantity); - Navigator.of(context).pop(); - }, - child: new Text("Add to cart", style: TextStyle(fontWeight: FontWeight.bold)) - ) - ) - ]), + ]), + padding: EdgeInsets.symmetric(horizontal: 20), + height: MediaQuery.of(context).size.height * 0.1, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + FlatButton.icon( + onPressed: () {}, + icon: Icon(Icons.favorite_border), + label: Text("")), + SizedBox( + width: MediaQuery.of(context).size.width * 0.6, + child: RaisedButton( + color: Colors.amber, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(60)), + padding: EdgeInsets.all(20), + onPressed: () { + _cartBloc.addOrderToCart(widget.product, _quantity); + Navigator.of(context).pop(); + }, + child: Text("Add to cart", + style: TextStyle(fontWeight: FontWeight.bold)))) + ]), ) ]) )