【问题标题】:How to make the TabBar not moving in SliverAppBar?如何使 TabBar 在 SliverAppBar 中不动?
【发布时间】:2021-01-14 05:23:25
【问题描述】:

这是我的第一个问题。我目前正在使用标签栏处理 sliverAppBar。当我将条子向下拖动时,我能做些什么来使标签栏保持在顶部而不是向下移动? this is how its looks based on my code 这是我的代码:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    theme: ThemeData(primaryColor: Colors.green[700]),
    home: DefaultTabController(
      length: 3,
      child: Scaffold(
        body: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              expandedHeight: 450,
              pinned: true,
              bottom: TabBar( 
                tabs: [
                  Tab(icon: Icon(Icons.directions_car)),
                  Tab(icon: Icon(Icons.directions_transit)),
                  Tab(icon: Icon(Icons.directions_bike)),
                ],
              ),
              backgroundColor: Colors.green[700],
              flexibleSpace: FlexibleSpaceBar(
                background: Image.asset(
                  'images/green.png',
                fit: BoxFit.cover,
                ),
              ),
            ),
            SliverFillRemaining(
                child: Container(
              child: Column(
                children: List<int>.generate(12, (index) => index)
                    .map((index) => Container(
                          height: 40,
                          margin: EdgeInsets.symmetric(vertical: 10),
                          color: Colors.grey[300],
                          alignment: Alignment.center,
                          child: Text('$index item'),
                        ))
                    .toList(),
              ),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(15),
                    topRight: Radius.circular(15),
                  )),
            ))
          ],
        ),
      ),
    ),
  );
}
}

编辑:我仍然希望条子的功能能够向下拖动,但我希望将选项卡保持在顶部而不移动

【问题讨论】:

    标签: flutter tabs tabbar sliverappbar


    【解决方案1】:

    你必须用一个堆栈包裹你的 CustomScrollView 并将 TabBar 放在它上面

    @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(primaryColor: Colors.green[700]),
          home: DefaultTabController(
            length: 3,
            child: Scaffold(
              body: Stack(
                children: <Widget>[
                  CustomScrollView(
                    slivers: <Widget>[
                      SliverAppBar(
                        expandedHeight: 450,
                        pinned: true,
                        backgroundColor: Colors.green[700],
                      ),
                      SliverFillRemaining(
                          hasScrollBody: false,
                          child: Container(
                            child: Column(
                              children: List<int>.generate(12, (index) => index)
                                  .map((index) => Container(
                                        height: 40,
                                        margin: EdgeInsets.symmetric(vertical: 10),
                                        color: Colors.grey[300],
                                        alignment: Alignment.center,
                                        child: Text('$index item'),
                                      ))
                                  .toList(),
                            ),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(15),
                                  topRight: Radius.circular(15),
                                )),
                          ))
                    ],
                  ),
    
                  /// YOUR TAB WITH ICONS
                  Positioned(
                    top: MediaQuery.of(context).padding.top,
                    child: Container(
                      width: MediaQuery.of(context).size.width,
                      height: 60.0,
                      color: Colors.green[700],
                      child: TabBar(
                        tabs: [
                          Tab(icon: Icon(Icons.directions_car)),
                          Tab(icon: Icon(Icons.directions_transit)),
                          Tab(icon: Icon(Icons.directions_bike)),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    

    这不会修复底部的溢出问题。如果你也想解决这个问题,只需将这个 hasScrollBody: false, 添加到你的 SliverFillRemaining 中,如下所示:

    SliverFillRemaining(
         hasScrollBody: false,
         child: Container(...),
    ),
    

    【讨论】:

    • 我仍然希望条子的功能能够向下拖动,但我希望将标签保持在顶部而不是移动
    • 我已经更新了我的答案。让我知道这是否是您要找的东西
    猜你喜欢
    • 2018-11-17
    • 2018-10-30
    • 2018-12-19
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2020-04-17
    • 2019-08-06
    • 2021-05-14
    相关资源
    最近更新 更多