简体   繁体   中英

flutter - add pinned title to SliverList

how can I add title to SliverList, Im showing list of restaurants and I need to show title to this list such as Top Restaurants

          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,
            ),
          ),

try to add SliverPersistentHeader before your SliverList, reference to this article, Sliver :

This is in your page class:

...
          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,
            ),
          ),
...

Place this outside your page class:

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM