在其他页面上颤抖地更改主应用栏标题 [英] Flutter change main appbar title on other pages
问题描述
在后续页面上更改AppBar title
方面寻求帮助,所以我是制表符,而有些则没有。 MyApp
在我的应用程序的身份验证页面上定义。然后,我转到一个包含选项卡的新页面,然后在某些选项卡页面上关闭了其他页面,我想要做的是,而不是放置另一个 AppBar
在主菜单下,我只想更改主 AppBar
的 title
其他页面。
Looking for some assistance with changing the AppBar title
on subsequent pages, so me being tabs and some not. MyApp
is defined on the authentication page of my app. I then goto a new page that holds the tabs, then I have other pages off some of the tab pages, what I want to be able to do is, instead of putting another AppBar
under the main one, I just want to change the title
of the main AppBar
when I am on any of the other pages.
关于如何执行此操作的任何想法,我看到一个示例不适合,因为我的标签设置不同,无法适应,可能是因为
Any ideas how to do this, I saw 1 example that did not fit because my tabs are setup different and could not make it fit, thought maybe there was a way to define the title initially so that I can change state or something and change the title.
对此有任何想法或想法吗?
Any ideas or thoughts on this?
推荐答案
您可以添加 TabController
并添加监听
使其在 Tab
s之间切换时,只要调用 setState
并更改 AppBar
标题。
You can add a TabController
and add listen
to it such that you call setState
whenever you are switching between the Tab
s, and change the AppBar
title accordingly.
import "package:flutter/material.dart";
void main(){
runApp(new MaterialApp(home:new MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> with TickerProviderStateMixin{
final List<MyTabs> _tabs = [new MyTabs(title: "Teal",color: Colors.teal[200]),
new MyTabs(title: "Orange",color: Colors.orange[200])
];
MyTabs _myHandler ;
TabController _controller ;
void initState() {
super.initState();
_controller = new TabController(length: 2, vsync: this);
_myHandler = _tabs[0];
_controller.addListener(_handleSelected);
}
void _handleSelected() {
setState(() {
_myHandler= _tabs[_controller.index];
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(_myHandler.title),
backgroundColor: _myHandler.color,
bottom: new TabBar(
controller: _controller,
tabs: <Tab>[
new Tab(text: _tabs[0].title,),
new Tab(text: _tabs[1].title,)
],
),),
);
}
}
class MyTabs {
final String title;
final Color color;
MyTabs({this.title,this.color});
}
这篇关于在其他页面上颤抖地更改主应用栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!