如何將標題添加到 SliverList,我顯示餐廳串列,我需要顯示此串列的標題,例如頂級餐廳
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => Padding(
padding:
const EdgeInsets.only(left: 15.0, right: 15, bottom: 5),
child: RestaurantWidget(restaurant: _con.topRestaurants[index]),
),
childCount: _con.topRestaurants.length,
),
),
uj5u.com熱心網友回復:
嘗試在你的 SliverList 之前添加 SliverPersistentHeader,參考這篇文章,Sliver:
這是在您的頁面類中:
...
SliverPersistentHeader(
pinned: true,
floating: false,
delegate: HeaderDelegate(backgroundColor, _title),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => Padding(
padding:
const EdgeInsets.only(left: 15.0, right: 15, bottom: 5),
child: RestaurantWidget(restaurant: _con.topRestaurants[index]),
),
childCount: _con.topRestaurants.length,
),
),
...
把它放在你的頁面類之外:
class HeaderDelegate extends SliverPersistentHeaderDelegate {
final Color backgroundColor;
final String _title;
Delegate(this.backgroundColor, this._title);
@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
return Container(
color: backgroundColor,
child: Center(
child: Text(
_title,
style: TextStyle(
color: Colors.white,
fontSize: 25,
),
),
),
);
}
@override
double get maxExtent => 80;
@override
double get minExtent => 50;
@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
return true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329972.html
上一篇:AndroidStudio顯示Unresolvedreference:Gson即使我在gradle中有它并且它可以作業
