首页 > 解决方案 > 为什么文本元素居中以及如何在 Dart 中将其与右侧对齐

问题描述

我有一个应该向左对齐的文本元素,但由于某种原因它看起来居中,我不知道为什么。

应该更改/添加哪些代码以使result字符串向右对齐?

return Positioned(
    bottom: 0,
    right: 0,
    left: 0,
    child: Align(
        alignment: Alignment.bottomCenter,
        child: Container(
            margin: EdgeInsets.fromLTRB(10, 0, 10, 30),
            padding: EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                //First row here. The one below is the second row:
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 5.0),
                      child: Icon(Icons.comment),
                    ),
                    Expanded(
                      child: Column(children: [
                        Padding(
                            padding: EdgeInsets.only(
                                top: 10.0, left: 5.0, right: 0),
                            child: Text(
                              result, //This text is centered. Why?
                              maxLines: 7,
                              overflow: TextOverflow.ellipsis,
                            )),
                      ]),
                    )
                  ],
                ),                    
              ],
            )
        )
    )
);

标签: androidflutterdartalignment

解决方案


为此,您可以使用方向。

例如:

MaterialApp(
  builder: (context, child) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: child,
    );
  },
);

推荐阅读