我有一个清单让我们说“ProductList”
我有一个过滤条与倾斜‘颜色’和‘形状’与按钮红色,绿色,方形和圆形。
颜色:红绿
形状:方形圆
第一类-彩色
如果我点击红色,'ProductList‘必须过滤红色的颜色。
firstFilteredList = ProductList.where((p) => p.color == "red");
现在,如果我点击绿色,'ProductList‘必须过滤红色和绿色。
firstFilteredList = ProductList.where((p) => p.color == "red" || p.color == "green");
第2st类-形状
现在,如果我点击正方形,'firstFilteredList‘必须过滤的方形形状。
secondFilteredList = firstFilteredList.where((p) => p.shape == "square");
现在,如果我点击圆形,'firstFilteredList‘必须过滤为方形和圆形。
secondFilteredList = firstFilteredList.where((p) => p.shape == "square" || p.shape == "round");
我需要删除那些过滤器,如果按钮是切换关闭。
为了简单起见,我刚才提到了两个过滤器类别--颜色和形状。可能有6-7个过滤器类别。因此,解决方案必须是可伸缩的。
有人能帮我解决这个问题吗?
编辑: ProductList是CategorizedProduct的一个元素。我要退单。
class CategorizedProduct {
String id;
String title;
List<Product> product;
String selectedVariant;
}
class Product {
String color;
String id;
bool shapeRectangle;
bool shapeRound;
bool shapeSquare;
}发布于 2020-09-10 07:36:49
您可以将查询放入列表中,而不是执行contains()。
这是一个省道的例子。
enum ProductColor { RED, GREEN, BLUE }
enum Weight { HEAVY, NORMAL, LIGHT }
enum Shape { SQUARE, ROUND, TRIANGLE }
class Product {
final String name;
final ProductColor color;
final Weight weight;
final Shape shape;
Product(this.name, this.color, this.weight, this.shape);
@override
String toString() {
return '$name $color $weight $shape';
}
}
class Query {
final List<ProductColor> color;
final List<Weight> weight;
final List<Shape> shape;
Query({this.color, this.weight, this.shape});
}
List<Product> filter(List<Product> products, Query query) {
return products
.where((product) =>
(query.color == null || query.color.contains(product.color)) &&
(query.weight == null || query.weight.contains(product.weight)) &&
(query.shape == null || query.shape.contains(product.shape)))
.toList();
}
void main() {
List<Product> _productList = [
Product("P1", ProductColor.BLUE, Weight.HEAVY, Shape.TRIANGLE),
Product("P2", ProductColor.GREEN, Weight.NORMAL, Shape.ROUND),
Product("P3", ProductColor.RED, Weight.LIGHT, Shape.ROUND),
Product("P4", ProductColor.GREEN, Weight.NORMAL, Shape.SQUARE),
];
Query _query = Query(color: [ProductColor.GREEN], shape: [Shape.ROUND]);
List<Product> results = filter(_productList, _query);
results.forEach(print);
}发布于 2020-09-10 07:01:03
您希望在筛选器选择中使用onPressed(),对on或off使用布尔值(筛选器是否选中)。就像这样:
//red color filter button
...
onPressed(
redFilter = !redFilter; //on if off, off if on...
)然后可以使用布尔值列表。
bool redFilter = false;
bool greenFilter = false;
bool squareFilter = false;
bool roundFilter = false;
List<bool> filters = [redFilter, greenFilter, squareFilter, roundFilter];https://stackoverflow.com/questions/63823521
复制相似问题