[英]How can I add a border to a widget in Flutter?
I'm using Flutter and I'd like to add a border to a widget (in this case, a Text
widget).我正在使用 Flutter 并且我想向小部件(在本例中为
Text
小部件)添加边框。
I tried TextStyle
and Text
, but I didn't see how to add a border.我尝试了
TextStyle
和Text
,但没有看到如何添加边框。
You can add the Text
as a child
to a Container
that has a BoxDecoration
with border
property:您可以将
Text
作为child
项添加到具有带border
属性的BoxDecoration
的Container
中:
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'),
)
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.您需要一个
DecoratedBox
来添加边框,但我使用Container
为了方便添加边距和填充。
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
在哪里
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(),
);
}
These have a border width of 1
, 3
, and 10
respectively.它们的边框宽度分别为
1
、 3
和10
。
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(
width: 1, // <--- border width 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,
),
);
}
These have a border side of这些有边界的一面
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,
),
),
);
}
These have border radii of 5
, 10
, and 30
respectively.它们的边界半径分别为
5
、 10
和30
。
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(
width: 3.0
),
borderRadius: BorderRadius.all(
Radius.circular(5.0) // <--- border radius here
),
);
}
DecoratedBox
/ BoxDecoration
are very flexible. DecoratedBox
/ BoxDecoration
非常灵活。 Read Flutter — BoxDecoration Cheat Sheet for many more ideas.阅读Flutter — BoxDecoration Cheat Sheet了解更多想法。
The best way is using BoxDecoration()最好的方法是使用 BoxDecoration()
Advantage优势
Disadvantage缺点
BoxDecoration
only use with Container
widget, so you want to wrap your widget in Container()
BoxDecoration
仅与Container
小部件BoxDecoration
使用,因此您希望将小部件包装在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"),
)
As stated in the documentation, Flutter prefers composition over parameters.正如文档中所述,Flutter 更喜欢组合而不是参数。
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;对于边框,您需要
DecoratedBox
,它具有定义边框的decoration
属性; but also background images or shadows.还有背景图像或阴影。
Alternatively, like Aziza said , you can use Container
.或者,就像Aziza 所说的,您可以使用
Container
。 Which is the combination of DecoratedBox
, SizedBox
and a few other useful widgets.这是
DecoratedBox
、 SizedBox
和一些其他有用的小部件的组合。
Here, as theText 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.在这里,由于Text小部件没有允许我们定义
border
的属性,我们应该用一个允许我们定义边框的小部件包装它。 There are several solutions.有几种解决方案。 But the best solution is the use of BoxDecoration in the Container widget.
但最好的解决方案是在Container小部件中使用BoxDecoration 。
Why choose to use BoxDecoration?为什么选择使用 BoxDecoration?
Because BoxDecoration offers more customization like the possibility to define:因为 BoxDecoration 提供了更多的自定义,比如定义的可能性:
First, the border
and also define:一、
border
又定义:
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:输出:
You can use Container to contain your widget:您可以使用 Container 来包含您的小部件:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color(0xff000000),
width: 1,
)),
child: Text()
),
Use a container with Boxdercoration.使用带有 Boxdercoration 的容器。
BoxDecoration(
border: Border.all(
width: 3.0
),
borderRadius: BorderRadius.circular(10.0)
);
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,
)
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),
),
);
I'm using Flutter and I'd like to add a border to a widget (in this case, a Text widget).我正在使用Flutter,我想向小部件(在本例中为Text小部件)添加边框。
I tried TextStyle and Text, but I didn't see how to add a border.我尝试了TextStyle和Text,但是没有看到如何添加边框。
In case someone would like a outlined/bordered text or apply multiple borders.如果有人想要轮廓/带边框的文本或应用多个边框。
You could try this:你可以试试这个:
https://pub.dev/packages/outlined_text https://pub.dev/packages/outlined_text
you can wrap that widget to DecoratedBox that provide decoration to that widget in that case在这种情况下,您可以将该小部件包装到 DecoratedBox 为该小部件提供装饰
Widget textDecoration(String text){ return DecoratedBox( decoration: BoxDecoration( border: Border.all( color: Colors.red, width: 10, ), ), child: Text(text)); Widget textDecoration(String text){ return DecoratedBox(decorated: BoxDecoration( border: Border.all( color: Colors.red, width: 10, ), ), child: Text(text)); }
}
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,
);
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),
),
),
Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
border: Border.all(),
),
child: const Text(
"FlutterService",
style: TextStyle(fontSize: 34.0),
),
)
Border.all() creates a black border on all sides of a container. Border.all() 在容器的所有边上创建一个黑色边框。 It is a very special type of constructor.
它是一种非常特殊的构造函数。
References: How To Add Border To Container in Flutter参考: 如何在Flutter中给容器添加边框
I have tried to summarize all the important posibilities when using border
in BoxDecoration
.我试图总结在
BoxDecoration
中使用border
时的所有重要可能性。
Container(
decoration: BoxDecoration(border: Border.all()),
child: const Text("Text"),
),
Container(
decoration: BoxDecoration(
border: Border.all(
width: 4,
color: Colors.green,
strokeAlign: BorderSide.strokeAlignCenter)),
child: const Text("Text"),
),
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"),
),
],
),
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"),
),
],
),
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"),
),
],
),
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],
),
),
],
)
you can add a border to a widget by wrapping it in a Container widget and specifying the border properties.您可以通过将其包装在 Container 小部件中并指定边框属性来向小部件添加边框。
Here's an example of how you can add a border to a Text widget:以下是如何向文本小部件添加边框的示例:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 2,
),
),
child: Text("This text has a border"),
) )
use the Text widget inside a container and use decoration to border the text decoration: BoxDecoration( border: Border.all( color: Color(0xff000000), width: 1, )),在容器内使用 Text 小部件并使用 decoration 为文本装饰添加边框:BoxDecoration( border: Border.all( color: Color(0xff000000), width: 1, )),
If you want to add border to some text of container then you can easily to do it by applying BoxDecoration to Container.如果您想为容器的某些文本添加边框,那么您可以通过将 BoxDecoration 应用到 Container 来轻松实现。
code :代码:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.redAccent,
width: 1,
),
),
child: Text('Some Text'),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.