iOS的tableView使用分组是很方便的,但是flutter就没有那么友好了,
像网上找的解决方法都是很多的固定场景,
每次使用都要写不同的逻辑,而且代码和逻辑耦合很严重,
不知道有没有人弄过,但是我没找到,
于是自己动手造个轮子,通过逻辑处理封装,
使用方法和iOS的基本一致,
当然目前只提供了数据分组、sectionHeader、sectionFooter和列表header和footer。

如上图,红色的是整个列表的header,一般会放个轮播图什么的,
时间是section的header,下面的帖子就是第一个section的row。
使用方法也很简单:
//每次刷新的时候调用
void _reloadData() {
_groupHandler = ListViewGroupHandler(
numberOfSections: _viewModel.sectionCount,
numberOfRowsInSection: (section) {
return _viewModel.countAtSection(section);
},
headerForSection: (section) {
return getHeaderWithText(_viewModel.dateAtSection(section));
},
cellForRowAtIndexPath: (indexPath) {
return getCellWithItem(_viewModel.itemAtIndexPath(indexPath));
},
header: () {
return Container(
height: 50,
width: double.infinity,
color: Colors.redAccent,
);
},
footer: () {
return Container(
height: 50,
width: double.infinity,
color: Colors.greenAccent,
);
},
);
}
child: ListView.builder(
itemCount: _groupHandler.allItemCount,
itemBuilder: (context, index) {
return _groupHandler.cellAtIndex(index);
},
),
没错,ListViewGroupHandler就是我封装的类,是不是很简单。
Git 链接地址:
https://gitee.com/zeng_li_zhi/ListViewGroupHandler
网友评论