listview - Flutter中SliverPersistentHeader和TabBarView如何搭配使用?

标签 listview flutter tabbar flutter-sliver

我的主页有 SliverPersistentHeaderTabBarView 如下代码:

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: DefaultTabController(
        length: 3,
        child: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 250.0,
                floating: false,
                pinned: true,
              ),
              SliverPersistentHeader(
                delegate: _SliverAppBarDelegate(
                  TabBar(
                    labelStyle: TextStyle(
                        fontFamily: 'Raleway',
                        fontSize: 17,
                        fontWeight: FontWeight.w400,
                        color: Theme.of(context).backgroundColor),
                    indicatorColor: Theme.of(context).hintColor,
                    labelColor: Theme.of(context).buttonColor,
                    unselectedLabelColor: Theme.of(context).dividerColor,
                    tabs: [
                      Tab(text: "Menu"),
                      Tab(text: "About"),
                      Tab(text: "Contact"),
                    ],
                  ),
                ),
                pinned: true,
              ),
            ];
          },
          body: TabBarView(
            children: <Widget>[MenuTab(), AboutTab(), ContactTab()],
          ),
        ),
      ),
    );
  }
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate(this._tabBar);

  final TabBar _tabBar;

  @override
  double get minExtent => _tabBar.preferredSize.height;

  @override
  double get maxExtent => _tabBar.preferredSize.height;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new Container(
      child: _tabBar,
    );
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return false;
  }
}

在那之后我有一个单一的类敌人每个选项卡,例如像下面的代码:

class _AboutTabState extends State<AboutTab> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Theme.of(context).backgroundColor,
        body: ListView(
      children: <Widget>[
        ListView.builder(
          shrinkWrap: true,
          itemCount: _list.length,
          itemBuilder: (BuildContext context, int index) {
            final _aboutList = _list[index];
            return ExpansionTile(
              title: ListTile(
                title: Padding(
                  padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                  child: Text(_aboutList.aboutTitle,
                      style: TextStyle(
                          fontFamily: 'Raleway',
                          fontSize: 16,
                          fontWeight: FontWeight.w500,
                          color: Theme.of(context).buttonColor)),
                ),
              ),
              children: <Widget>[
                ListTile(
                  title: Padding(
                    padding: const EdgeInsets.fromLTRB(10, 0, 0, 0),
                    child: Text(_aboutList.content,
                        style: TextStyle(
                            fontFamily: 'Raleway',
                            fontSize: 16,
                            fontWeight: FontWeight.w400,
                            color: Theme.of(context).toggleableActiveColor)),
                  ),
                )
              ],
            );
          },
        ),
      ],
    ));

问题:

当我按照屏幕截图折叠 SliverPersistentHeader 时,我得到了错误的结果。 发送

enter image description here

enter image description here

最佳答案

_SliverAppBarDelegate() 函数正在返回一个容器,您可以像这样在其中添加颜色:

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate(this._tabBar);

  final TabBar _tabBar;

  @override
  double get minExtent => _tabBar.preferredSize.height;
  @override
  double get maxExtent => _tabBar.preferredSize.height;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new Container(
      color: Colors.white, // ADD THE COLOR YOU WANT AS BACKGROUND.
      child: _tabBar,
    );
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return false;
  }
}

关于listview - Flutter中SliverPersistentHeader和TabBarView如何搭配使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339990/

相关文章:

flutter - TextFormField 启用后未聚焦

flutter - 如何防止Flutter剪切BoxDecoration阴影?

xamarin - 在 Xamarin Forms 的 Tabbar 中添加自定义按钮

ios - TabBar选择项更改背景颜色

java - 单击 ListView 项时如何在详细 Activity 上加载 Firebase DB 值

android - Dart , flutter - ListView 不显示

listview - 如何从级联黑莓中的 ListView 中获取值(value)

android - 列表适配器仅将 1 个项目添加到 ListView

flutter - 如何刷新屏幕以更新元素列表?

swift - 我想从 Storyboard 移动到另一个 Storyboard ,它基本上是一个标签栏 Controller