简体   繁体   English

如何垂直对齐 Flutter 文本小部件?

[英]How can I align the Flutter Text Widget Vetically?

I am trying to align these text widgets vertically so that they are perfectly centered between the two lines.我正在尝试垂直对齐这些文本小部件,以使它们在两行之间完美居中。 However, flutter sees the size of the text widget to be as big as the red rectangle behind the text (Background color of text only for illustration purposes) because of the decender line.但是,flutter 认为文本小部件的大小与文本后面的红色矩形一样大(文本的背景颜色仅用于说明目的),因为有下降线。 However, this results in the text looking a bit offcentered.但是,这会导致文本看起来有点偏离中心。 Is there a way to align the text using the baseline and not the decender?有没有办法使用基线而不是 decender 来对齐文本?

在此处输入图像描述

Play with the text height (line-height)玩文字高度(行高)

Text('Hey There', 
  style: TextStyle(height: 5, fontSize: 10),
)

You can try Align widget just wrap your text with Align and provide your alignment您可以尝试 Align 小部件,只需使用 Align 包装您的文本并提供您的 alignment

Wrap it in a center widget将其包裹在中心小部件中

Center(
    child: Text(
      "PAYMENT",
      style: TextStyle(
        backgroundColor: Colors.blue,
        fontSize: 20,
      ),
      textAlign: TextAlign.center,
    ),
)

Dartpad:飞镖盘:

https://dartpad.dev/flutter?null_safety=true&id=3b0bf085892c1867012f5dd00713ccf7 https://dartpad.dev/flutter?null_safety=true&id=3b0bf085892c1867012f5dd00713ccf7

飞镖板结果

I think what your looking for is to use the " CrossAxisAlignment " attribute in the Column widget.我认为您要寻找的是在Column小部件中使用“ CrossAxisAlignment ”属性。

Column(
  crossAxisAlignment: CrossAxisAlignment.center,
  children: [
    Text('Delivery'),
    Text('Payment'),
    Text('Total'),
  ],
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM