[英]How to Set Initial Tab in flutter
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.我希望每当我运行我的应用程序时,它都应该移动到选项卡 A,但默认情况下它移动到第一个选项卡,名为选项卡 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. DefaultTabController
有一个initialIndex
属性,默认设置为0
。
In your case, since you have two Tab
s, you would need to set the initialIndex
property to 1
.在您的情况下,由于您有两个
Tab
,您需要将initialIndex
属性设置为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"),
),
]),
)),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.