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"),
),
]),
)),
);
}
}
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"),
),
]),
)),
);
}
}