减少图标和文本之间的间距 - Flutter OutlinedButton

发布于 2025-01-11 18:43:28 字数 748 浏览 0 评论 0原文

有没有一种简单的方法可以减少 OutlinedButton 上图标和文本之间的间距?

下面是我的代码。做了一些不同的尝试,但没有成功。

OutlinedButton.icon(
  onPressed: () {},
  icon: Icon(Icons.flash_on_outlined, size: 20.0),
  label: Text(
    'Surge',
    style: TextStyle(
      fontSize: 15.0,
      fontWeight: FontWeight.bold,
      color: Colors.blue,
    ),
  ),
  style: OutlinedButton.styleFrom(
    padding: EdgeInsets.zero,
    //fixedSize: Size(40, 25),
    backgroundColor: Colors.blue[100],
    side: BorderSide(
      color: Colors.blue,
      width: 1,
    ),
  ),
),

输入图片此处描述

Is there an easy way to reduce the space between the icon and text on an OutlinedButton?

Below is my code. Have made a few different attempts but no luck.

OutlinedButton.icon(
  onPressed: () {},
  icon: Icon(Icons.flash_on_outlined, size: 20.0),
  label: Text(
    'Surge',
    style: TextStyle(
      fontSize: 15.0,
      fontWeight: FontWeight.bold,
      color: Colors.blue,
    ),
  ),
  style: OutlinedButton.styleFrom(
    padding: EdgeInsets.zero,
    //fixedSize: Size(40, 25),
    backgroundColor: Colors.blue[100],
    side: BorderSide(
      color: Colors.blue,
      width: 1,
    ),
  ),
),

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁憨憨 2025-01-18 18:43:28

您正在使用OutlinedButton.icon。如果您查看其源代码,您会发现它根本没有什么神奇之处:它只是将图标和文本放在 Row 中,并在中间放置一个 SizedBox作为间隙,其源代码如下:

// Flutter source code: `outlined_button.dart`, line 378.
final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
return Row(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[icon, SizedBox(width: gap), Flexible(child: label)],
);

因此,如果您不喜欢这个默认的 8 单位间隙,则只需不要使用 .icon 构造函数即可。只需使用普通的构造函数并传入一个 Row 作为它的子元素,可以有任何你想要的间隙:

OutlinedButton(
  child: Row(
    mainAxisSize: MainAxisSize.min,
    children: const [
      Icon(Icons.star),
      SizedBox(width: 8),
      Text('Add to bookmark'),
    ],
  ),
  onPressed: () {},
)

You are using OutlinedButton.icon. If you look into its source code, you'll see that it's nothing magical at all: it simply puts your icon and text in a Row and places a SizedBox in the middle as a gap, its source code is as follows:

// Flutter source code: `outlined_button.dart`, line 378.
final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
return Row(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[icon, SizedBox(width: gap), Flexible(child: label)],
);

So, if you don't like this default 8-unit gap, simply don't use .icon constructor. Just use the normal constructor and pass in a Row as its child, with whatever gap you want:

OutlinedButton(
  child: Row(
    mainAxisSize: MainAxisSize.min,
    children: const [
      Icon(Icons.star),
      SizedBox(width: 8),
      Text('Add to bookmark'),
    ],
  ),
  onPressed: () {},
)
唐婉 2025-01-18 18:43:28
  OutlinedButton.icon(
    onPressed: () {},
    icon: Wrap(
      // mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Icon(Icons.flash_on_outlined, size: 20.0),
        Text(
          'Surge',
          style: TextStyle(
            fontSize: 15.0,
            fontWeight: FontWeight.bold,
            color: Colors.blue,
          ),
        )
      ],
    ),
    label: Text(""),
    style: OutlinedButton.styleFrom(
      padding: EdgeInsets.zero,
      //fixedSize: Size(40, 25),
      backgroundColor: Colors.blue[100],
      side: BorderSide(
        color: Colors.blue,
        width: 1,
      ),
    ),
  )

输入图片此处描述

  OutlinedButton.icon(
    onPressed: () {},
    icon: Wrap(
      // mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Icon(Icons.flash_on_outlined, size: 20.0),
        Text(
          'Surge',
          style: TextStyle(
            fontSize: 15.0,
            fontWeight: FontWeight.bold,
            color: Colors.blue,
          ),
        )
      ],
    ),
    label: Text(""),
    style: OutlinedButton.styleFrom(
      padding: EdgeInsets.zero,
      //fixedSize: Size(40, 25),
      backgroundColor: Colors.blue[100],
      side: BorderSide(
        color: Colors.blue,
        width: 1,
      ),
    ),
  )

enter image description here

咽泪装欢 2025-01-18 18:43:28

我认为实现这种情况的最佳方法是将图标按钮替换为带有 RichText 作为子项的按钮:

OutlinedButton(
          onPressed: () {},
          child: RichText(
            text: TextSpan(children: [
              // Icon as a font character
              TextSpan(
                text: String.fromCharCode(Icons.flash_on_outlined.codePoint),
                style: TextStyle(
                  color: Colors.amber,
                  fontFamily: Icons.flash_on_outlined.fontFamily,
                  package: Icons.flash_on_outlined.fontPackage,
                ),
              ),
              // Button text
              TextSpan(
                text: 'Surge',
                style: TextStyle(
                  fontSize: 15.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                ),
              ),
            ]),
          ),
          style: OutlinedButton.styleFrom(
            backgroundColor: Colors.blue[100],
            side: BorderSide(
              color: Colors.blue,
              width: 1,
            ),
          ),
        )

此解决方案删除了​​图标的任何边距。

The best way to implement such case I think is to replace icon button with button with RichText as a child:

OutlinedButton(
          onPressed: () {},
          child: RichText(
            text: TextSpan(children: [
              // Icon as a font character
              TextSpan(
                text: String.fromCharCode(Icons.flash_on_outlined.codePoint),
                style: TextStyle(
                  color: Colors.amber,
                  fontFamily: Icons.flash_on_outlined.fontFamily,
                  package: Icons.flash_on_outlined.fontPackage,
                ),
              ),
              // Button text
              TextSpan(
                text: 'Surge',
                style: TextStyle(
                  fontSize: 15.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                ),
              ),
            ]),
          ),
          style: OutlinedButton.styleFrom(
            backgroundColor: Colors.blue[100],
            side: BorderSide(
              color: Colors.blue,
              width: 1,
            ),
          ),
        )

This solution removes any margin from an icon.

看海 2025-01-18 18:43:28

您的尺寸,关于图标和文本,是块还是可以修改它?

your size, about the icon and the text, are block or you can modify it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文