I want to prevent the tab from moving even if I tap on TabBar.
TabBar( controller: this._controller, tabs: <Widget>[ new Tab( text: "A", ), new Tab( text: "B", ), new Tab( text: "C", ), ]) I want to prevent the tab from moving even if I tap on TabBar.
TabBar( controller: this._controller, tabs: <Widget>[ new Tab( text: "A", ), new Tab( text: "B", ), new Tab( text: "C", ), ]) I have a more elegant solution:
TabBar( onTap: (index) { if (_controller.indexIsChanging) { _controller.index = _controller.previousIndex; } else { return; } }, ), if (_controller.indexIsChanging) { _controller.index = _controller.previousIndex; } else { return; }. It does nothing when tapping the active tab, otherwise, it stays on the selected tab.If you're embedding the TabBar in the bottom of an AppBar, you'll need to implement PreferredSizeWidget. This is easily achieved:
class ReadOnlyTabBar extends StatelessWidget implements PreferredSizeWidget { final TabBar child; const ReadOnlyTabBar({Key key, @required this.child}) : super(key: key); @override Widget build(BuildContext context) { return IgnorePointer(child: child); } @override Size get preferredSize => this.child.preferredSize; } I think you have to add listner to tab click and then change the index to 0 again. In this we need to add controller and we can set index through that.
class TabBarDemoWidget extends State<TabBarDemo> with TickerProviderStateMixin{ @override Widget build(BuildContext context) { int _tabIndex = 0; var tab = TabController( initialIndex: 0, length: 3, vsync: this ); void _handleTabSelection(){ setState(() { tab.index = _tabIndex; }); } tab.addListener(_handleTabSelection); return DefaultTabController( length: 3, initialIndex: 0, child: TabBar( labelColor: Colors.teal, controller: tab, tabs: [ GestureDetector( child:Tab( icon: Icon( Icons.directions_car)) , onTap: (){ _tabIndex = 0; }, ),GestureDetector( child:Tab( icon: Icon(Icons.directions_car)) , onTap: (){ _tabIndex = 0; }, ),GestureDetector( child:Tab( icon: Icon(Icons.directions_car)) , onTap: (){ _tabIndex = 0; }, ) ], ), ); } } addListener. it works. but, maybe, GestureDetector not needed. gyazo.com/5e5b61036e009cb37ce20a4c41de4937@Rob Lyndon gave perfect answer.
This is another one like that only. But in this we are not creating one more class
Scaffold( appBar: AppBar( bottom: PreferredSize( preferredSize: Size.fromHeight(_kTextAndIconTabHeight + indicatorWeight), child: IgnorePointer( child: TabBar( ), ), ), ), ); Pass a gesture detector to the tab that you want to disable. Set onTap as null to disable tap. Look at the example provided below!
TabBar( tabs: [ Tab(child: Text("15")), //enabled Tab(child: GestureDetector(child: Text("16"), onTap: null), //disabled ], ), Wrap the tab widget with GestureDetector, and follow the code!!
TabBar( automaticIndicatorColorAdjustment: true, tabs: [ const Tab( child: TextWidget('Official'), ), GestureDetector( onTap: () {}, child: const Tab( child: TextWidget('PF,ESI'), ), ), ], indicatorSize: TabBarIndicatorSize.tab, tabAlignment: TabAlignment.start, isScrollable: true, ),