本文最后更新于 2024年8月19日 下午
ListView
默认构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ListView({ Axis scrollDirection = Axis.vertical, bool reverse = false, ScrollController? controller, bool? primary, ScrollPhysics? physics, EdgeInsetsGeometry? padding, double? itemExtent, Widget? prototypeItem, bool shrinkWrap = false, bool addAutomaticKeepAlives = true, bool addRepaintBoundaries = true, double? cacheExtent, List<Widget> children = const <Widget>[], })
|
ListView.builder
ListView.builder适合列表项比较多或者列表项不确定的情况,下面看一下ListView.builder的核心参数列表:
1 2 3 4 5 6 7
| ListView.builder( itemCount: 100, itemExtent: 50.0, itemBuilder: (BuildContext context, int index) { return ListTile(title: Text("$index")); } );
|
ListView.separated
ListView.separated可以在生成的列表项之间添加一个分割组件,它比ListView.builder多了一个separatorBuilder参数,该参数是一个分割组件生成器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Widget build(BuildContext context) { Widget divider1 = const Divider( color: Colors.blue, ); Widget divider2 = const Divider(color: Colors.green); return ListView.separated( itemBuilder: (BuildContext context, int index) { return ListTile(title: Text("$index")); }, separatorBuilder: (BuildContext context, int index) { return index % 2 == 0 ? divider1 : divider2; }, itemCount: 100 ); }
|