简体   繁体   English

Flutter:是否可以在单击(onPressed)时调整 IconButton 的大小?

[英]Flutter: is it possible to resize IconButton when it is clicked (onPressed)?

I have a row, whose children are few buttons.我有一排,谁家的孩子都是几个纽扣。 I use them to pick color of my item, and when one icon is clicked I save color on server.我用它们来选择我的项目的颜色,当单击一个图标时,我将颜色保存在服务器上。 Now, I would like the chosen Icon to resize (to get bigger).现在,我想调整所选图标的大小(变大)。 So that it really look like "selected".使它看起来真的像“选中”。

Do you have any tips how to achieve that?你有什么技巧可以实现吗?

Here is my code for a single icon.这是我的单个图标代码。 Others are completely same, only with another color.其他的完全一样,只是用了另一种颜色。

IconButton(
   icon: Ink(
        decoration: BoxDecoration(
        border: Border.all(color: yellow, width: 1),
        color: yellow,
        borderRadius: BorderRadius.circular(50.0)),
        child: const Icon(
           Icons.circle,
           color: yellow,
           size: 30,
            ),
       ),
   onPressed: () async {

      // how to resize when clicked? 
                                    
     item.set('color', 'yellow');
     item.save();
      },
  ),

Let's use a boolean variable to track the state of the icon.让我们使用一个 boolean 变量来跟踪图标的 state。 When the icon is clicked, you can toggle the value of the boolean variable and use it to control the size of the icon, this should be perfect for what you want to do.单击图标时,您可以切换 boolean 变量的值并使用它来控制图标的大小,这应该非常适合您想要执行的操作。

We will make the Icon reusable and expose its properties, you can define it as a custom widget and pass the necessary parameters as arguments.我们将使Icon可重用并公开其属性,您可以将其定义为自定义小部件并将必要的参数传递为 arguments。

Here is an example of how you can do this:以下是如何执行此操作的示例:

// Feel free to name the widget any name, I called it 'ColorIcon`.

class ColorIcon extends StatefulWidget {
  final Color color;
  final Function? onPressed;

  ColorIcon({require this.color, this.onPressed});

  @override
  _ColorIconState createState() => _ColorIconState();
}

class _ColorIconState extends State<ColorIcon> {
  bool _isSelected = false;

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Ink(
        decoration: BoxDecoration(
          border: Border.all(color: widget.color, width: 1),
          color: widget.color,
          borderRadius: BorderRadius.circular(50.0)),
        child: Icon(
          Icons.circle,
          color: widget.color,
          size: _isSelected ? 50 : 30,
        ),
      ),
      onPressed: () {
        setState(() {
          _isSelected = !_isSelected;
        });
        widget.onPressed();
      },
    );
  }
}

You can then use the ColorIcon widget like this:然后,您可以像这样使用ColorIcon小部件:

ColorIcon(
  color: yellow,
  onPressed: () {
    item.set('color', 'yellow');
    item.save();
  },
),

This will allow you to reuse the ColorIcon widget for multiple icons and customize its behaviour by passing different values for the color and onPressed arguments while achieving the resize functionality.这将允许您为多个图标重用ColorIcon小部件,并通过传递不同的颜色值和onPressed arguments 来自定义其行为,同时实现调整大小功能。

To change the icon size dynamically, you can hold the size as a variable:要动态更改图标大小,您可以将大小保存为变量:

var _iconSize = 30;

And then:接着:

Icon(
          Icons.circle,
          color: bikeyellow,
          size: _iconSize, //<-- SEE HERE
        ),

And in your onPressed setState the variable to something bigger:在您的onPressed setState中,将变量设置为更大的值:

onPressed: ()  {
        setState(() {
          _iconSize = 100;
        });
      }

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

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