我正在使用Flutter,我想向小部件(在本例中是文本小部件)添加边框。
我尝试了TextStyle和文本,但我不知道如何添加边框。
我正在使用Flutter,我想向小部件(在本例中是文本小部件)添加边框。
我尝试了TextStyle和文本,但我不知道如何添加边框。
当前回答
你可以把那个小部件包装到DecoratedBox,在这种情况下,DecoratedBox为那个小部件提供装饰
Widget textDecoration(String text){
return DecoratedBox(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red,
width: 10,
),
),
child: Text(text)
);
}
其他回答
用容器包装小部件
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),
),
);
如果你想添加边框的一些文本的容器,那么你可以很容易地通过应用BoxDecoration容器。
代码:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.redAccent,
width: 1,
),
),
child: Text('Some Text'),
);
文本边框样式:
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],
),
),
],
)
你可以把那个小部件包装到DecoratedBox,在这种情况下,DecoratedBox为那个小部件提供装饰
Widget textDecoration(String text){
return DecoratedBox(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red,
width: 10,
),
),
child: Text(text)
);
}
正如文档中所述,Flutter更喜欢组合而不是参数。
大多数情况下,您不是在寻找属性,而是在寻找包装器(有时还需要一些助手/“构建器”)。
对于边界,你需要DecoratedBox,它有一个定义边界的装饰属性;也包括背景图像或阴影。
或者,就像Aziza说的,你可以使用Container。这是DecoratedBox, SizedBox和其他一些有用的小部件的组合。