![](/img/trans.png)
[英]How to change BottomNavigationBarItem icon when selected, Flutter
[英]How can I change Icon colors when it's selected in flutter?
我是 flutter 的新手。我希望圖標 colors 是黑色的,除非他們點擊了。 要么當我點擊時顏色沒有改變,要么我不能改變點擊的顏色。 我看到了這個三元解決方案,但我不知道為什么它不起作用。
class _RootAppState extends State<RootApp> {
int _selectedIndex = 0;
int? currentIndex;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text('Index 0: Home', style: optionStyle,),
...
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
/* appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
), */
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home, size: 25),
label: 'Home',
),...
],
currentIndex: _selectedIndex,
selectedItemColor: _selectedIndex == currentIndex ? Colors.black :Colors.amber[800],
onTap: _onItemTapped,
),
);
}
我對此也很陌生,從我的應用程序來看,我只是在嘗試添加這個type: BottomNavigationBarType.fixed,
嘗試使用CupertinoTabScaffold 。 在 state 更改中,您所在的選項卡的顏色會瞬間亮起。 您也可以使用主題更改此顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.