如何从另一个页面访问 tabbarcontroller?
How to acces tabbarcontroller from another Page?
我想从另一个页面访问标签栏视图。这样我就可以通过单击按钮立即访问第三个 tabbarview。无需进入第一页。这怎么可能?
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => firstpage()), // here i want immediately go to third page
);
},
);
当我将第三页作为 MaterialPageRout 插入时,我的应用程序和标签栏丢失了。
class Home extends StatefulWidget {
Home({Key key, this.title}) : super(key: key);
final int title;
@override
State<StatefulWidget> createState() {
return HomeExtended();
}
}
class HomeExtended extends State<Home> with TickerProviderStateMixin {
//here I want to acces the title from home
// when I add super(key: key) it marks the key
// I think its because it is not a stateful widget
假设我们有以下应用:
import 'package:flutter/material.dart';
void main() {
runApp(TabBarDemo());
}
class TabBarDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
),
);
}
}
将以下内容添加到 TabBarDemo
class
final int initialIndex;
TabBarDemo({Key key, this.initialIndex}) : super(key: key);
将 DefaultTabController
编辑为:
//...
DefaultTabController(
length: 3,
initialIndex: initialIndex??0,
//...
然后当路由到 TabBarDemo 时 class 你可以这样调用它:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TabBarDemo(initialIndex:2)),
);
编辑:
如果您正在使用 StatefulWidget
并且它的状态是另一个扩展 State
的 class,在状态 class 中,您可以通过关键字 widget
访问有状态小部件。
initialIndex: widget.initialIndex??0,
我想从另一个页面访问标签栏视图。这样我就可以通过单击按钮立即访问第三个 tabbarview。无需进入第一页。这怎么可能?
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => firstpage()), // here i want immediately go to third page
);
},
);
当我将第三页作为 MaterialPageRout 插入时,我的应用程序和标签栏丢失了。
class Home extends StatefulWidget {
Home({Key key, this.title}) : super(key: key);
final int title;
@override
State<StatefulWidget> createState() {
return HomeExtended();
}
}
class HomeExtended extends State<Home> with TickerProviderStateMixin {
//here I want to acces the title from home
// when I add super(key: key) it marks the key
// I think its because it is not a stateful widget
假设我们有以下应用:
import 'package:flutter/material.dart';
void main() {
runApp(TabBarDemo());
}
class TabBarDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
),
);
}
}
将以下内容添加到 TabBarDemo
class
final int initialIndex;
TabBarDemo({Key key, this.initialIndex}) : super(key: key);
将 DefaultTabController
编辑为:
//...
DefaultTabController(
length: 3,
initialIndex: initialIndex??0,
//...
然后当路由到 TabBarDemo 时 class 你可以这样调用它:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TabBarDemo(initialIndex:2)),
);
编辑:
如果您正在使用 StatefulWidget
并且它的状态是另一个扩展 State
的 class,在状态 class 中,您可以通过关键字 widget
访问有状态小部件。
initialIndex: widget.initialIndex??0,