【问题标题】:how to change iconbutton colour immediately on pressed (flutter)如何在按下时立即更改图标按钮颜色(颤动)
【发布时间】:2021-05-14 21:02:24
【问题描述】:

此代码仅适用于一个图标按钮。但是当我创建多个图标按钮时,情况就不同了。使用三 (3) 个图标按钮,在按下任何 (1) 个图标时,所有三 (3) 个图标都会改变其颜色。如何单独改变颜色?

类 _userProfileScreenState 扩展状态 { 颜色_iconcolor = Colors.black;


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

【问题讨论】:

  • 你尝试创建03变量来管理它吗?
  • 在看到此线程中的最终答案后尝试创建。无论如何,谢谢!

标签: flutter


【解决方案1】:

你可以这样做

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

【讨论】:

    【解决方案2】:

    这很简单,尝试创建 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.
           ]
        )
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 2021-01-28
      相关资源
      最近更新 更多