Hello I have a tab bar in Flutter and I want to disable swiping between tabs
// Set the bottom navigation barbottomNavigationBar: new Material(// set the color of the bottom navigation barcolor: const Color(0xFFF7F7F7),// set the tab bar as the child of bottom navigation barchild: new TabBar(tabs: <Tab>[new Tab(// set icon to the tabicon: 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 controllercontroller: controller,),),);}}
I am moving tabs on clicking each tab bar button and I want to disable swiping thank you
Best Answer
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: const NeverScrollableScrollPhysics(),controller: tabcontroler,children: <Widget>[Container(color: Colors.red),Container(color: Colors.green),Container(color: Colors.blue),],),
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 listphysics: 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
You can set the physics to NeverScrollableScrollPhysics()
TabBarView(physics: NeverScrollableScrollPhysics(),children: [EditorPickTabView(),AllProfileTabView(),],),
Use physics: NeverScrollableScrollPhysics(), in TabBarView
TabBarView(physics: NeverScrollableScrollPhysics(), <---- Add linechildren: [child1(),child2(),],),
you have to use physics: NeverScrollableScrollPhysics(),
in Tabbar or ListView or PageView