Skip to content

Commit

Permalink
adding dismissible widget for remove item in shopping list and update…
Browse files Browse the repository at this point in the history
… total price
  • Loading branch information
amirhossein-jahangiri committed Aug 14, 2021
1 parent 391b970 commit 86a19ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
9 changes: 9 additions & 0 deletions lib/providers/cart.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../models/cart_item.dart';

Expand All @@ -19,6 +22,12 @@ class Cart with ChangeNotifier {
return total;
}

void removeItem(String productId){
log('remove item method $productId and items length id ${_items!.length}');
_items!.remove(productId);
notifyListeners();
}

void addItem(String productId, String title, double price) {
if (_items!.containsKey(productId)) {
_items!.update(
Expand Down
9 changes: 6 additions & 3 deletions lib/screens/cart_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class CartScreen extends StatelessWidget {
Text('Total:', style: TextStyle(fontSize: 20.0)),
Spacer(),
Chip(
label: Text(
'\$ ${cart.totalAmount}',
style: TextStyle(color: Theme.of(context).primaryTextTheme.headline6!.color),
label: Consumer<Cart>(
builder: (context, cart, child) => Text(
'\$ ${cart.totalAmount.toStringAsFixed(2)}',
style: TextStyle(color: Theme.of(context).primaryTextTheme.headline6!.color),
),
),
backgroundColor: Theme.of(context).primaryColor,
),
Expand All @@ -43,6 +45,7 @@ class CartScreen extends StatelessWidget {
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (context, index) => CartItem(
productId: cart.items.keys.toList()[index],
id: cart.items.values.toList()[index].id,
title: cart.items.values.toList()[index].title,
price: cart.items.values.toList()[index].price,
Expand Down
49 changes: 37 additions & 12 deletions lib/widgets/cart_item.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/cart.dart';

class CartItem extends StatelessWidget {
final String? productId;
final String? id;
final String? title;
final double? price;
final int? quantitiy;

const CartItem({
this.productId,
this.id,
this.title,
this.price,
Expand All @@ -15,18 +22,36 @@ class CartItem extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 15.0 ,vertical: 4.0),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
leading: CircleAvatar(child: Padding(
padding: const EdgeInsets.all(5.0),
child: FittedBox(child: Text('\$ $price')),
)),
title: Text(title!),
subtitle: Text('Total: \$ ${(price! * quantitiy!)}'),
trailing: Text('$quantitiy x '),
//final cart = Provider.of<Cart>(context , listen: false);
return Dismissible(
key: ValueKey(id),
direction: DismissDirection.endToStart,
onDismissed: (direction){
log('product id is ${productId!}');
Provider.of<Cart>(context , listen: false).removeItem(productId!);
},
background: Container(
margin: const EdgeInsets.symmetric(
vertical: 15.0,horizontal: 4.0,
),
padding: const EdgeInsets.only(right: 20.0),
color: Theme.of(context).errorColor,
alignment: Alignment.centerRight,
child: Icon(Icons.delete , color: Colors.white, size: 40,),
),
child: Card(
margin: const EdgeInsets.symmetric(horizontal: 15.0 ,vertical: 4.0),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
leading: CircleAvatar(child: Padding(
padding: const EdgeInsets.all(5.0),
child: FittedBox(child: Text('\$ $price')),
)),
title: Text(title!),
subtitle: Text('Total: \$ ${(price! * quantitiy!)}'),
trailing: Text('$quantitiy x '),
),
),
),
);
Expand Down

0 comments on commit 86a19ff

Please sign in to comment.