我是 flutter 的新手,我不知道如何使用 if-else 语句更改标签的颜色。基本上,这个语句给了我错误。
selectedItemColor:_selectedIndex == 0 ? Colors.cyan: Colors.grey,
**_selectedIndex == 1 ? Colors.cyan: Colors.grey,**
我在底部导航栏中使用它,如果我使用标题,颤振不接受。
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar( onTap: _onItemTapped, items: [
BottomNavigationBarItem(icon: Icon(Icons.home, color: _selectedIndex == 0 ? Colors.cyan: Colors.grey,), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.ballot, color: _selectedIndex == 1 ? Colors.cyan: Colors.grey,), label: 'Services'),
],
selectedItemColor:_selectedIndex == 0 ? Colors.cyan: Colors.grey,
_selectedIndex == 1 ? Colors.cyan: Colors.grey,
),
appBar: AppBar(
title: Text('Home',
style: TextStyle(
fontFamily: 'Noto Sans TC',
),
),
centerTitle: false,
backgroundColor: HexColor('#09B9B6'),
),
body: PageView(
controller: _pageController,
children: _Screens,
onPageChanged: _onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
);
}
它被称为三元运算符 - ? :
。根据您当前的逻辑,您应该使用以下代码:
selectedItemColor: _selectedIndex == 0 || _selectedIndex == 1 ? Colors.cyan : Colors.grey
但我看不出有任何理由使用这个逻辑来设置selectedItemColor
的BottomNavigationBar
。它将自动设置为所选项目,因此您可以使用它selectedItemColor: Colors.cyan
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句