selectFilterResults method

void selectFilterResults({
  1. required BuildContext context,
  2. required String paymentId,
  3. required String paymentStatus,
  4. required String amount,
  5. required String productName,
})

Implementation

void selectFilterResults(
    {required BuildContext context,
    required String paymentId,
    required String paymentStatus,
    required String amount,
    required String productName}) {
  Navigator.pop(context);

  items.clear();
  bool isSearchByPayId = paymentId.isNotEmpty;
  bool isSearchByStatus = paymentStatus.isNotEmpty;
  bool isSearchByAmount = amount.isNotEmpty;
  bool isSearchByProdName = productName.isNotEmpty;

  RxList<OrderData> sortList = <OrderData>[].obs;
  RxList<OrderData> tempSortList = <OrderData>[].obs;

  for (OrderData order in mainItemList) {
    if (isSearchByAmount &&
            double.parse(order.amount!) == double.parse(amount) ||
        isSearchByPayId &&
            order.uuid!.toLowerCase().contains(paymentId.toLowerCase()) ||
        isSearchByStatus &&
            order.status!
                .toLowerCase()
                .contains(paymentStatus.toLowerCase())) {
      sortList.add(order);
    } else if (isSearchByProdName &&
        order.products != null &&
        order.products!.length > 0) {
      for (ProductData prod in order.products!) {
        if (prod.name!.toLowerCase().contains(productName.toLowerCase())) {
          sortList.add(order);
          break;
        }
      }
    }
  }

  tempSortList.addAll(sortList);

  ///Sort by Payment ID
  if (isSearchByPayId) {
    for (OrderData order in sortList) {
      if (!order.uuid!.toLowerCase().contains(paymentId.toLowerCase())) {
        tempSortList.remove(order);
      }
    }
  }

  ///Sort by Amount
  if (isSearchByAmount) {
    for (OrderData order in sortList) {
      double amnt = double.parse(order.amount!);
      double searchAmnt = double.parse(amount);
      if (amnt != searchAmnt) {
        tempSortList.remove(order);
      }
    }
  }

  ///Sort by Payment Status
  if (isSearchByStatus) {
    for (OrderData order in sortList) {
      if (!order.status!
          .toLowerCase()
          .contains(paymentStatus.toLowerCase())) {
        tempSortList.remove(order);
      }
    }
  }

  ///Sort by Product Name
  if (isSearchByProdName) {
    for (OrderData order in sortList) {
      bool found = false;
      if (order.products != null && order.products!.length > 0) {
        for (ProductData prod in order.products!) {
          if (prod.name!.toLowerCase().contains(productName.toLowerCase())) {
            found = true;
            break;
          }
        }
        if (found) {
          break;
        }
      }
      if (!found) {
        tempSortList.remove(order);
      }
    }
  }
  items.addAll(tempSortList);
  update();
}