简体   繁体   English

Flutter - 如何对列表进行勾选框筛选以缩小搜索范围

[英]Flutter - How to do a tick box filter for a list to narrow down search

I am very new to flutter and still learning. 我很陌生,还在学习。 I am struggle on how to create a filter for a list of cars. 我很擅长如何为汽车列表创建过滤器。 Eg tick a box to only show red cars. 例如,勾选一个仅显示红色汽车的方框。

I do a little bit of searching but I can't seem to figure out how to do it. 我做了一些搜索,但我似乎无法弄清楚如何做到这一点。 I did see a "where" method but struggling to make sence of it. 我确实看到了“哪里”的方法,但努力使它恍然大悟。

What is the the best way of doing this and can you please point me in the right direction. 这样做的最佳方式是什么,请你指出正确的方向。 Can't get my head about this one. 无法理解这一点。

So to create a filter for your list. 所以要为你的列表创建一个过滤器。 Lets assume that you have a class for your car: 让我们假设您有一个汽车课程:

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

And lets say you have some car objects: 让我们说你有一些汽车对象:

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

Now you create a statefulWidget for your list: 现在,您为列表创建一个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."
           ),
         ),
       );
     }
}

So, what you are trying to do can be achieved by this. 所以,你要做的就是通过这个来实现。 Now all you have to do is to do it dynamic, show this list on your page. 现在,您所要做的就是动态完成,在页面上显示此列表。 Remember the more your struggle the more you learn. 记住,你学到的越多,你的斗争就越多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM