我正在使用Flutter,我想向小部件(在本例中是文本小部件)添加边框。
我尝试了TextStyle和文本,但我不知道如何添加边框。
我正在使用Flutter,我想向小部件(在本例中是文本小部件)添加边框。
我尝试了TextStyle和文本,但我不知道如何添加边框。
当前回答
使用带有Boxdercoration的容器。
BoxDecoration(
border: Border.all(
width: 3.0
),
borderRadius: BorderRadius.circular(10.0)
);
其他回答
最好的方法是使用BoxDecoration()
优势
您可以设置小部件的边框 你可以设置边框的颜色或宽度 您可以设置边框的圆角 您可以添加一个小部件的影子
缺点
BoxDecoration只与容器小部件一起使用,所以你需要将小部件包装在Container()中
例子
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"),
)
正如文档中所述,Flutter更喜欢组合而不是参数。
大多数情况下,您不是在寻找属性,而是在寻找包装器(有时还需要一些助手/“构建器”)。
对于边界,你需要DecoratedBox,它有一个定义边界的装饰属性;也包括背景图像或阴影。
或者,就像Aziza说的,你可以使用Container。这是DecoratedBox, SizedBox和其他一些有用的小部件的组合。
如果你想添加边框的一些文本的容器,那么你可以很容易地通过应用BoxDecoration容器。
代码:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.redAccent,
width: 1,
),
),
child: Text('Some Text'),
);
试试下面的代码:
Container(
margin: margin,
padding: padding,
decoration: BoxDecoration(
border: Border.all(
color: color,
width: width,
),
),
child: Text(
data,
style: TextStyle(fontSize: 30.0),
),
),
下面是一个扩展的答案。一个DecoratedBox是你需要添加边框,但我使用一个容器,以方便添加边缘和填充。
这是一般的设置。
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),
),
);
}
BoxDecoration在哪里
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(),
);
}
边框宽度
它们的边界宽度分别为1、3和10。
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(
width: 1, // <--- border width here
),
);
}
边框颜色
它们的边框颜色是
Colors.red Colors.blue Colors.green
Code
BoxDecoration myBoxDecoration() {
return BoxDecoration(
border: Border.all(
color: Colors.red, // <--- border color
width: 5.0,
),
);
}
边界边
这些有边界的一面
左(3.0),上(3.0) 底部(13.0) 左(蓝色[100],15.0),上(蓝色[300],10.0),右(蓝色[500],5.0),下(蓝色[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,
),
),
);
}
边界半径
它们的边界半径分别为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非常灵活。阅读颤振箱装饰小抄更多的想法。