I want to get the sum total of the price of all the products in a list of cards with data from firestore

Viewed 32

I want to get the sum total of the price of all the products in a list of cards with data from firestore and i don't how to do that on flutter.

This is the where i show the cards and where i want to show the total price too:

  class ShopListPage extends StatelessWidget {
  const ShopListPage({super.key});

  Stream<List<ShopList>> readProducts() => FirebaseFirestore.instance
      .collection('shoplist')
      .orderBy('price')
      .snapshots()
      .map((snapshot) =>
          snapshot.docs.map((doc) => ShopList.fromJson(doc.data())).toList());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.background,
      body: StreamBuilder<List<ShopList>>(
          stream: readProducts(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Algo ha ocurrido! ${snapshot.error}');
            } else if (snapshot.hasData) {
              final shoplist = snapshot.data!;
              return ListView(
                  children: shoplist
                      .map((p) => BuildListCards(shopList: p))
                      .toList());
            } else {
              return const Center(child: CircularProgressIndicator());
            }
          }),
      floatingActionButton: FloatingActionButton(
        child: const Icon(
          Icons.paid,
        ),
        onPressed: () {},
      ),
    );
  }
}

The BuildListCard Class: here is where i show the price individually

  class BuildListCards extends StatelessWidget {
  final ShopList shopList;
  const BuildListCards({Key? key, required this.shopList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(8),
      clipBehavior: Clip.hardEdge,
      child: ConstrainedBox(
        constraints: const BoxConstraints(maxHeight: 80),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Image(
              image: NetworkImage(shopList.imageUrl),
              fit: BoxFit.cover,
            ),
            Padding(
              padding: const EdgeInsets.all(8),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(shopList.name),
                  Text('Precio: ${shopList.price}\$'),
                  Text('Cantidad: ${shopList.quantity}')
                ],
              ),
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.fromLTRB(0, 25, 8, 0),
              child: ElevatedButton(
                style: ButtonStyle(
                  foregroundColor: MaterialStateProperty.all<Color>(
                    const Color(0xFFFFFFFF),
                  ),
                  backgroundColor: MaterialStateProperty.all<Color>(
                    const Color(0xFF6750A4),
                  ),
                ),
                onPressed: () => showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (BuildContext context) => AlertDialog(
                        title: Text(
                          '¿Desea eliminar el producto ${shopList.name} de la base de datos?',
                          textAlign: TextAlign.center,
                        ),
                        actions: <Widget>[
                          TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              child: const Text('Cancelar')),
                          TextButton(
                              onPressed: () {
                                final docShopList = FirebaseFirestore.instance
                                    .collection('shoplist')
                                    .doc(shopList.id);

                                docShopList.delete();
                                Navigator.pop(context);
                              },
                              child: const Text('Eliminar')),
                        ],
                      ),
                    ),
                child: const Text(
                  'Eliminar',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

i want to calculate the sum total of the price, i use shoplist.price to show the price

0 Answers
Related