如何在Flutter中调整IconButton的大小(高度和宽度)

Ste*_*ane 9 flutter

如何在Flutter中调整IconButton的大小(高度和宽度)?看起来它需要默认的宽度和高度.没有高度或宽度属性.

new IconButton(
    padding: new EdgeInsets.all(0.0),
    color: themeData.primaryColor,
    icon: new Icon(Icons.clear, size: 18.0),
    onPressed: onDelete,
)
Run Code Online (Sandbox Code Playgroud)

Ste*_*ane 25

您可以使用SizedBox强制它自行调整大小.

new SizedBox(
   height: 18.0,
   width: 18.0,
   child: new IconButton(
      padding: new EdgeInsets.all(0.0),
      color: themeData.primaryColor,
      icon: new Icon(Icons.clear, size: 18.0),
      onPressed: onDelete,
   )
)
Run Code Online (Sandbox Code Playgroud)


adr*_*ntu 25

有一种比接受的答案更新的方法。它看起来像这样:

IconButton(
  iconSize: 18.0,
  icon: new Icon(Icons.clear)
Run Code Online (Sandbox Code Playgroud)

所以使用 iconSize 属性并摆脱 SizedBox。

我注意到按下按钮时,旧的已接受解决方案的绘图效果不佳。

  • iconSize 属性仅更改图标大小本身,而不更改它的容器(实际上是一个 SizedBox)所以我认为更改按钮大小的唯一方法是提供您自己的 SizedBox 包装器,正如接受的答案所暗示的那样 (22认同)
  • 这是不正确的。它不会改变按钮的大小,只会改变图标。 (6认同)
  • 人们读错了吗?这显然是最正确的做法。说它不正确的人一定是使用了“Icon”的“size”属性,而不是“IconButton”的“iconSize”属性。 (3认同)
  • 为什么你觉得需要-1?这段代码当然可以工作,我在我的项目中使用它。可能是我们使用了不同的小部件。请在快速 -1-ing 并说“唯一的方法......”之前考虑声誉。 (2认同)
  • 这应该被接受的答案。没有什么缺点 (2认同)

dfm*_*ler 18

您可以用 InkWell 替换 IconButton:

InkWell(
    child: Icon(Icons.clear, size: 18.0, color: themeData.primaryColor),
    onTap: onDelete,
),
Run Code Online (Sandbox Code Playgroud)


小智 11

如果有人想要更改图标按钮的启动/悬停阴影大小。您需要将 IconButton 中的splashRadius 属性设置为所需的值。

IconButton(
     splashRadius: 12,
     padding: EdgeInsets.zero,
     icon: Icon(
             Icons.visibility,
              color: Theme.of(context).primaryColorDark,
    ),
)
Run Code Online (Sandbox Code Playgroud)


and*_*rei 5

使用该constraints属性IconButton

IconButton(
    constraints: BoxConstraints(maxHeight: 36),
    icon: new Icon(Icons.clear, size: 18.0),
)
Run Code Online (Sandbox Code Playgroud)

来源


jos*_*oid 5

使用IconButton > splashRadius

IconButton(
  // use this to decrease/increase the splash spacing
  splashRadius: 24.0,  // (Material.defaultSplashRadius = 35.0)
  color: buttonColor,
  icon: Icon(Icons.heart),
  onPressed: () {},
);
Run Code Online (Sandbox Code Playgroud)

演示图标按钮