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

3

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

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0