Skip to content

Showing only an even number of products and no products if exist only one or an odd number of products. #9

@NikolinNgjela

Description

@NikolinNgjela

Hi @rohan20 , very useful repo about the above use case.

I’m using a similar use case only a brief change on products_list_page if I'm not wrong.
The current condition will show only an even number of products but if the category have only one product or an odd number of products like 9,11 ect… will lose one product.

I’ve add the following conditions as a workaround in order to show all products inside the condition else if (index % 2 == 0).

} else if (index % 2 == 0) {


  // if we have odd number of products 7,9,11 ect show the last one
  if (index == model.getProductsCount() - 1) {
    return ProductsListItem(
      product1: model.productsList[index],
      product2: null,
    );

  // if we have only one product
  } else if ( model.getProductsCount() == 1) {
    return ProductsListItem(
      product1: model.productsList[0],
      product2: null,
    );
  } else {
    //2nd, 4th, 6th.. index would contain nothing since this would
    //be handled by the odd indexes where the row contains 2 items
    return Container();
  }

}

Thanks for sharing the repo! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions