在Flutter的TabBar中禁用滑动选项卡

110

你好,我在Flutter中有一个标签栏,想要禁用在标签之间滑动的功能。

      // Set the bottom navigation bar
      bottomNavigationBar: new Material(

        // set the color of the bottom navigation bar
        color: const Color(0xFFF7F7F7),
        // set the tab bar as the child of bottom navigation bar
        child: new TabBar(
          tabs: <Tab>[
            new Tab(
              // set icon to the tab
              icon: new Icon(Icons.home,color: Colors.black),
            ),
            new Tab(
              icon: new Icon(Icons.favorite,color: Colors.black),
            ),
            new Tab(
              icon: new Icon(Icons.search,color: Colors.black),
            ),
            new Tab(
              icon: new Icon(Icons.settings,color: Colors.black),
            ),
          ],
          // setup the controller
          controller: controller,


        ),
      ),
    );
  }
}

我正在点击每个选项卡按钮并移动选项卡,我想禁用滑动,谢谢。


如果我理解正确,您正在尝试禁用指示器,因为我不明白“swiping”在TabBar中的意思。 - Raouf Rahiche
5
通过滑动,我指的是你可以通过向左或向右滑动来在选项卡之间切换。 - André Abboud
请看一下我的答案。 - Raouf Rahiche
1
好的,谢谢你,干得好! - André Abboud
5个回答

254

你可以通过改变页面视图对用户输入的响应方式来实现这一点,使用 physics 属性。我们有一个专门用于此目的的 NeverScrollableScrollPhysics,只需将 physics 更改为它,如下所示:

TabBarView(
        physics: const NeverScrollableScrollPhysics(),
        controller: tabcontroler,
        children: <Widget>[
          Container(color: Colors.red),
          Container(color: Colors.green),
          Container(color: Colors.blue),
        ],
      ),

23

物理学: NeverScrollableScrollPhysics(),


1.您可以从TabBarView()禁用滑动

TabBarView(
        physics: NeverScrollableScrollPhysics(),
        controller: tabcontroler,
        children: <Widget>[]
)

2. 你可以通过 ListView()PageView() 禁用滚动。

 ListView.builder(
    // you can set BouncingScrollPhysics() if you show animation when user end of list
          physics: NeverScrollableScrollPhysics(),
          itemCount: categories.length,
          itemBuilder: (BuildContext ctx, int index) {
            return CategoryItem(categories[index]);
          },
)


PageView.builder(
          physics: NeverScrollableScrollPhysics(),
)

物理学接受这些值:

1. BouncingScrollPhysics():当您到达列表的末尾/开头时弹跳滚动。
2. NeverScrollableScrollPhysics():停止切换选项卡或停止列表滚动或停止PageView的页面更改。
3. ClampingScrollPhysics():正常行为。


1
值得注意的是,“BouncingScrollPhysics”是Cupertino小部件的默认值,而“ClampingScrollPhysics”是Material小部件的默认值。 - Tim
1
值得注意的是,“BouncingScrollPhysics”是Cupertino小部件的默认值,而“ClampingScrollPhysics”是Material小部件的默认值。 - undefined

2
在TabBarView中使用physics: NeverScrollableScrollPhysics()
TabBarView(
   physics: NeverScrollableScrollPhysics(), <---- Add line
   children: [
      child1(),
      child2(),
   ],
),

1
你可以将物理设置为NeverScrollableScrollPhysics()。
TabBarView(
   physics: NeverScrollableScrollPhysics(),
   children: [
     EditorPickTabView(),
     AllProfileTabView(),
   ],
),

0

你需要使用物理引擎:NeverScrollableScrollPhysics()

在Tabbar、ListView或PageView中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接