在哪种情况下,textAlign属性可在Flutter中工作?

时间:2018-08-01 16:28:25

标签: flutter text-alignment

在下面的代码中,textAlign属性不起作用。如果您删除了位于以上几个级别的DefaultTextStyle包装器,则textAlign开始起作用。

为什么以及如何确保其始终正常工作?

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new DefaultTextStyle(style: new TextStyle(fontSize: 10.0), child: new Column(children: <Widget>[
        new Text("Should be left", textAlign: TextAlign.left,),
        new Text("Should be right", textAlign: TextAlign.right,)
      ],))
    );
  }
}

雷米建议的两种方法显然都不是“在野外”工作的。这是我同时在行和列中嵌套的示例。第一种方法不能对齐,第二种方法会使应用程序崩溃:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new Directionality(textDirection: TextDirection.ltr, child: new DefaultTextStyle(
            style: new TextStyle(fontSize: 10.0, color: Colors.white),
            child: new Column(children: <Widget>[
              new Row(children: <Widget>[
                new Container(color: Colors.grey, child: new Column(children: <Widget>[
                  new Align(alignment: Alignment.centerLeft, child: new Text("left")),
                  new Align(alignment: Alignment.centerRight, child: new Text("right")),
                ],)),
                new Container(color: Colors.grey, child: new Column(children: <Widget>[
                  new Align(alignment: Alignment.centerLeft, child: new Text("left")),
                  new Align(alignment: Alignment.centerRight, child: new Text("right")),
                ],)),
              ],),
              /*new Row(children: <Widget>[
                new Container(color: Colors.grey, child: new Column(children: <Widget>[
                  new SizedBox(width: double.infinity, child: new Text("left", textAlign: TextAlign.left,)),
                  new SizedBox(width: double.infinity, child: new Text("right", textAlign: TextAlign.right)),
                ],)),
                new Container(color: Colors.grey, child: new Column(children: <Widget>[
                  new SizedBox(width: double.infinity, child: new Text("left", textAlign: TextAlign.left)),
                  new SizedBox(width: double.infinity, child: new Text("right", textAlign: TextAlign.right)),
                ],)),
              ],)*/]
    )));
  }
}

我从代码中得到的是

enter image description here

即文本居中,而忽略Align元素的对齐方式。

9 个答案:

答案 0 :(得分:32)

DefaultTextStyle与该问题无关。删除它仅使用默认样式,该样式比您使用的样式大得多,因此隐藏了问题。


textAlignText所占用的空间大于实际内容时将其对齐。

问题是,在Column内,您的Text占用了最小的空间。然后,Column使用crossAxisAlignment对齐其子元素,默认为center

捕获此类行为的一种简单方法是将文本包装成这样:

Container(
   color: Colors.red,
   child: Text(...)
)

使用您提供的代码,呈现以下内容:

enter image description here

问题突然变得很明显:Text并没有占据整个Column的宽度。


您现在有一些解决方案。

您可以将Text包裹到Align中以模仿textAlign的行为

Column(
  children: <Widget>[
    Align(
      alignment: Alignment.centerLeft,
      child: Container(
        color: Colors.red,
        child: Text(
          "Should be left",
        ),
      ),
    ),
  ],
)

将呈现以下内容:

enter image description here

或者您可以强制Text填充Column的宽度。

通过在crossAxisAlignment: CrossAxisAlignment.stretch上指定Column,或将SizedBox与无限width一起使用。

Column(
  children: <Widget>[
    SizedBox(
      width: double.infinity,
      child: Container(
        color: Colors.red,
        child: Text(
          "Should be left",
          textAlign: TextAlign.left,
        ),
      ),
    ),
  ],
),

呈现以下内容:

enter image description here

答案 1 :(得分:8)

在您的列中指定CrossAxisAlignment.start

答案 2 :(得分:3)

在容器中设置alignment: Alignment.centerRight

Container(
    alignment: Alignment.centerRight,
    child:Text(
       "Hello",
    ),
)

答案 3 :(得分:1)

在Colum小部件中,文本对齐将自动居中,因此请使用crossAxisAlignment: CrossAxisAlignment.start对齐开始。

Column( 
    crossAxisAlignment: CrossAxisAlignment.start, 
    children: <Widget>[ 
    Text(""),
    Text(""),
    ]);

答案 4 :(得分:1)

为了获得最大的灵活性,我通常更喜欢像这样使用SizedBox:

Row(
                                children: <Widget>[
                                  SizedBox(
                                      width: 235,
                                      child: Text('Hey, ')),
                                  SizedBox(
                                      width: 110,
                                      child: Text('how are'),
                                  SizedBox(
                                      width: 10,
                                      child: Text('you?'))
                                ],
                              )

过去使用对齐时,我遇到过文本对齐问题,而sizebox总是可以工作。

答案 5 :(得分:0)

textAlign属性仅在Text的内容还有更多空间可用时起作用。以下是两个示例,它们显示了何时textAlign具有影响力以及何时不具有影响力。


没有影响

例如,在此示例中,它不会有任何影响,因为Text的内容没有多余的空间。

Text(
  "Hello",
  textAlign: TextAlign.end, // no impact
),

enter image description here


有影响力

如果将其包装在Container中,并提供额外的width,使其具有更多的空间。

Container(
  width: 200,
  color: Colors.orange,
  child: Text(
    "Hello",
    textAlign: TextAlign.end, // has impact
  ),
)

enter image description here

答案 6 :(得分:0)

您可以使用容器,它将帮助您设置对齐方式。

Widget _buildListWidget({Map reminder}) {
return Container(
  color: Colors.amber,
  alignment: Alignment.centerLeft,
  padding: EdgeInsets.all(20),
  height: 80,
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      Container(
        alignment: Alignment.centerLeft,
        child: Text(
          reminder['title'],
          textAlign: TextAlign.left,
          style: TextStyle(
            fontSize: 16,
            color: Colors.black,
            backgroundColor: Colors.blue,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      Container(
        alignment: Alignment.centerRight,
        child: Text(
          reminder['Date'],
          textAlign: TextAlign.right,
          style: TextStyle(
            fontSize: 12,
            color: Colors.grey,
            backgroundColor: Colors.blue,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
    ],
  ),
);
}

答案 7 :(得分:0)

您可以在脚手架或容器中的任何位置对齐文本,但中心除外:-

它适用于我的应用程序中的任何地方:-

new Text(

                "Nextperience",

//i have setted in center.

                textAlign: TextAlign.center,

//when i want it left.

 //textAlign: TextAlign.left,

//when i want it right.

 //textAlign: TextAlign.right,

                style: TextStyle(

                    fontSize: 16,

                    color: Colors.blue[900],

                    fontWeight: FontWeight.w500),

              ), 

答案 8 :(得分:-1)

GestureDetector(
          onTap: () {},
          child: Container(
            padding: EdgeInsets.all(5),
            color: buttonContainerColor,
            margin: EdgeInsets.only(top: 10.0),
            width: double.infinity,
            height: bottomContainerHeight,
            alignment: Alignment.center,
            child: Text(
              "CALCULATE",
              style: TextStyle(fontSize: 25.0, color: Colors.white),
            ),
          ),
        )