如何在Flutter中使文本居中对齐到SizedBox的中心

5

我在Flutter中使用SizedBox来放置文本,但是文本粘在了盒子的顶部,如何将文本放置在盒子的中间。

child: Container(
                width: 240.0,
                height: 42.0,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(24.0),
                  color: const Color(0xff2c2c2c),
                ),
                child: SizedBox(
                  child: Text(
                    'SIGN UP',
                    style: TextStyle(
                      fontFamily: 'Arial',
                      fontSize: 18,
                      color: Colors.white,
                      height: 1,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
1个回答

9

Text包装在一个Center小部件中。

   child: Container(
          width: 240.0,
          height: 42.0,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(24.0),
            color: const Color(0xff2c2c2c),
          ),
          child: Center(
            child: Text(
              'SIGN UP',
              style: TextStyle(
                fontFamily: 'Arial',
                fontSize: 18,
                color: Colors.white,
                height: 1,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),

据我所知,你可以删除SizedBox小部件,这是结果。 enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接