carrito_page.dart 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/carrito_provider.dart';

class CarritoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text('Mi carrito de compras'),
          backgroundColor: Colors.amber
      ),

      body: Consumer<CarritoProvider>(
        builder: (context, carritoProvider, child) {
          final carrito = carritoProvider.carrito;
          return ListView.builder(
            itemCount: carrito.length,
            itemBuilder: (context, index) {
              final item = carrito[index];
              return Card(
                child: ListTile(
                  leading:Image.asset('assets/img/logo_visorus.jpg', height: 15), // Imagen del artículo
                  title: Text('${item.articulo.nombre}'),// TITULO DEL ARTICULO
                  subtitle: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          //BOTON DE ELIMINAR -
                          IconButton(
                            icon: Icon(Icons.remove,color: Colors.red),
                            onPressed: () {
                              carritoProvider.updateQuantity(item.articulo, item.cantidad - 1);
                            },
                          ),
                          Text('${item.cantidad}'),
                          //AGREGAR + PRODUCTO
                          IconButton(
                            icon: Icon(Icons.add, color: Colors.blue),
                            onPressed: () {
                              carritoProvider.updateQuantity(item.articulo, item.cantidad + 1);
                            },
                          ),
                          //CALCULAR PERCIO TOTAL DEL PRODUCTO
                          Text('Subtotal: \$${(item.precio * item.cantidad).toStringAsFixed(2)}'),
                        ],
                      ),
                    ],
                  ),
                  trailing: IconButton(
                    icon: Icon(Icons.delete, color: Colors.red),
                    onPressed: () {
                      carritoProvider.removeFromCart(item.articulo);
                    },
                  ),
                ),
              );
            },
          );
        },
      ),
      bottomNavigationBar: Consumer<CarritoProvider>(
        builder: (context, carritoProvider, child) {
          return Container(
            padding: EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Total: \$${carritoProvider.totalAmount.toStringAsFixed(2)}',
                  style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                ),
                ElevatedButton(
                  onPressed: () {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('Compra confirmada')),
                    );
                  },
                  child: Text('COMPRAR'),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}