Flutter 去掉 TabBar 和 SliverAppBar 的空格

如何解决Flutter 去掉 TabBar 和 SliverAppBar 的空格

我想实现一个浮动的 AppBar,底部一个固定的 TabBar。以下是测试代码dartPad):

Widget build(BuildContext context) {
return Scaffold(
  body: nestedScrollView(
    floatHeaderSlivers: true,headerSliverBuilder: (BuildContext context,bool innerBoxIsScrolled) {
      return <Widget>[
        SliverAppBar(
          backgroundColor: Colors.yellow,title: Text(
            "WhatsApp type sliver appbar",),centerTitle: true,pinned: true,floating: true,bottom: PreferredSize(
            preferredSize: Size.fromHeight(kToolbarHeight),child: Container(
              color: Colors.orange,alignment: Alignment.topLeft,child: TabBar(
                  isScrollable: true,indicatorColor: Colors.white,indicatorSize: TabBarIndicatorSize.label,controller: _tabController,labelPadding: EdgeInsets.only(
                      top: 13,bottom: 13,left: 16,right: 16),tabs: [
                    Text(
                      "TAB A",Text(
                      "TAB B",]),];
    },body: TabBarView(
      controller: _tabController,children: [
        TabA(),const Center(
          child: Text('display Tab 2',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),],);

}

我发现向下滚动时,它会在 TabBar 上有一个顶部填充。有什么办法可以去掉那个空间吗?我已经设置了 SliverAppBar 的工具栏高度,但即使我降低高度,该空间也会保持在那里。

向上滚动(显示应用栏):

enter image description here

向下滚动(隐藏appbar,顶部黄色不隐藏):

enter image description here

解决方法

只需设置属性 pinned: false

documentation

固定→ bool 应用栏是否应在滚动视图开始时保持可见。 [...] 最后

同时从底部删除 tabBar:并将其添加到列内的 tabbarview 上方

,

感谢您的帮助。

最后,我还有另一个可以考虑的解决方案。我在这里发帖供其他人参考。

Widget build(BuildContext context) {
return Scaffold(
  body: NestedScrollView(
    floatHeaderSlivers: true,headerSliverBuilder: (BuildContext context,bool innerBoxIsScrolled) {
      return <Widget>[
        SliverAppBar(
          backgroundColor: Colors.yellow,title: Text(
            "WhatsApp type sliver appbar",),elevation: 0.0,forceElevated: false,pinned: false,floating: true,primary: false,centerTitle: false,titleSpacing: NavigationToolbar.kMiddleSpacing,automaticallyImplyLeading: false,SliverAppBar(
          backgroundColor: Colors.orange,pinned: true,titleSpacing: 0,toolbarHeight: 48,forceElevated: true,title: Align(
            alignment: Alignment.topLeft,child: TabBar(
                isScrollable: true,indicatorColor: Colors.white,indicatorSize: TabBarIndicatorSize.label,controller: _tabController,labelPadding: EdgeInsets.only(
                    top: 13,bottom: 13,left: 16,right: 16),tabs: [
                  Text(
                    "TAB A",Text(
                    "TAB B",]),];
    },body: TabBarView(
      controller: _tabController,children: [
        TabA(),const Center(
          child: Text('Display Tab 2',style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),],);
}

基本上,我所做的是分离 2 个 SliverAppBar,一个不是固定和浮动的;另一个被固定。这使得上方的应用栏在向下滚动时消失并在向上滚动时显示,而标签栏将保持固定在那里。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?