答案 0 :(得分:6)
SliverPersistentHeader
是SliverAppBar
后面更通用的小部件,您可以使用。
SliverPersistentHeader(
delegate: SectionHeaderDelegate("Section B"),
pinned: true,
),
SectionHeaderDelegate
可以通过以下方式实现:
class SectionHeaderDelegate extends SliverPersistentHeaderDelegate {
final String title;
final double height;
SectionHeaderDelegate(this.title, [this.height = 50]);
@override
Widget build(context, double shrinkOffset, bool overlapsContent) {
return Container(
color: Theme.of(context).primaryColor,
alignment: Alignment.center,
child: Text(title),
);
}
@override
double get maxExtent => height;
@override
double get minExtent => height;
@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => false;
}
答案 1 :(得分:2)
一种方法是创建一个CustomScrollView
,并将固定在SliverAppBar
上的true
和一个SliverFixedExtentList
对象与小部件一起传递。
示例:
List<Widget> _sliverList(int size, int sliverChildCount) {
var widgetList = new List<Widget>();
for (int index = 0; index < size; index++)
widgetList
..add(SliverAppBar(
title: Text("Title $index"),
pinned: true,
))
..add(SliverFixedExtentList(
itemExtent: 50.0,
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.lightBlue[100 * (index % 9)],
child: Text('list item $index'),
);
}, childCount: sliverChildCount),
));
return widgetList;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Slivers"),
),
body: CustomScrollView(
slivers: _sliverList(50, 10),
),
);
}