【问题标题】:How to disable swiping whole screen inside the Tab Bar?如何禁用在标签栏内滑动整个屏幕?
【发布时间】:2021-08-21 21:11:17
【问题描述】:

我在 SliverAppBar 和该特定选项卡内有一个 TabBar,有一个容器,该容器进一步从左到右在其中滑动,但是当我在屏幕内向左或向右滑动时,它会更改选项卡。假设,我在标签 1 上,如果我在容器内滑动,它会转到另一个标签。我怎么能阻止它?如果用户只按下顶部的应用栏,我只想从一个选项卡切换到另一个选项卡,如果它在屏幕内滑动,那应该没有任何效果。我已经尝试过 NeverScrollPhysics,但它只会停止应用栏并且在屏幕内没有任何区别。

下面是标签的代码:-

         SafeArea(
          child: NestedScrollView(
            controller: _scrollViewController,
            headerSliverBuilder: (BuildContext context, bool  boxIsScrolled){
              return <Widget>[
                SliverAppBar(
                  automaticallyImplyLeading: false,
                  backgroundColor: Colors.transparent,
                  floating: true,
                  pinned: false,
                   snap: true,
                  title: TabBar(

                      isScrollable: true,
                      controller: _tabController,
                      unselectedLabelColor: Colors.blueGrey,
                      unselectedLabelStyle: TextStyle(fontSize: 15,fontWeight: FontWeight.w700 ),
                      labelColor: white,
                      indicatorSize: TabBarIndicatorSize.label,
                      indicator: BoxDecoration(
                          borderRadius: BorderRadius.circular(50),
                          color: mRed),
                      tabs: [

                        Tab(
                          child: Container(
                         
                              child: Text("TAB 1",),
                            ),
                          ),
                         Tab(
                          child: Container(
                         
                              child: Text("TAB 2",),
                            ),
                          ),
                        )
    and so on......

【问题讨论】:

    标签: flutter dart flutter-layout flutter-animation


    【解决方案1】:

    我很好奇您将NeverScrollableScrollPhysics() 放在哪里,因为如果我正确理解您的问题,它就是这样做的。

    您已经删除了下一个至关重要的部分...但是让我们假设在您的 NestedScrollView 主体中有您的 TabBarView,那么它应该如下所示:

        NestedScrollView(
          headerSliverBuilder: ......
    
          body: TabBarView(
            physics: const NeverScrollableScrollPhysics(),
            .....
    

    这是一个完整的示例:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(primarySwatch: Colors.blue),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: DefaultTabController(
            length: 2,
            child: NestedScrollView(
              headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) => [
                const SliverAppBar(
                  title: Text('AppBar title'),
                  bottom: TabBar(
                    tabs: [
                      Tab(
                        child: Align(
                          child: Text('Tab 1'),
                        ),
                      ),
                      Tab(
                        child: Align(
                          child: Text('Tab 2'),
                        ),
                      )
                    ],
                  ),
                )
              ],
              body: TabBarView(
                physics: const NeverScrollableScrollPhysics(),
                children: [
                  Container(
                    child: const Text('Text 1'),
                  ),
                  Container(
                    child: const Text('Text 2'),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 我已经尝试过除此之外的所有其他地方...谢谢。
    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2023-04-01
    • 2023-01-10
    相关资源
    最近更新 更多