How to Set Initial Tab in flutter
1,474
The DefaultTabController
has an initialIndex
property which is set to 0
by default.
In your case, since you have two Tab
s, you would need to set the initialIndex
property to 1
.
I added a demo using your code as an example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
// set initial index to 1
initialIndex: 1,
child: Scaffold(
appBar: AppBar(
title: Text("Tab Bar"),
bottom:
TabBar(tabs: [Tab(child: Text('B')), Tab(child: Text('A'))]),
),
body: TabBarView(children: [
Center(
child: Text("this is to be second tab"),
),
Center(
child: Text("this is to be first tab"),
),
]),
)),
);
}
}
Author by
Laurenz Albe
PostgreSQL support specialist and consultant PostgreSQL enthusiast and contributor System and application programmer PostgreSQL blog
Updated on December 24, 2022Comments
-
Laurenz Albe over 1 year
I want that whenever I run my application, it should be moved to Tab A, but by default it is moving to first tab, which is named tab B.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title: Text("Tab Bar"), bottom: TabBar(tabs: [Tab(child: Text('B')), Tab(child: Text('A'))]), ), body: TabBarView(children: [ Center( child: Text("this is to be second tab"), ), Center( child: Text("this is to be first tab"), ), ]), )), ); } }
-
Karmalakas over 3 yearsinitialIndex maybe? stackoverflow.com/questions/52941366/…
-
-
Admin over 3 yearsthanx sir this is working fine but i have one more issue like we reach to tab A and from tab A to if i swipe left then it would be navigate to different screen and also if we re-return in Tab A and from tab A if i swipe right then it is going to tab B good but if i swipe right again from tab b then it will be navigate to different screen