通过应用程序在导航上动态更改Appbar内容?
我的应用程序中有导航,但是我想动态更改Appbar标题,具体取决于当前显示的页面。 主要问题是,我正在使用tabbarview来显示主要页面,但我也可以进入某些子页面。该部分现在正常工作,但是我不知道,我如何根据显示的页面 /子页面进行APPBAR更改
是主类,它保存Appbar:
class AppView extends StatefulWidget {
const AppView({Key? key}) : super(key: key);
@override
State<AppView> createState() => _AppViewState();
}
class _AppViewState extends State<AppView> with TickerProviderStateMixin{
late TabController _controller;
@override
void initState(){
super.initState();
_controller = TabController(length: 6, vsync: this);
_controller.addListener(() {
if(navKey.currentState!.canPop()){
navKey.currentState!.pop();
}
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
if(navKey.currentState!.canPop()) {
navKey.currentState!.pop();
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: HeadLine(
"Test",
color: white,
),
),
body: Navigator(
key: navKey,
onGenerateRoute: (_) => MaterialPageRoute(
builder: (_) => TabBarView(
controller: _controller,
physics: NeverScrollableScrollPhysics(),
children: [
HomeScreen(),
ChatOverviewScreen(),
ForumOverviewScreen(),
CalendarScreen(),
GroupScreen(),
ProfileScreen()
],
)
),
),
bottomNavigationBar: BottomBar(
controller: _controller,
)),
);
}
}
用于测试,目前只有一个按钮在以下页面之一上调用一个子页面:
class ChatOverviewScreen extends StatelessWidget {
const ChatOverviewScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: PrimaryButton(
text: "Do Stuff",
onPressed: () {
navKey.currentState!.push(ChatDetailScreen.route());
},
),
);
}
}
Navkey通过Basicaly在我的主文件中设置为全局变量,
final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
我想设置Appbar的标题,具体取决于显示哪个页面 /子页面,当它是子页面时,应该在也是箭头的箭头。
I've got my Navigation in my App working, but I would like to change the AppBar Title dynamically, depending on which page is currently displayed.
Main Problem is, I'm using a TabBarView, to show the main pages, but I can also get to some sub pages. That part is now working fine, but I can't figure out, how I could make the AppBar change depending on the displayed page / sub-page
Here is the main class, which holds the AppBar:
class AppView extends StatefulWidget {
const AppView({Key? key}) : super(key: key);
@override
State<AppView> createState() => _AppViewState();
}
class _AppViewState extends State<AppView> with TickerProviderStateMixin{
late TabController _controller;
@override
void initState(){
super.initState();
_controller = TabController(length: 6, vsync: this);
_controller.addListener(() {
if(navKey.currentState!.canPop()){
navKey.currentState!.pop();
}
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
if(navKey.currentState!.canPop()) {
navKey.currentState!.pop();
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: HeadLine(
"Test",
color: white,
),
),
body: Navigator(
key: navKey,
onGenerateRoute: (_) => MaterialPageRoute(
builder: (_) => TabBarView(
controller: _controller,
physics: NeverScrollableScrollPhysics(),
children: [
HomeScreen(),
ChatOverviewScreen(),
ForumOverviewScreen(),
CalendarScreen(),
GroupScreen(),
ProfileScreen()
],
)
),
),
bottomNavigationBar: BottomBar(
controller: _controller,
)),
);
}
}
For Testing, theres currently just a button which calls a sub page on one of these pages:
class ChatOverviewScreen extends StatelessWidget {
const ChatOverviewScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: PrimaryButton(
text: "Do Stuff",
onPressed: () {
navKey.currentState!.push(ChatDetailScreen.route());
},
),
);
}
}
navKey is set as global variable in my main file via
final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
Basicaly I want to set the Title of the AppBar depending on which page / subpage is shown and when it is a sub page, there should also be an arrow to get back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该做这样的事情:
您可以使用ENUM来识别页面,此外,您可以使用某些州经理。
flutter状态管理
You should do something like this:
You can use enum to identify the pages, also, you can use some state manager.
Flutter State management