如何将中间元素置于 3 个元素的颤振行中居中

问题描述 投票:0回答:1

如何将中间元素集中在一行 3 个元素中(其中每个元素具有不同的长度),我希望侧面元素最大程度地位于左侧/右侧,而中间元素恰好位于中间?

Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                            child: Container(
                          child: Text(
                            'el',
                            style: TextStyle(
                              fontSize: 16,
                              color: Color(0xFFD50000),
                            ),
                          ),
                        )),
                        Expanded(
                            child: Container(
                          child: Text('elem',
                              style: TextStyle(
                                color: Colors.black,
                                fontSize: 16,
                              )),
                        )),
                        Expanded(
                            child: Container(
                          child: Text('longest elem',
                              style: TextStyle(
                                fontSize: 16,
                                color: Color(0xFFD50000),
                              )),
                        )),
                      ],
                    )

flutter dart alignment row
1个回答
0
投票

当您使用

Expanded
时,每个人都会变得灵活:1。现在您可以使用 Container 中的
align
,然后使用
textAlign
使其居中(只需要其中之一)。

Expanded(
  child: Container(
    alignment: Alignment.center,
    child: Text(
      'elem',
      textAlign: TextAlign.center, //may work without it
      style: TextStyle(
        color: Colors.black,
        fontSize: 16,
      ),
    ),
  ),
),
© www.soinside.com 2019 - 2024. All rights reserved.