如何在 Flutter 中导航时更改默认选项卡?
How to change the default tab while navigating in Flutter?
我有以下用于 TabBar 页面的代码:
class HomePage extends StatefulWidget {
static String tag = 'home-page';
@override
_homepage createState() => new _homepage();
}
class _homepage extends State<HomePage> with TickerProviderStateMixin{
AnimationController percentageAnimationController;
TabController _tabController;
@override
void initState() {
_tabController = new TabController(length: 3, vsync: this);
super.initState();
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
bottomNavigationBar: new Material(
color: Colors.white,
child: new TabBar(
controller: _tabController,
indicatorColor: Theme.Colors.loginGradientStart,
labelColor: Theme.Colors.loginGradientStart,
tabs: <Widget>[
new Tab(
icon: new Icon(wind_icon),
),
new Tab(
icon: new Icon(chart_icon),
),
new Tab(
icon: new Icon(settings_icon),
),
]
),
),
body:
new TabBarView(
children: <Widget>[
TabOne(),
TabTwo(),
TabThree(),
],
controller: _tabController,
),
);
}
}
现在,如果我导航到此页面,它会自动打开第一个选项卡,但我想打开第二个选项卡而不是第一个选项卡,即选项卡索引 1。
我发现我们可以通过使用 _tabController.animateTo(1); 来实现
但我想知道如何通过按下其他页面的按钮来做到这一点。
您可以使用初始索引:
_tabController = new TabController(length: 3, vsync: this, initialIndex: 1);
我有类似的问题。我创建了一个名为 "globals.dart".
的文件
my_app/globals.dart
library my_app.globals;
import 'package:flutter/material.dart';
TabController tabController;
并且,在您设置选项卡控制器之后,复制对此的引用。
_主页Class
import 'package:my_app/globals.dart' as globals;
@override
void initState() {
_tabController = new TabController(length: 3, vsync: this);
globals.tabController = _tabController;
super.initState();
}
other_page Class
import 'package:my_app/globals.dart' as globals;
...(in any function you want)...
globals.tabController.animateTo(2);
我有以下用于 TabBar 页面的代码:
class HomePage extends StatefulWidget {
static String tag = 'home-page';
@override
_homepage createState() => new _homepage();
}
class _homepage extends State<HomePage> with TickerProviderStateMixin{
AnimationController percentageAnimationController;
TabController _tabController;
@override
void initState() {
_tabController = new TabController(length: 3, vsync: this);
super.initState();
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
bottomNavigationBar: new Material(
color: Colors.white,
child: new TabBar(
controller: _tabController,
indicatorColor: Theme.Colors.loginGradientStart,
labelColor: Theme.Colors.loginGradientStart,
tabs: <Widget>[
new Tab(
icon: new Icon(wind_icon),
),
new Tab(
icon: new Icon(chart_icon),
),
new Tab(
icon: new Icon(settings_icon),
),
]
),
),
body:
new TabBarView(
children: <Widget>[
TabOne(),
TabTwo(),
TabThree(),
],
controller: _tabController,
),
);
}
}
现在,如果我导航到此页面,它会自动打开第一个选项卡,但我想打开第二个选项卡而不是第一个选项卡,即选项卡索引 1。
我发现我们可以通过使用 _tabController.animateTo(1); 来实现 但我想知道如何通过按下其他页面的按钮来做到这一点。
您可以使用初始索引:
_tabController = new TabController(length: 3, vsync: this, initialIndex: 1);
我有类似的问题。我创建了一个名为 "globals.dart".
的文件my_app/globals.dart
library my_app.globals;
import 'package:flutter/material.dart';
TabController tabController;
并且,在您设置选项卡控制器之后,复制对此的引用。
_主页Class
import 'package:my_app/globals.dart' as globals;
@override
void initState() {
_tabController = new TabController(length: 3, vsync: this);
globals.tabController = _tabController;
super.initState();
}
other_page Class
import 'package:my_app/globals.dart' as globals;
...(in any function you want)...
globals.tabController.animateTo(2);