close

How to add a border to a widget in Flutter?

This is a repost of an answer I composed on Stack Overflow.

Image for post

There are two or three different ways to add a fringe to a Flutter gadget. The most essential route is to envelop your gadget by a DecoratedBox. Notwithstanding, the Container gadget additionally has a DecoratedBox implicit. In the accompanying models I will utilize Container for the comfort of adding edge and cushioning.

Here is the overall arrangement.

Image for post
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

Image for post

These have an outskirt width of 1, 3, and 10 individually.

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

Border color

Image for post

These have a fringe shade 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

Image for post

These have a fringe side of

  • left (3.0), top (3.0)
  • base (13.0)
  • left (blue[100], 15.0), top (blue[300], 10.0), right (blue[500], 5.0), base (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,
      ),
    ),
  );
}

Fringe span

The span influences the roundness of the corners.

Image for post

These have fringe radii of 5, 10, and 30 separately.

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment