简体   繁体   English

如何在按下时立即更改图标按钮颜色(颤动)

[英]how to change iconbutton colour immediately on pressed (flutter)

This code works fine with only one iconbutton.此代码仅适用于一个图标按钮。 But when I create multiple iconbuttons, the scenario is different.但是当我创建多个图标按钮时,情况就不同了。 With three(3) icon buttons, on pressing anyone(1) icon, all the three(3) icons changes it's colour.使用三 (3) 个图标按钮,在按下任何 (1) 个图标时,所有三 (3) 个图标都会改变其颜色。 How to change colour individually?如何单独更改颜色?

class _userProfileScreenState extends State { Color _iconcolor = Colors.black; class _userProfileScreenState 扩展 State { 颜色 _iconcolor = Colors.black;


IconButton(
      onPressed: () {
      setState(() {
          _iconcolor = Color(0xff187bcd);
           },);
      },
      icon: Icon(
          FontAwesomeIcons.mars,
          color: _iconcolor, //male
          size: 45,
          ),
      ),

You could do something like this你可以做这样的事情

class SomeState extends State<StatefulWidget> {
  Color _iconColor = Colors.white;

  @override
  Widget build(BuildContext) {
    return ListTile(
      leading: new IconButton(
        icon: Icon(Icons.star, color: _iconColor),
        onPressed: () {
          setState(() {
          _iconColor = Colors.yellow;
        });
      },
    );
  }
}

This is simple, try creating 3 variables which hold color value for 3 buttons这很简单,尝试创建 3 个变量来保存 3 个按钮的颜色值

class SomeState extends State<StatefulWidget> {
  Color _iconColor1 = Colors.white;
  Color _iconColor2 = Colors.black;
  Color _iconColor3 = Colors.green
  @override
  Widget build(BuildContext) {
    return Row(
        children:[
        IconButton(
            icon: Icon(Icons.share, color: _iconColor1,),
            
            onPressed: () {
                 setState((){
                      _iconColor1= //setNew Color
                 });
           }     
        ),
        //enter 2 more icons like this and change color to different variable.
       ]
    )
}
}

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

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