html listview ios,Flutter-ListView分组(像iOS一样优雅)

本文介绍了一种在Flutter中实现类似iOS tableView分组的方法,通过逻辑处理封装了一个ListViewGroupHandler,支持数据分组、sectionHeader、sectionFooter等功能,并提供了一个简单的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS的tableView使用分组是很方便的,但是flutter就没有那么友好了,

像网上找的解决方法都是很多的固定场景,

每次使用都要写不同的逻辑,而且代码和逻辑耦合很严重,

不知道有没有人弄过,但是我没找到,

于是自己动手造个轮子,通过逻辑处理封装,

使用方法和iOS的基本一致,

当然目前只提供了数据分组、sectionHeader、sectionFooter、无数据占位和列表header和footer。

bac7786bff64

601592793365_.pic.jpg

如上图,红色的是整个列表的header,一般会放个轮播图什么的,

时间是section的header,下面的帖子就是第一个section的row。

使用方法也很简单:

class MyHomePage extends StatefulWidget {

MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override

_MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State {

int _counter = 0;

void _incrementCounter() {

setState(() {

_counter++;

_list.add(_counter.toString());

});

}

ListViewGroupHandler _groupHandler;

List _list = [];

@override

Widget build(BuildContext context) {

_groupHandler = ListViewGroupHandler(

//日志开关

openLog: true,

//分组数量,需要自行根据data设置,默认1

numberOfSections: 1,

//每组cell个数,需要自行根据data设置

numberOfRowsInSection: (section) => _list.length,

//indexPath: IndexPath(section,row,index)

cellForRowAtIndexPath: (indexPath) => Text(_list[indexPath.row]),

//头部

header: () => Text("header"),

//占位Placeholder

emptyPlaceholder: () {

return Center(

child: Text("暂无数据!"),

);

},

);

return Scaffold(

appBar: AppBar(

// Here we take the value from the MyHomePage object that was created by

// the App.build method, and use it to set our appbar title.

title: Text(widget.title),

),

floatingActionButton: FloatingActionButton(

child: Icon(Icons.add),

onPressed: () => _incrementCounter(),

),

body: ListView.builder(

itemBuilder: (context, index) => _groupHandler.cellAtIndex(index),

itemCount: _groupHandler.allItemCount,

));

}

}

2021-03-02

1、新增无数据时占位widget支持。

2、增加demo。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值