我试图根据用户所在的页面来更改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;
});
}
}
答案 0 :(得分:1)
感谢您的解决方案,可以将底部的导航器取出并放入自己的.dart文件中,例如
bottomNavigationBar: BottomNavBar(),
并获取选定的标签索引
class _HomeScreen2State extends State<HomeScreen2> {
//Hold current Tab Index
int _selectTab = 0;
final _pageOptions = [
HomeScreen(),
NewOrderScreen(),
OrderHistoryScreen(),
ContactScreen(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Gift Shop Dashboard'),
),
body: _pageOptions[_selectTab],
bottomNavigationBar: BottomNavBar(),
);
}
}
答案 1 :(得分:0)
创建一个包含应用栏标题的变量,或者您可以使用与HomePage类中传递的标题变量相同的变量,但是您必须删除最后一个。
如果您在HomePage类中使用title变量,请确保使用 “ widget.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; } }); } }
答案 2 :(得分:0)
要将BottomNavigationBar
分隔在不同的.dart
文件中,例如在config.dart
中:
import 'package:flutter/material.dart';
class Config {
static List<BottomNavigationBarItem> navigationBarItems = [
BottomNavigationBarItem(
icon: Icon(
Icons.date_range,
),
title: Text(
"Calendar",
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.list, // event_note
),
title: Text(
"List",
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.bookmark_border, // grid_on
),
title: Text(
"Bookmarks",
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.account_circle,
),
title: Text(
"Me",
),
),
];
static BottomNavigationBar navigationBar = BottomNavigationBar(
items: navigationBarItems,
type: BottomNavigationBarType.fixed,
fixedColor: Colors.red,
);
}
和main.dart
中的
import 'package:flutter/material.dart';
import 'config.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hello',
theme: ThemeData(
primarySwatch: Colors.blueGrey,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _index = 0;
Text _title;
_onTap(int index) {
setState(() {
_index = index;
_title = Config.navigationBarItems[_index].title;
});
}
@override
void initState() {
_title = Config.navigationBarItems[_index].title;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: _title,
),
body: Container(
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _index,
type: Config.navigationBar.type,
fixedColor: Config.navigationBar.fixedColor,
items: Config.navigationBar.items,
onTap: _onTap,
),
);
}
}
答案 3 :(得分:0)
只需在条件中设置条件
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 1;
String appbarTitleString = "Home";
var appBarTitleText = new Text("Home");
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Profile',
style: optionStyle,
),
Text(
'Index 1: Home',
style: optionStyle,
),
Text(
'Index 2: More',
style: optionStyle,
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: appBarTitleText,
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Profile'),
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('More'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
if (index == 0){
appbarTitleString = "Profile" ;
appBarTitleText = new Text(appbarTitleString);
}else if (index == 1){
appbarTitleString = "Home" ;
appBarTitleText = new Text(appbarTitleString);
}else if(index == 2){
appbarTitleString = "More" ;
appBarTitleText = new Text(appbarTitleString);
}
});
}
}