597

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.

krishnaacharyaa
26.1k9 gold badges102 silver badges138 bronze badges
asked Nov 21, 2017 at 21:48

25 Answers 25

1085

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

enter image description here

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'),
)
Victor Eronmosele
7,9942 gold badges13 silver badges40 bronze badges
answered Nov 21, 2017 at 22:47
Sign up to request clarification or add additional context in comments.

Comments

609

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.

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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.

answered Dec 8, 2018 at 6:08

6 Comments

Anyone know how to use BorderSide with BorderRadius?
@HaSnenTai Did you find any solution? In my design i need to give border bottom with pill like shape. How can i achieve this?
@RobertWilliams, I would use a custom drawing widget.
@Suragch The widget is Text which needs a strong (pill shaped) border. The text widget width is not fixed. For using custom drawing widget won't I need to provide fix properties?
U can also use the short-hand BorderRadius.circular(8) instead of BorderRadius.all(Radius.circular(5.0)).
|
40

The best way is using BoxDecoration()

Advantage

  • You can set the border of a widget
  • You can set the border Color or Width
  • You can set a Rounded corner of a border
  • You can add a Shadow of a 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"),
 )

Enter image description here

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Dec 3, 2019 at 9:13

Comments

35

You can wrap the widget with DecoratedBox which provides decoration :

Widget textDecoration(String text){
 return DecoratedBox(
 decoration: BoxDecoration(
 border: Border.all(
 color: Colors.red,
 width: 10,
 ),
 ),
 child: Text(text)
 );
}
Ahmed Sbai
16.5k11 gold badges34 silver badges54 bronze badges
answered Nov 2, 2021 at 8:26

Comments

32

Summary

I have tried to summarize all the important possibilities when using border in BoxDecoration.

Outcomes of the below explained borders:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


Explaination

Basic

enter image description here

 Container(
 decoration: BoxDecoration(border: Border.all()),
 child: const Text("Text"),
 ), 

BorderColor, width and strokeAlign

enter image description here

Container(
 decoration: BoxDecoration(
 border: Border.all(
 width: 4,
 color: Colors.green,
 strokeAlign: BorderSide.strokeAlignCenter)),
 child: const Text("Text"),
),

Border only on specific side

enter image description here

 Row(
 mainAxisAlignment: MainAxisAlignment.spaceAround,
 children: [
 Container(
 decoration: const BoxDecoration(
 border: Border(top: BorderSide(width: 2))),
 child: const Text("Text"),
 ),
 Container(
 decoration: const BoxDecoration(
 border: Border(bottom: BorderSide(width: 2))),
 child: const Text("Text"),
 ),
 Container(
 decoration: const BoxDecoration(
 border: Border(
 top: BorderSide(width: 2),
 bottom: BorderSide(width: 4))),
 child: const Text("Text"),
 ),
 ],
 ),

Different Shapes

enter image description here

 Row(
 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
 children: [
 Container(
 padding: const EdgeInsets.all(10),
 decoration: BoxDecoration(
 border: Border.all(),
 shape: BoxShape.circle),
 child: const Text("Text"),
 ),
 Container(
 padding: const EdgeInsets.all(10),
 decoration: BoxDecoration(
 border: Border.all(),
 borderRadius: BorderRadius.circular(10),
 ),
 child: const Text("Text"),
 ),
 ],
 ),

Curved Border Radius

enter image description here

 Row(
 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
 children: [
 Container(
 padding: const EdgeInsets.all(10),
 decoration: BoxDecoration(
 border: Border.all(),
 borderRadius: const BorderRadius.horizontal(
 left: Radius.circular(5), right: Radius.circular(20))
 ),
 child: const Text("Text"),
 ),
 Container(
 padding: const EdgeInsets.all(10),
 decoration: BoxDecoration(
 border: Border.all(),
 borderRadius: const BorderRadius.only(
 topLeft: Radius.circular(10),
 bottomRight: Radius.circular(20))),
 child: const Text("Text"),
 ),
 ],
 ),
answered Dec 28, 2022 at 16:06

Comments

21

As stated in the documentation, Flutter prefers composition over parameters.

Most of the time you're not looking for a property, but instead a wrapper (and sometimes a few helpers/"builder").

For borders, you want 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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Nov 21, 2017 at 23:15

Comments

13

Here, as the Text widget does not have a property that allows us to define a border, we should wrap it with a widget that allows us to define a border. There are several solutions. But the best solution is the use of BoxDecoration in the Container widget.

Why choose to use BoxDecoration?

Because BoxDecoration offers more customization like the possibility to define:

First, the border and also define:

  • border Color
  • border width
  • border radius
  • shape
  • and more ...

An example:

 Container(
 child:Text(' Hello Word '),
 decoration: BoxDecoration(
 color: Colors.yellow,
 border: Border.all(
 color: Colors.red ,
 width: 2.0 ,
 ),
 borderRadius: BorderRadius.circular(15),
 ),
 ),

Output:

Enter image description here

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Dec 10, 2020 at 14:33

Comments

9

Wrap that widget with the container

Container(
 margin: const EdgeInsets.all(30.0),
 padding: const EdgeInsets.all(10.0),
 decoration: BoxDecoration(border: Border.all(
 color: Colors.black,
 width: 1,
 ),
 ), 
 child: Text(
 "text",
 style: TextStyle(fontSize: 30.0),
 ),
 );
m4n0
32.6k28 gold badges81 silver badges98 bronze badges
answered Sep 25, 2021 at 8:27

Comments

9

The above answers are also correct, but in case you want to add multiple borders at the same widget, then you can set this

Container(
 child: const Center(
 child: Text(
 'This is your Container',
 ),
 ),
 decoration: BoxDecoration(
 borderRadius: BorderRadius.circular(10),
 color: Colors.white,
 boxShadow: const [
 BoxShadow(color: Colors.green, spreadRadius: 8),
 BoxShadow(color: Colors.yellow, spreadRadius: 5),
 ],
 ),
 height: 50,
 )

enter image description here

answered Mar 7, 2022 at 11:42

Comments

6

You can use Container to contain your widget:

Container(
 decoration: BoxDecoration(
 border: Border.all(
 color: Color(0xff000000),
 width: 1,
 )),
 child: Text()
),
CoderUni
6,2049 gold badges31 silver badges62 bronze badges
answered Jun 5, 2020 at 11:20

1 Comment

This does not anything over the accepted answer from 3 years prior.
5

Use a container with Boxdercoration.

 BoxDecoration(
 border: Border.all(
 width: 3.0
 ),
 borderRadius: BorderRadius.circular(10.0)
 );
Chris Catignani
5,30819 gold badges46 silver badges54 bronze badges
answered Jul 5, 2020 at 10:46

1 Comment

This does not anything over the accepted answer from 3 years prior.
4

In case someone would like a outlined/bordered text or apply multiple borders.

You could try this:

https://pub.dev/packages/outlined_text

enter image description here

DEMO

answered May 29, 2021 at 21:10

Comments

4

You can use this method for adding border colour for a container.

Container(
 height: height * 0.06,
 width: width,
 alignment: Alignment.center,
 decoration: BoxDecoration(
 border: Border.all(
 color: primaryColor,
 ),
 borderRadius: BorderRadius.circular(10),
 ),
 child: Text(
 "data",
 style: const TextStyle(
 fontWeight: FontWeight.bold
 ),
 ),
 );
answered Oct 18, 2023 at 13:05

Comments

3

You can achieve bottom rounded corner with three side borders.

Stack(
children: [
 Container(
 decoration: BoxDecoration(
 border: Border.all(color: Colors.red, width: 2),
 borderRadius: const BorderRadius.only(
 topLeft: Radius.zero,
 topRight: Radius.zero,
 bottomLeft: Radius.circular(20),
 bottomRight: Radius.circular(20),
 ),
 ),
 child: Padding(
 padding: const EdgeInsets.all(20.0),
 child: Column(
 crossAxisAlignment: CrossAxisAlignment.start,
 children: [
 const SizedBox(
 height: 1,
 ),
 Text("Dummy text"),
 const SizedBox(
 height: 10,
 ),
 ],
 ),
 ),
 ),
 Padding(
 padding: const EdgeInsets.only(left: 2, right: 2),
 child: Container(
 color: AppColors.highLighterBg,
 width: size.width,
 height: 10,
 ),
 )
],

);

answered Oct 4, 2023 at 13:18

Comments

3
 In Flutter, you can add a border to a widget using the Container widget and its decoration property. The BoxDecoration class allows you to define various visual elements, including borders.
 import 'package:flutter/material.dart';
void main() {
 runApp(MyApp());
}
class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 return MaterialApp(
 home: Scaffold(
 appBar: AppBar(
 title: Text('Border Example'),
 ),
 body: Center(
 child: Container(
 width: 200,
 height: 100,
 decoration: BoxDecoration(
 border: Border.all(
 color: Colors.blue, // Set border color
 width: 2.0, // Set border width
 ),
 borderRadius: BorderRadius.circular(8.0), // Set border radius
 ),
 child: Center(
 child: Text(
 'Hello, Border!',
 style: TextStyle(fontSize: 16),
 ),
 ),
 ),
 ),
 ),
 );
 }
}
Try this solution from latest version of Flutter V3.16
answered Jan 12, 2024 at 13:20

Comments

3

In Flutter, the Text widget itself does not have a direct property for adding a border. However, you can achieve this effect in multiple ways, and this an a way:

Using Container with BoxDecoration

Container(
 decoration: BoxDecoration(
 border: Border.all(color: Colors.blue, width: 2), 
 ),
 padding: EdgeInsets.all(8), // Optional padding
 child: Text(
 'Hello, Flutter!',
 style: TextStyle(fontSize: 20),
 ),
)
answered Feb 18 at 16:08

Comments

2

If you want to add border to some text of container then you can easily to do it by applying BoxDecoration to Container.

code :

Container(
 decoration: BoxDecoration(
 border: Border.all(
 color: Colors.redAccent,
 width: 1,
 ),
 ),
 child: Text('Some Text'),
);
Mol0ko
3,3082 gold badges24 silver badges50 bronze badges
answered May 5, 2021 at 5:15

Comments

2

Rounded corner/border with bottom shadow

Container(
 // child it's depend on your requirement
 child: const Center(
 child: Text(
 'This is your Container',
 ),
 ),
 decoration: BoxDecoration(
 borderRadius: BorderRadius.circular(10),
 color: Colors.white,
 boxShadow: <BoxShadow>[
 // shadow color and radius
 BoxShadow(
 color: Colors.black54,
 blurRadius: 15.0,
 offset: Offset(0.0, 0.75)
 )
 ],
 ),
 // according your height ex. 50
 height: 50,
);
answered Aug 16, 2022 at 13:03

Comments

2

Try the following code:

Container(
 margin: margin,
 padding: padding,
 decoration: BoxDecoration(
 border: Border.all(
 color: color,
 width: width,
 ),
 ),
 child: Text(
 data,
 style: TextStyle(fontSize: 30.0),
 ),
),
answered Sep 11, 2022 at 0:41

Comments

2

Text border style:

Stack(
 children: <Widget>[
 // Stroked text as border.
 Text(
 'Greetings, planet!',
 style: TextStyle(
 fontSize: 40,
 foreground: Paint()
 ..style = PaintingStyle.stroke
 ..strokeWidth = 6
 ..color = Colors.blue[700]!,
 ),
 ),
 // Solid text as fill.
 Text(
 'Greetings, planet!',
 style: TextStyle(
 fontSize: 40,
 color: Colors.grey[300],
 ),
 ),
 ],
)
answered Jan 3, 2023 at 8:13

Comments

2

Yes, there's different approaches for that. One of them is: Wrap it in a container. And use box decoration like this.

Container(
 padding: const EdgeInsets.all(16.0),
 decoration: BoxDecoration(
 border: Border.all(width: 5, color: Colors.red),
 borderRadius: BorderRadius.all(Radius.circular(50)),
 ), 
 child: const Text(
 "Box decoration",
 style: TextStyle(fontSize: 34.0),
 ),
)
answered Feb 7, 2023 at 11:46

Comments

2

Container is expensive, if you only want to add a border to a widget use DecoratedBox instead:

DecoratedBox(
 decoration: BoxDecoration(
 border: Border.all(color: appColorScheme(context).secondary),
 borderRadius: BorderRadius.circular(CupertinoContextMenu.kOpenBorderRadius),
 ),
 child: //Your Widget,
)
answered Jun 3, 2024 at 20:10

Comments

1

use the Text widget inside a container and use decoration to border the text:

decoration: BoxDecoration(
 border: Border.all(
 color: Color(0xff000000),
 width: 1,
)),
Ahmed Sbai
16.5k11 gold badges34 silver badges54 bronze badges
answered Jan 24, 2023 at 16:14

Comments

0

Just wrap the widget with container and decorate the container as you want.

answered Dec 14, 2023 at 4:39

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

To add a border to your Text widget in Flutter, wrap it with a Container and use BoxDecoration for decoration:

Container(
 decoration: BoxDecoration(
 border: Border.all(
 color: Colors.black, // Adjust color as needed
 width: 2.0, // Adjust width as needed
 ),
 ),
 child: Text(
 'Your Text Here',
 ),
)

This creates a black border around your text. Adjust color and width as needed.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.