Text
构造函数的TextAlign
属性。Text("text", textAlign: TextAlign.center,)
我认为更灵活的选择是将Text()
包裹在Align()
中:
Align(
alignment: Alignment.center, // Align however you like (i.e .centerRight, centerLeft)
child: Text("My Text"),
),
Center()
方法似乎完全忽略了 Text widget 上的 TextAlign
属性。如果您尝试使用 TextAlign.left
或者 TextAlign.right
进行对齐,它将无法实现,文本仍然会处于中心位置。
TextStyle(leadingDistribution:TextLeadingDistribution.even)
以使它们在垂直方向上正确居中。 - dan-gphAlign
包装,并将 alignment
设置为 Alignment.center
。 child: Align(
alignment: Alignment.center,
child: Text(
'Text here',
textAlign: TextAlign.center,
),
),
这对我来说产生了最佳结果。
文本标题必须始终置于顶部。
错误的方式:
child: Center(
child: Text(
textAlign: TextAlign.center,
"Hello World",
),
),
child: Center(
child: Text(
"Hello World",
textAlign: TextAlign.center,
),
),
您需要在文本小部件上使用 textAlign
属性。这对我来说产生了最好的结果。
Text(
'Hi there',
textAlign: TextAlign.center,
)
Alt+Enter
,然后选择Wrap with Center
,接着添加textAlign: TextAlign.center
。将文本置于中心:
Container(
height: 45,
color: Colors.black,
child: Center(
child: Text(
'test',
style: TextStyle(color: Colors.white),
),
),
);
SizedBox中心处的文本元素工作方式更加出色,以下是示例代码
Widget build(BuildContext context) {
return RawMaterialButton(
fillColor: Colors.green,
splashColor: Colors.greenAccent,
shape: new CircleBorder(),
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
width: 100.0,
height: 100.0,
child: Center(
child: Text(
widget.buttonText,
maxLines: 1,
style: TextStyle(color: Colors.white)
),
)
)]
),
),
onPressed: widget.onPressed
);
}
享受编码的乐趣
概述:我使用Flex小部件通过水平轴上的MainAxisAlignment.center来居中文本。我使用容器填充来创建文本周围的边距空间。
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(20),
child:
Text("No Records found", style: NoRecordFoundStyle))
])