首页 > 解决方案 > 如何在 Text.rich() 中对齐文本中心

问题描述

我正在使用一个文本跨度小部件,我喜欢在其中将所有文本居中,而不是在开始时默认对齐。

 Text.rich(
                          TextSpan(
                            text: "Go to your e-mail inbox and ",
                            style: TextStyle(
                                color: appBackground_color,
                                fontSize: 14,
                                fontWeight: FontWeight.w500),
                            children: <InlineSpan>[
                              TextSpan(
                                text: "activate your account",
                                style: TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.w700,
                                  color: appBackground_color,
                                ),
                              ),
                              TextSpan(
                                text: " to proceed.",
                                style: TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.w500,
                                    color: appBackground_color),
                              ),
                            ],
                          ),
                        ),

我怎样才能在这个小部件中居中文本?

标签: flutterflutter-layout

解决方案


使用RichText()而不是Text.rich(). Text.rich()没有textAlign参数。像这样使用:

RichText(
          textAlign: TextAlign.center,
          text: TextSpan(
            text: "Go to your e-mail inbox and ",
            children: <InlineSpan>[
              TextSpan(),
              TextSpan(),
            ],
          ),
        ),

请记住,RichText()必须有一个宽度textAlign才能工作。像这样:

 Container(
              width: double.infinity,
              child: RichText(
               //**
              ),
            ),

推荐阅读