mat*_*olo 4 flutter iconbutton
我有一个带有多个 IconButton 的行,我需要更改它们的颜色和大小。我设法更改了颜色,但无法更改图标大小。
IconTheme(
data: IconThemeData(size: 48.0, color: Colors.yellow),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
onPressed: () => null,
),
IconButton(
icon: Icon(Icons.file_upload),
onPressed: () => _addPhoto(false),
),
IconButton(
icon: Icon(Icons.camera_alt),
onPressed: () => _addPhoto(true),
),
],
),
),
Run Code Online (Sandbox Code Playgroud)
如果我使用 iconSize 在 IconButtons 中设置大小,它会起作用,但使用 IconTheme 则不会。
我该如何解决?
如官方文档中所定义,请在此处链接:
此属性不得为空。默认为 24.0。此处给出的大小通过 IconTheme 传递给图标属性中的小部件。例如,在此处而不是在中设置大小,Icon.size 属性允许 IconButton 调整启动区域的大小以适合 Icon。如果您要使用 Icon.size 设置图标的大小,则 IconButton 将默认为 24.0,然后图标本身可能会被剪裁。
因此,需要为 IconButton 赋予 iconSize 属性,因为这会覆盖 IconTheme 大小属性。如果您希望您的按钮具有从 IconTheme 派生的大小,那么您应该制作您的自定义 IconButton,为您设置 iconSize。例如:
class CustomIconButton extends StatelessWidget {
CustomIconButton({Key key, this.onPressed, this.icon});
final Function onPressed;
final Icon icon;
@override
Widget build(BuildContext context) {
IconThemeData iconThemeData = IconTheme.of(context);
return IconButton(
onPressed: onPressed, iconSize: iconThemeData.size, icon: icon);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3060 次 |
| 最近记录: |