你好,我正在学习颤振。几年前我是一名程序员。我出于某种原因退出了编程并开始做自己的生意。现在我正在尝试开发一个应用程序来管理我的业务。
所以,我确实在这里发布了我的问题的解决方案,但我想让我的代码更好..
有没有办法访问 BottomNavigationBarItem() 的标签文本并更新 appBarTitle 值,而不是使用硬编码的 SWITCH - CASE?
这是我的代码示例。我希望我的代码更加动态而不是硬编码..
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
String _appBarTitle = "Gadi Wala !!";
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions = <Widget>[
LedgerPage(),
Text(
'Index 1 : Trips',
style: optionStyle,
),
Text(
'Index 2 : Trucks',
style: optionStyle,
),
Text(
'Index 3 : Profile',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
switch (index) {
case 0:
_appBarTitle = "Ledger";
break;
case 1:
_appBarTitle = "Trips";
break;
case 2:
_appBarTitle = "Trucks";
break;
case 3:
_appBarTitle = "Profile";
break;
default:
_appBarTitle = "Gadi Wala";
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Icon(
Icons.local_shipping,
color: Colors.white70,
size: 24.0,
),
title: Text(_appBarTitle),
actions: [
IconButton(
icon: Icon(Icons.notifications),
onPressed: () {},
)
],
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.auto_stories),
label: 'Ledger',
),
BottomNavigationBarItem(
icon: Icon(Icons.alt_route_rounded),
label: 'Trips',
),
BottomNavigationBarItem(
icon: Icon(Icons.local_shipping_rounded),
label: 'Trucks',
),
BottomNavigationBarItem(
icon: Icon(Icons.person_pin_rounded),
label: 'Profile',
),
],
backgroundColor: Colors.blue,
selectedFontSize: 20,
unselectedItemColor: Colors.grey,
showUnselectedLabels: true,
currentIndex: _selectedIndex,
selectedItemColor: Colors.teal[800],
onTap: _onItemTapped,
),
);
}
}
先感谢您 !!