Adding a border to a widget in Flutter

Suragch
3 min readDec 30, 2018

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

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. In the following examples I will use 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

--

--