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

问题描述 投票:0回答:1

我很陌生,还在学习。我很擅长如何为汽车列表创建过滤器。例如。勾选一个方框,只显示红色汽车。

我做了一些搜索,但我似乎无法弄清楚如何做到这一点。我确实看到了“哪里”的方法,但努力使它恍然大悟。

这样做的最佳方式是什么,请你指出正确的方向。无法理解这一点。

list dart flutter filtering
1个回答
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."
           ),
         ),
       );
     }
}

所以,你要做的就是通过这个来实现。现在,您所要做的就是动态完成,在页面上显示此列表。记住,你学到的越多,你的斗争就越多。

© www.soinside.com 2019 - 2024. All rights reserved.