美文网首页
SliverPersistentHeader

SliverPersistentHeader

作者: Jycoding | 来源:发表于2021-06-29 00:17 被阅读0次

import 'package:flutter/material.dart';

class commentPage extends StatefulWidget {

  commentPage({Key key}) : super(key: key);

  @override

  _commentPageState createState() => _commentPageState();

}

class _commentPageState extends State<commentPage>

    with SingleTickerProviderStateMixin {

  TabController tabController;

  @override

  void initState() {

    super.initState();

    this.tabController = TabController(length: 2, vsync: this);

  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      // appBar: AppBar(

      //   title: Text('评论'),

      // ),

      body: Scaffold(

        body: CustomScrollView(

          slivers: <Widget>[

            SliverAppBar(

              pinned: true,

              elevation: 0,

              expandedHeight: 250,

              flexibleSpace: FlexibleSpaceBar(

                title: Text('评论'),

                background: Image.network(

                  'https://img0.baidu.com/it/u=4092964127,2965208246&fm=26&fmt=auto&gp=0.jpg',

                  fit: BoxFit.cover,

                ),

              ),

            ),

            SliverPersistentHeader(

              pinned: true,

              delegate: StickyTabBarDelegate(

                child: TabBar(

                  labelColor: Colors.black,

                  controller: this.tabController,

                  tabs: <Widget>[

                    Tab(text: 'Home'),

                    Tab(text: 'Profile'),

                  ],

                ),

              ),

            ),

            SliverFillRemaining(

              child: TabBarView(

                controller: this.tabController,

                children: <Widget>[

                  Center(child: Text('Content of Home')),

                  Center(child: Text('Content of Profile')),

                ],

              ),

            ),

          ],

        ),

      ),

    );

  }

}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {

  final TabBar child;

  StickyTabBarDelegate({@required this.child});

  @override

  Widget build(

      BuildContext context, double shrinkOffset, bool overlapsContent) {

    return this.child;

  }

  @override

  double get maxExtent => this.child.preferredSize.height;

  @override

  double get minExtent => this.child.preferredSize.height;

  @override

  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {

    return true;

  }

}

相关文章

网友评论

      本文标题:SliverPersistentHeader

      本文链接:https://www.haomeiwen.com/subject/rsqhultx.html