简体   繁体   English

在 flutter 中选中时,如何更改图标 colors?

[英]How can I change Icon colors when it's selected in flutter?

I'm new to flutter. I want to Icon colors be black except when they clicked.我是 flutter 的新手。我希望图标 colors 是黑色的,除非他们点击了。 Either the color doesn't change when I click, or I can't change the color of the clicked one.要么当我点击时颜色没有改变,要么我不能改变点击的颜色。 I saw this ternary solution but I don't know why it isn't working.我看到了这个三元解决方案,但我不知道为什么它不起作用。

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,
      ),
    );
  }

im pretty new to this too, looking from my app i just added this in try adding this in type: BottomNavigationBarType.fixed,我对此也很陌生,从我的应用程序来看,我只是在尝试添加这个type: BottomNavigationBarType.fixed,

Try using CupertinoTabScaffold .尝试使用CupertinoTabScaffold In state changes, the color of the tab you are in will light up momentarily.在 state 更改中,您所在的选项卡的颜色会瞬间亮起。 You can also change this color with Theme.您也可以使用主题更改此颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM