Flutter-如何为列表打勾复选框以缩小搜索范围

时间:2019-03-30 18:04:43

标签: list dart flutter filtering

我是新手,仍会继续学习。我在如何为汽车列表创建过滤器上感到挣扎。例如。勾选一个框仅显示红色汽车。

我做了一些搜索,但似乎无法弄清楚该怎么做。我确实看到了“ where”方法,但是却难以理解。

执行此操作的最佳方法是什么,能否请您指出正确的方向?我对此一无所知。

1 个答案:

答案 0 :(得分:0)

因此为您的列表创建一个过滤器。假设您有汽车课:

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."
           ),
         ),
       );
     }
}

因此,您可以尝试完成此操作。现在,您要做的就是动态地执行此操作,并在页面上显示此列表。记住,您的斗争越多,您就会学到更多。