How to add a border to a widget in Flutter?

后端 未结 7 597
别那么骄傲
别那么骄傲 2020-11-30 18:38

I\'m using Flutter and I\'d like to add a border to a widget (in this case, a Text widget).

I tried TextStyle and Text, but I didn\'t see how to add a border.

相关标签:
7条回答
  • 2020-11-30 19:16

    Here is an expanded answer. A DecoratedBox is what you need to add a border, but I am using a Container for the convenience of adding margin and padding.

    Here is the general setup.

    Widget myWidget() {
      return Container(
        margin: const EdgeInsets.all(30.0),
        padding: const EdgeInsets.all(10.0),
        decoration: myBoxDecoration(), //             <--- BoxDecoration here
        child: Text(
          "text",
          style: TextStyle(fontSize: 30.0),
        ),
      );
    }
    

    where the BoxDecoration is

    BoxDecoration myBoxDecoration() {
      return BoxDecoration(
        border: Border.all(),
      );
    }
    

    Border width

    These have a border width of 1, 3, and 10 respectively.

    BoxDecoration myBoxDecoration() {
      return BoxDecoration(
        border: Border.all(
          width: 1, //                   <--- border width here
        ),
      );
    }
    

    Border color

    These have a border color of

    • Colors.red
    • Colors.blue
    • Colors.green

    Code

    BoxDecoration myBoxDecoration() {
      return BoxDecoration(
        border: Border.all(
          color: Colors.red, //                   <--- border color
          width: 5.0,
        ),
      );
    }
    

    Border side

    These have a border side of

    • left (3.0), top (3.0)
    • bottom (13.0)
    • left (blue[100], 15.0), top (blue[300], 10.0), right (blue[500], 5.0), bottom (blue[800], 3.0)

    Code

    BoxDecoration myBoxDecoration() {
      return BoxDecoration(
        border: Border(
          left: BorderSide( //                   <--- left side
            color: Colors.black,
            width: 3.0,
          ),
          top: BorderSide( //                    <--- top side
            color: Colors.black,
            width: 3.0,
          ),
        ),
      );
    }
    

    Border radius

    These have border radii of 5, 10, and 30 respectively.

    BoxDecoration myBoxDecoration() {
      return BoxDecoration(
        border: Border.all(
          width: 3.0
        ),
        borderRadius: BorderRadius.all(
            Radius.circular(5.0) //                 <--- border radius here
        ),
      );
    }
    

    Going on

    DecoratedBox/BoxDecoration are very flexible. Read Flutter — BoxDecoration Cheat Sheet for many more ideas.

    0 讨论(0)
  • 2020-11-30 19:22

    Best way is using BoxDecoration()

    Advantage

    • You can set border of widget
    • You can set border Color or Width
    • You can set Rounded corner of border
    • You can add Shadow of widget

    Disadvantage

    • BoxDecoration only use with Container widget so you want to wrap your widget in Container()

    Example

        Container(
          margin: EdgeInsets.all(10),
          padding: EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: Colors.orange,
            border: Border.all(
                color: Colors.pink[800],// set border color
                width: 3.0),   // set border width
            borderRadius: BorderRadius.all(
                Radius.circular(10.0)), // set rounded corner radius
            boxShadow: [BoxShadow(blurRadius: 10,color: Colors.black,offset: Offset(1,3))]// make rounded corner of border
          ),
          child: Text("My demo styling"),
        )
    

    0 讨论(0)
  • 2020-11-30 19:23

    As stated in the documentation, flutter prefer composition over parameters. Most of the time what you're looking for is not a property, but instead a wrapper (and sometimes a few helpers/"builder")

    For borders what you want is DecoratedBox, which has a decoration property that defines borders ; but also background images or shadows.

    Alternatively like @Aziza said, you can use Container. Which is the combination of DecoratedBox, SizedBox and a few other useful widgets.

    0 讨论(0)
  • 2020-11-30 19:23

    Using BoxDecoration() is the best way to show border.

    Container(
      decoration: BoxDecoration(
        border: Border.all(
        color: Color(0xff000000),
        width: 4,
      )),
      child: //Your child widget
    ),
    

    You can also view full format here

    0 讨论(0)
  • 2020-11-30 19:35

    Use a container with Boxdercoration.

     BoxDecoration(
        border: Border.all(
          width: 3.0
        ),
        borderRadius: BorderRadius.circular(10.0)
      );
    
    0 讨论(0)
  • 2020-11-30 19:41

    You can add the TextField as a child to a Container that has a BoxDecoration with border property:

    new Container(
      margin: const EdgeInsets.all(15.0),
      padding: const EdgeInsets.all(3.0),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.blueAccent)
      ),
      child: Text("My Awesome Border"),
    )
    
    0 讨论(0)
提交回复
热议问题