如何为边框按钮添加边框?

时间:2018-09-12 20:03:10

标签: button dart icons flutter border

我最近才陷入困境,到目前为止,我一直很喜欢它,但是我一直在进行一些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,
          ),
        )),
  ],
);

它产生以下内容:see screenshot

我想要这个:see second screenshot

5 个答案:

答案 0 :(得分:7)

只需将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: () {},
  ),
),

答案 1 :(得分:5)

你好凯瑟琳,欢迎您!

通过更深入地研究组成MaterialButton的小部件,可以实现所需的目标。

首先,您需要Ink小部件。这样可以使用BoxDecoration提供更灵活的样式。

然后

Ink可以包含一个InkWell小部件,该小部件可以识别onTap并绘制飞溅效果。默认情况下,飞溅会继续到包含框的边缘,但是您可以通过给InkWell很大的borderRadius使其呈圆形。

这是您要使用的按钮的示例:

Material(
  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,
        ),
      ),
    ),
  )
),

结果如下:

screenshot of Flutter app with custom styled button

答案 2 :(得分:4)

可以使用带边框的 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,
                              ),
                            )

答案 3 :(得分:1)

在 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.
  }
}

答案 4 :(得分:0)

对于TextButton

style 中使用 sideMaterialStatePropertyBorderSide

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