如何为 flutter 按钮添加边框?

How do I add a border to a flutter button?

我最近刚开始使用 flutter,到目前为止我很喜欢它,但我在一些 UI 更改上遇到了困难。感谢您的帮助!

我的目标是获得一个圆形按钮,该按钮具有蓝色背景图标,但外部边框为深蓝色。附有图片

我的方法是:

  1. 获取圆形蓝色按钮。
  2. 在该按钮中放置一个图标。
  3. 添加边框。

我卡在了第 3 步,因为我不知道如何添加边框,或者如果考虑到我处理问题的方式是否可能的话。具体颜色我暂时无所谓,以后换主题。

这是我目前的代码:

  var messageBtn = new Row(
  children: <Widget>[
    new Padding(
      padding: const EdgeInsets.all(20.0),
      child: new RawMaterialButton(
        onPressed: _messages,
        child: new Padding(
          padding: const EdgeInsets.all(20.0),
          child: new Icon(
            Icons.message,
            size: 30.0,
            color: Colors.white,
          ),
        ),
        shape: new CircleBorder(),
        fillColor: Colors.deepPurple,
      ),
    ),
    new Padding(
        padding: const EdgeInsets.all(8.0),
        child: new Text(
          'Send Messages',
          style: new TextStyle(
            fontSize: 20.0,
          ),
        )),
  ],
);

它产生这个:

我想要这个:

嗨,凯瑟琳,欢迎光临!

您可以通过更深入地研究构成 MaterialButton 的小部件来实现您想要的。

首先你需要 Ink widget. This offers more flexible styling using a BoxDecoration.

Ink 然后可以包含一个 InkWell 小部件,它可以识别 onTap 并绘制飞溅效果。默认情况下,飞溅会继续到包含框的边缘,但您可以通过给 InkWell 一个非常大的 borderRadius.

来使其呈圆形

这是您所关注的按钮示例:

Material(
  type: MaterialType.transparency, //Makes it usable on any background color, thanks @IanSmith
  child: Ink(
    decoration: BoxDecoration(
      border: Border.all(color: Colors.indigoAccent, width: 4.0),
      color: Colors.indigo[900],
      shape: BoxShape.circle,
    ),
    child: InkWell(
      //This keeps the splash effect within the circle
      borderRadius: BorderRadius.circular(1000.0), //Something large to ensure a circle
      onTap: _messages,
      child: Padding(
        padding:EdgeInsets.all(20.0),
        child: Icon(
          Icons.message,
          size: 30.0,
          color: Colors.white,
        ),
      ),
    ),
  )
),

结果如下:

只需将 IconButton 包装到 Container 中并将其 decoration 设置如下:

Container(
  decoration: BoxDecoration(
    border: Border.all(color: Colors.blue, width: 4),
    color: Colors.yellow,
    shape: BoxShape.circle,
  ),
  child: IconButton(
    iconSize: 56,
    icon: Icon(Icons.check),
    onPressed: () {},
  ),
),

可以使用带边框的 FloatingActionButton :

   FloatingActionButton(
                              mini: false,
                              backgroundColor: Colors.blue.shade900,
                              splashColor: Colors.black,
                              onPressed: () {
                                logOutDialog(context);
                              },
                              hoverElevation: 1.5,
                              shape: StadiumBorder(
                                  side: BorderSide(
                                      color: Colors.blue, width: 4)),
                              elevation: 1.5,
                              child: Icon(
                                Icons.logout,
                                color: _foregroundColor,
                              ),
                            )

在 Flutter 2 中有 TextButton:

TextButton(
  style: ButtonStyle(
   side: RedSelectedBorderSide(),
  ),
  child: Text(
    "Button"
  ),
  onPressed: (){}
);

其中 RedSelectedBorderSide() 是:

class RedSelectedBorderSide extends MaterialStateBorderSide {
  @override
  BorderSide resolve(Set<MaterialState> states) {
    if (states.contains(MaterialState.selected)) {
      return BorderSide(
        width: 2,
        color: Colors.red,
      ); // 
    }
    return null;// Defer to default value on the theme or widget.
  }
}

对于TextButton

style 中使用 sideMaterialStateProperty 以及 BorderSide

TextButton(
  style: ButtonStyle(
   side: MaterialStateProperty.all(
     BorderSide(width: 1, color: Colors.black),
   ),
  ),
  child: Text(
    "My Button"
  ),
  onPressed: (){}
);