disable swiping tabs in TabBar flutter

Flutter

Flutter Problem Overview


Hello I have a tab bar in Flutter and I want to disable swiping between tabs

      // 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,


        ),
      ),
    );
  }
}

I am moving tabs on clicking each tab bar button and I want to disable swiping thank you

Flutter Solutions


Solution 1 - Flutter

you can achieve that by changing how the page view should respond to user input using the physics property. and we have a NeverScrollableScrollPhysics for that purpose so just change physics to that like this :

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

Solution 2 - Flutter

physics: NeverScrollableScrollPhysics(),


1. You can disable swiping from TabBarView()

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

2. You can disable scrooling from 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(),
)

physics accept these value :

  1. BouncingScrollPhysics() : bouncing scrolling when you end/start of list
  2. NeverScrollableScrollPhysics() : stop tab change OR stop list scrolling OR stop page change of pageview
  3. ClampingScrollPhysics() : normal behaviour

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndr&#233; AbboudView Question on Stackoverflow
Solution 1 - FlutterRaouf RahicheView Answer on Stackoverflow
Solution 2 - FlutterSanjayrajsinhView Answer on Stackoverflow