You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何实现多行文本每行居中对齐?

实现Flutter多行文本每行居中对齐

你只需要给目标Text组件添加textAlign: TextAlign.center属性即可,无需拆分文本为多个Text组件。修改后的代码如下:

body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(top: 20, left: 15, right: 15),
          child: Padding(
            padding: const EdgeInsets.only(top: 10),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(15),
              child: Container(
                height: 500,
                color: const Color.fromARGB(255, 35, 35, 35),
                child: Padding(
                  padding: const EdgeInsets.only(top: 20),
                  child: Column(children: const [
                    Text(
                      'Student Space',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 30,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    Text(
                      'This is a very very very long text that should be centered just like the first text in this column. I don\'t want to cut it for each row.',
                      textAlign: TextAlign.center, // 新增居中对齐属性
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ]),
                ),
              ),
            ),
          ),
        ),
      ),

textAlign: TextAlign.center会让文本自动换行后每一行都保持居中对齐,完全符合你想要的Word文本居中样式。另外注意修复了原代码中字符串里单引号的语法错误(I don't改为I don\'t),避免编译报错。

内容的提问来源于stack exchange,提问作者AverageCopyPaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 05:10:50