Add border to a Container with borderRadius in Flutter

后端 未结 4 721
臣服心动
臣服心动 2021-02-03 21:01
Container(
      child: Text(
          \'This is a Container\',
          textScaleFactor: 2,
          style: TextStyle(color: Colors.black),
      ),

      decoratio         


        
相关标签:
4条回答
  • 2021-02-03 21:03

    There is an answer here

    Container(
                decoration: BoxDecoration(
                    border: Border.all(
                        color: Color(0xFFF05A22),
                        style: BorderStyle.solid,
                        width: 1.0,
                    ),
                    color: Colors.transparent,
                    borderRadius: BorderRadius.circular(30.0),
                ),
    ),
    
    0 讨论(0)
  • 2021-02-03 21:15

    There are a couple ways to add a border to a Flutter widget. The most basic way is to wrap your widget in a DecoratedBox. However, the Container widget also has a DecoratedBox built in.

    For output as above use a Stack instead of Row because of Stack allows us to make multiple widgets overlay each other and you can align or position your widget using the Align or Positioned widget.

     Container(
      height: 65,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.deepPurple.shade100,
      ),
      child: Stack(
        children: <Widget>[
          Container(
            width: 8,
    
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(15),
                  bottomLeft: Radius.circular(15)),
              color: Colors.deepPurple,
            ),
          )
        ],
      ),
    )
    

    0 讨论(0)
  • 2021-02-03 21:16

    It's not possible to add border: and borderRadius: at the same time, you'll get this error:

    A borderRadius can only be given for uniform borders.

    You can achieve what you want using the borderRadius: and a boxShadow: instead of border: like this:

    boxShadow: [
      BoxShadow(color: Colors.green, spreadRadius: 3)
    ]
    

    Your sample code would be like this:

    Container(
      child: Text(
        'This is a Container',
        textScaleFactor: 2,
        style: TextStyle(color: Colors.black),
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.white,
        boxShadow: [
          BoxShadow(color: Colors.green, spreadRadius: 3),
        ],
      ),
      height: 50,
    ),
    

    Edit: To achieve the example you now provided, you could do this:

    Container(
      padding: EdgeInsets.only(left: 12.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Colors.green,
      ),
      height: 50,
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
              topRight: Radius.circular(10.0),
              bottomRight: Radius.circular(10.0)),
          color: Colors.white,
        ),
        child: Text(
          'This is a Container',
          textScaleFactor: 2,
          style: TextStyle(color: Colors.black),
        ),
      ),
    ),
    

    Another solution:

    Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Colors.white,
      ),
      height: 50,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            width: 12.0,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10.0),
                  bottomLeft: Radius.circular(10.0)),
              color: Colors.green,
            ),
          ),
          Text(
            'This is a Container',
            textScaleFactor: 2,
            style: TextStyle(color: Colors.black),
          )
        ],
      ),
    ),
    
    0 讨论(0)
  • 2021-02-03 21:22

    I think an easier way inspired by @pablo 's answer would be to just make a boxShadow with and offset but without any blur.

    decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(10),
            ),
            boxShadow: [
              // to make elevation
              BoxShadow(
                color: Colors.black45,
                offset: Offset(2, 2),
                blurRadius: 4,
              ),
              // to make the coloured border
              BoxShadow(
                color: Colors.blue,
                offset: Offset(0, 4),
              ),
            ],
          ),
    

    The decoration above will give us an elevated box which has a blue border in the bottom. Another benefit of this approcah is that you can use it with

    borderRadius: BorderRadius.circular(num)

    Which means you can have a container with all rounded sides + a colored border.

    Please note that the coloured border comes under the original shadow. This is done to prevent the elevation color from darkening the border.

    0 讨论(0)
提交回复
热议问题