iOS的tableView使用分組是很方便的,但是flutter就沒有那么友好了积锅,
像網(wǎng)上找的解決方法都是很多的固定場(chǎng)景枣氧,
每次使用都要寫不同的邏輯,而且代碼和邏輯耦合很嚴(yán)重网缝,
不知道有沒有人弄過,但是我沒找到蟋定,
于是自己動(dòng)手造個(gè)輪子粉臊,通過邏輯處理封裝,
使用方法和iOS的基本一致驶兜,
當(dāng)然目前只提供了數(shù)據(jù)分組扼仲、sectionHeader、sectionFooter抄淑、無(wú)數(shù)據(jù)占位和列表header和footer屠凶。
如上圖,紅色的是整個(gè)列表的header肆资,一般會(huì)放個(gè)輪播圖什么的矗愧,
時(shí)間是section的header,下面的帖子就是第一個(gè)section的row郑原。
使用方法也很簡(jiǎn)單:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
_list.add(_counter.toString());
});
}
ListViewGroupHandler _groupHandler;
List<String> _list = <String>[];
@override
Widget build(BuildContext context) {
_groupHandler = ListViewGroupHandler(
//日志開關(guān)
openLog: true,
//分組數(shù)量唉韭,需要自行根據(jù)data設(shè)置夜涕,默認(rèn)1
numberOfSections: 1,
//每組cell個(gè)數(shù),需要自行根據(jù)data設(shè)置
numberOfRowsInSection: (section) => _list.length,
//indexPath: IndexPath(section,row,index)
cellForRowAtIndexPath: (indexPath) => Text(_list[indexPath.row]),
//頭部
header: () => Text("header"),
//占位Placeholder
emptyPlaceholder: () {
return Center(
child: Text("暫無(wú)數(shù)據(jù)属愤!"),
);
},
);
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,
));
}
}
Git 鏈接地址:
https://gitee.com/zeng_li_zhi/ListViewGroupHandler
安裝女器,在pubspec文件里添加:
list_group_handler:
git:
url: https://gitee.com/zeng_li_zhi/ListViewGroupHandler.git
ref: 'v0.0.1' #這是tag, nullsafe 支持為'v1.0.0'
2021-03-02
1、新增無(wú)數(shù)據(jù)時(shí)占位widget支持住诸。
2驾胆、增加demo。
2021-07-11
1贱呐、新增nullsafe版本 v1.0.0俏拱,上一版本為v0.0.1
2022-07-20
1、推薦官方類似效果 sticky_headers http://www.reibang.com/p/a25d747be7b4