Untitled

mail@pastecode.io avatar
unknown
dart
a year ago
5.7 kB
5
Indexable
Never
//cart add method 
  List<ItemInCart> cart = [];
  
  cartAdd(Product p) {
    for (var item in cart) {
      if (item.product.id == p.id) {
        item.quantity++;
      } else {
        cart.add(ItemInCart(product: p, quantity: 1));
      }
    }
  }


//Order page and OrderItem
class OrderPage extends StatelessWidget {
  final DataManager dataManager;
  const OrderPage({super.key, required this.dataManager});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: dataManager.cart.length,
        itemBuilder: (context, index) {
          var item = dataManager.cart[index];
          return OrderItem(
              item: item,
              onRemove: (p) {
                dataManager.cartRemove(p);
              });
        });
  }
}

class OrderItem extends StatelessWidget {
  final ItemInCart item;
  final Function onRemove;
  const OrderItem({Key? key, required this.item, required this.onRemove})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 4,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.only(left: 8.0),
                  child: Text("${item.quantity}x"),
                )),
            Expanded(
                flex: 6,
                child: Text(
                  item.product.name,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                )),
            Expanded(
                flex: 2,
                child: Text(
                    "\$${(item.product.price * item.quantity).toStringAsFixed(2)}")),
            Expanded(
                flex: 1,
                child: IconButton(
                    color: Theme.of(context).primaryColor,
                    onPressed: () {
                      onRemove(item.product);
                    },
                    icon: const Icon(Icons.delete)))
          ],
        ),
      ),
    );
  }
}









//menu page widget
FutureBuilder(
      future: dataManager.getMenu(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          //has data
          var categories = snapshot.data!;
          return ListView.builder(
            itemCount: categories.length,
            itemBuilder: (context, index) {
              return Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(categories[index].name),
                  ),
                  ListView.builder(
                    shrinkWrap: true,
                    physics: const ClampingScrollPhysics(),
                    itemCount: categories[index].products.length,
                    itemBuilder: (context, prodIndex) {
                      var product = categories[index].products[prodIndex];
                      return ProductItem(
                        product: product,
                        onAdd: (addedProduct) {
                          dataManager.cartAdd(addedProduct);
                        },
                      );
                    },
                  )
                ],
              );
            },
          );
        } else {
          //doesn't
          if (snapshot.hasError) {
            return const Text("There have been an error");
          }
          return const CircularProgressIndicator();
        }
      },
    );
    
    
    
    
    
    //ProductItem instance code (in same file)
class ProductItem extends StatelessWidget {
  final Product product;
  final Function onAdd;
  const ProductItem({super.key, required this.product, required this.onAdd});

  @override
  Widget build(context) {
    return Container(
      padding: const EdgeInsets.all(8.0),
      child: Center(
        child: Card(
          elevation: 4,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network(
                product.imageUrl,
                fit: BoxFit.fill,
                width: double.infinity,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          product.name,
                          style: const TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text("\$${product.price}"),
                      ),
                    ],
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 8.0),
                    child: ElevatedButton(
                      onPressed: () {
                        onAdd(product);
                      },
                      child: const Text("Add"),
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}