How to increase items Quantity in E-Commerce Cart using Provider in Flutter

5

I want to increase the quantity of product when I click on the add button in the productview or add to cart multiple time but I do not increase its quantity remain the same, someone helps me to solve this problem. How can I give an index to the items?

I am using Provider for state management

ProviderSpace.dart

import 'dart:collection';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

class AddItem extends ChangeNotifier {
  List<Cart> _addNewData = [];


  int get newDataCount {
    return _addNewData.length;
  }

  UnmodifiableListView<Cart> get tasks {
    return UnmodifiableListView(_addNewData);
  }

  void addCardManager(String itemName, String assetName, int itemRate,
      int quantity, String type) {
    final newAddItem = Cart(
      name: itemName,
      type: type,
      rate: itemRate,
      asset: assetName,
    );
    _addNewData.add(newAddItem);
    notifyListeners();
  }

  void updateTask(Cart task) {
    task.toggleDone();
    notifyListeners();
  }

  void decrease(Cart task) {
    task.decreaseDown();
    notifyListeners();
  }

  void removeCard(Cart task) {
    _addNewData.remove(task);
    notifyListeners();
  }
}

class Cart {
  final String name;
  final String asset;
  final String type;
  final int rate;
  int quantity;

  Cart({
    this.name,
    this.asset,
    this.type,
    this.rate,
    this.quantity = 1,
  });

  void toggleDone() {
    quantity++;
  }

  void decreaseDown() {
    quantity == 0 ? 0 : quantity--;
  }

}

The code where I need to update the value

          changeButtonOnTap
                ? Container(
                    width: 80,
                    height: 25,
                    child: FlatButton(
                      onPressed: () {
                        Provider.of<AddItem>(context, listen: false)
                            .addCardManager(widget.name, widget.name,
                                widget.mrp, widget.quantity, widget.type);
                        setState(() {
                          changeButtonOnTap = false;
                        });
                      },
                      color: Color(0xffFF6200),
                      child: Text("Add",
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 11,
                            color: Color(0xffffffff),
                          )),
                    ),
                  )
                : Container(
                    height: 25.00,
                    width: 80,
                    decoration: BoxDecoration(
                      color: Color(0xffFF6200),
                      borderRadius: BorderRadius.circular(30.00),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        GestureDetector(
                            onTap: () {                                          <-----here i need to make change
                              Provider.of<AddItem>(context, listen: false)
                                  .updateTask(Provider.of<AddItem>(context,
                                          listen: false)
                                      .tasks[0]);
                              setState(() {
                                value++;
                                print(Provider.of<AddItem>(context,
                                        listen: false)
                                    .newDataCount);
                              });
                            },
                            child: Icon(Icons.add_circle,
                                color: Colors.white)),
                        Text(
                          value.toString(),
                          style: TextStyle(color: Colors.white),
                        ),
                        GestureDetector(
                          onTap: () {
                            setState(() {
                              value >= 2 ? value-- : null;
                            });
                          },
                          child: Icon(
                            Icons.do_not_disturb_on,
                            color: Colors.white,
                          ),
                        )
                      ],
                    ),
                  )
flutter
dart
flutter-provider
asked on Stack Overflow Jun 24, 2020 by Asbah Riyas

1 Answer

3

The below code is used to increase the quantity of the item (if the same item added)

  void addCardManager(String itemName, String assetName, int itemRate,
    int ogRate, int quantity, String type) {
    final newAddItem = Cart(
      name: itemName,
      type: type,
      rate: itemRate,
      ogPrice: ogRate,
      asset: assetName,
    );
    if (_addNewData.length != 0) {
      bool isFound = false;
      for (int itemcount = 0; itemcount < newDataCount; itemcount++) {
        if (_addNewData[itemcount].name == newAddItem.name) {
          print("addCard");
          isFound = true;
          _addNewData[itemcount].toggleDone();
          notifyListeners();
          break;
        }
      }
      if (!isFound) {
        _addNewData.add(newAddItem);
        notifyListeners();
      }
    } else {
      _addNewData.add(newAddItem);
      notifyListeners();
    }
  }

  void removeCardManager(String itemName, String assetName, int itemRate,
      int quantity, String type) {
    final newAddItem = Cart(
      name: itemName,
      type: type,
      rate: itemRate,
      asset: assetName,
    );
    print(_addNewData);
    if (_addNewData.length != 0) {
      bool isFound = false;

      for (int itemcount = 0; itemcount < newDataCount; itemcount++) {
        if (_addNewData[itemcount].name == newAddItem.name) {
          print("RemoveCard");
          isFound = true;
          decrease(_addNewData[itemcount]);
          notifyListeners();
          break;
        }
      }

    }
  }

for reference, I will add the full code linked to the above code

  void updateTask(Cart task) {
    task.toggleDone();
    notifyListeners();
  }

  void decrease(Cart task) {
    if (task.quantity == 1) {
      removeCard(task);
    }
    task.decreaseDown();
    notifyListeners();
  }

  void removeCard(Cart task) {
    _addNewData.remove(task);
    notifyListeners();
  }
}

class Cart {
  final String name;
  final String asset;
  final int ogPrice;
  final String type;
  final int rate;
  int quantity;

  Cart({
    this.name,
    this.ogPrice,
    this.asset,
    this.type,
    this.rate,
    this.quantity = 1,
  });

  void toggleDone() {
    quantity++;
  }

  void decreaseDown() {
    quantity == 0 ? 0 : quantity--;
  }
}
answered on Stack Overflow Sep 8, 2020 by Asbah Riyas • edited Nov 14, 2020 by Inconnu

User contributions licensed under CC BY-SA 3.0