disable swiping tabs in TabBar flutter
FlutterFlutter 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 :
- BouncingScrollPhysics() : bouncing scrolling when you end/start of list
- NeverScrollableScrollPhysics() : stop tab change OR stop list scrolling OR stop page change of pageview
- ClampingScrollPhysics() : normal behaviour