Skip to content

Instantly share code, notes, and snippets.

@mhadaily
Created March 7, 2022 16:17
Show Gist options
  • Save mhadaily/872028df9009ddb7d05a26a3f2c393de to your computer and use it in GitHub Desktop.
Save mhadaily/872028df9009ddb7d05a26a3f2c393de to your computer and use it in GitHub Desktop.
Disable tab index in Flutter
import 'package:flutter/material.dart';
void main() {
runApp(const TabBarDemo());
}
class TabBarDemo extends StatefulWidget {
const TabBarDemo();
@override
_TabBarDemoState createState() => _TabBarDemoState();
}
class _TabBarDemoState extends State<TabBarDemo> with TickerProviderStateMixin {
late TabController tabController;
@override
void initState() {
super.initState();
tabController = TabController(
initialIndex: 0,
length: 3,
vsync: this,
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
onTap: (int index) {
if(index == 1){
return;
}
tabController.index = index;
},
tabs: const [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
title: const Text('Tabs Demo'),
),
body: TabBarView(
controller: tabController,
children: const [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
),
);
}
@override
void dispose() {
tabController.dispose();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment