首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用颤振和飞镖实现多个滤波器

用颤振和飞镖实现多个滤波器
EN

Stack Overflow用户
提问于 2020-09-10 05:46:38
回答 2查看 2.7K关注 0票数 0

我有一个清单让我们说“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的一个元素。我要退单。

代码语言:javascript
复制
class CategorizedProduct {
  String id;
  String title;
  List<Product> product;
  String selectedVariant;
}

class Product {
  String color;
  String id;
  bool shapeRectangle;
  bool shapeRound;
  bool shapeSquare;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-10 07:36:49

您可以将查询放入列表中,而不是执行contains()

这是一个省道的例子。

代码语言:javascript
复制
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);
}
票数 5
EN

Stack Overflow用户

发布于 2020-09-10 07:01:03

您希望在筛选器选择中使用onPressed(),对on或off使用布尔值(筛选器是否选中)。就像这样:

代码语言:javascript
复制
//red color filter button 
...
onPressed(
  redFilter = !redFilter; //on if off, off if on...
)

然后可以使用布尔值列表。

代码语言:javascript
复制
bool redFilter = false; 
bool greenFilter = false;
bool squareFilter = false;
bool roundFilter = false;

List<bool> filters = [redFilter, greenFilter, squareFilter, roundFilter];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63823521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档