gpt4 book ai didi

flutter - 使用 BottomNavigationBar 根据页面更改 AppBar 标题

转载 作者:行者123 更新时间:2023-12-02 15:24:34 24 4
gpt4 key购买 nike

我正在尝试根据用户所在的页面更改 AppBar 标题 - 页面由加载不同类(页面)的 BottomNavigationBar 控制

我设法改变这一点的唯一方法是为每个页面添加一个应用栏,我认为这不是继续下去的方法。

class HomePage extends StatefulWidget {
final String title;

HomePage({Key key, this.auth, this.userId, this.onSignedOut, this.title})
: super(key: key);

final BaseAuth auth;
final VoidCallback onSignedOut;
final String userId;

@override
State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
final List<Widget> _children = [
Projects(),
TimedProject(),
Overview(),
Clients(),
];

GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();

@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('TITLE I NEED TO CHANGE DEPENDING ON PAGE',
style: TextStyle(color: Colors.black),
),

backgroundColor: Colors.white,
),
endDrawer: AppDrawer(),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
currentIndex: _currentIndex,
selectedItemColor: Theme.of(context).primaryColor,
type: BottomNavigationBarType.fixed,
items: [
new BottomNavigationBarItem(
icon: Icon(Icons.storage),
title: Text('Jobs'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.timer),
title: Text('Timer'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.pie_chart_outlined),
title: Text('Overview'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.supervisor_account), title: Text('Clients'))
],
),
);
}

void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}

最佳答案

创建一个保存应用栏标题的变量,或者您可以使用在 HomePage 类中传递的相同标题变量,但您必须删除最终的变量。

如果您在 HomePage 类中使用 title 变量,请确保使用“小部件.标题”

class HomePage extends StatefulWidget {
final String title;

HomePage({Key key, this.auth, this.userId, this.onSignedOut, this.title})
: super(key: key);

final BaseAuth auth;
final VoidCallback onSignedOut;
final String userId;

@override
State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
String _title;

final List<Widget> _children = [
Projects(),
TimedProject(),
Overview(),
Clients(),
];

GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();

@override
initState(){
_title = 'Some default value';
}


@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(_title,
style: TextStyle(color: Colors.black),
),

backgroundColor: Colors.white,
),
endDrawer: AppDrawer(),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
currentIndex: _currentIndex,
selectedItemColor: Theme.of(context).primaryColor,
type: BottomNavigationBarType.fixed,
items: [
new BottomNavigationBarItem(
icon: Icon(Icons.storage),
title: Text('Jobs'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.timer),
title: Text('Timer'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.pie_chart_outlined),
title: Text('Overview'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.supervisor_account), title: Text('Clients'))
],
),
);
}

void onTabTapped(int index) {
setState(() {
_currentIndex = index;
switch(index) {
case 0: { _title = 'Jobs'; }
break;
case 1: { _title = 'Timer'; }
break;
case 2: { _title = 'Overview'; }
break;
case 3: { _title = 'Clients'; }
break;
}
});
}
}

关于flutter - 使用 BottomNavigationBar 根据页面更改 AppBar 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com