【问题标题】:Flutter - How to do a tick box filter for a list to narrow down searchFlutter - 如何对列表进行复选框过滤以缩小搜索范围
【发布时间】:2019-03-31 02:50:16
【问题描述】:

我是 Flutter 的新手,还在学习。我在为如何为汽车列表创建过滤器而苦苦挣扎。例如。勾选一个框以仅显示红色汽车。

我做了一些搜索,但我似乎无法弄清楚如何去做。我确实看到了一种“在哪里”的方法,但很难理解它。

最好的方法是什么,请您指出正确的方向。无法理解这一点。

【问题讨论】:

  • 据我所知,我猜你想创建一个可过滤的列表视图,如果是这样,请参阅medium.com/@thedome6/…
  • 是的,类似但不可搜索。只有一个复选框或下拉菜单。这仍然是最好的方法,只需将其从搜索更改为勾选框吗?
  • 我在答案中写了一些代码。请检查它是否有帮助。

标签: list dart flutter filtering


【解决方案1】:

所以要为您的列表创建一个过滤器。让我们假设您的汽车有一个类:

Class Car {
   final String carName;
   final String color;
   Car({this.carName, this.color});
}

假设你有一些汽车对象:

List<Car> AllCars = [
    new Car(carName: "McQueen",color: "red"),
    new Car(carName: "Mater",color: "rusty"),
];

现在您为您的列表创建一个statefulWidget

class ListPage extends StatefulWidget{
    @override
   _listPageState createState() => new _listPageState();
}

class _listPageState extends State<ListPage> {

    List<Car> _RedCars = null;

    @override
    void initState() {
        super.initState();
        _RedCars = AllCars.where((i) => i.color == "red").toList();
    }

    @override
    Widget build(BuildContext context) {
       return new Scaffold(
         body: new Container(
           child: new Text(
             "Boom!!! You are done. Now build your list on the page."
           ),
         ),
       );
     }
}

因此,您可以通过此来实现您想要做的事情。现在您所要做的就是动态地进行,在您的页面上显示此列表。记住你的斗争越多,你学到的就越多。

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2021-08-20
    • 1970-01-01
    相关资源
    最近更新 更多