首页 > 解决方案 > 行匹配父宽度

问题描述

我正在尝试将Row包含的“短标签”设置为Row如下所示。

放置mainAxisSize: MainAxisSize.max是不够的,因为它应该匹配父宽度,基于这个答案: 在颤振中相当于 wrap_content 和 match_parent?

尝试使用Expanded外部行,但它会导致错误,同样使用SizedBox.expand. 不能使用约束,width: double.infinity因为左边的行(// Left part在代码中)必须只占用它需要的空间,其余的必须从黄色容器中取出。

截图和代码如下:

应用截图

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Column(
          children: [
            Row(
              children: [
                // Left part
                Row(
                  children: [
                    Column(children: [
                      // Error using Expanded ...
                      //Expanded(
                      //child:
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Text('Short label:'),
                          Container(width: 20, height: 20, color: Colors.red),
                        ],
                      ),
                      //),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Text('Long text label:'),
                          Container(width: 20, height: 20, color: Colors.red),
                        ],
                      ),
                    ]),
                  ],
                ),
                Expanded(
                  child: Container(
                    color: Colors.yellow,
                    height: 100,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    ),
  );
}

编辑(旧,检查下面的最终编辑)

IntrinsicWidth正如@mohandes 建议的那样,是使行具有相同宽度的正确方法。

但是,如果我在下面添加另一行(下图中包含复选框的那一行),将行包装在两个小部件中,则它不起作用IntrinsicWidth。复选框行应与上一行一样宽,以便与左侧的复选框对齐。

在第一行中,我在右侧添加了一个橙色容器作为类似块的占位符。

截图和代码如下:

在此处输入图像描述

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Column(
          children: [
            Row(children: [
              Column(
                children: [
                  IntrinsicWidth(
                    child: Row(children: [
                      IntrinsicWidth(
                        child: Column(children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text('Short label 1:'),
                              Container(width: 20, height: 20, color: Colors.red),
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text('Short label 123456:'),
                              Container(width: 20, height: 20, color: Colors.red),
                            ],
                          ),
                        ]),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          width: 40,
                          height: 40,
                          color: Colors.orange,
                        ),
                      )
                    ]),
                  ),
                  IntrinsicWidth(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Checkbox(value: true, onChanged: null),
                        Text('Checkbox'),
                      ],
                    ),
                  ),
                ],
              ),
              Expanded(
                child: Container(
                  color: Colors.yellow,
                  height: 100,
                ),
              ),
            ]),
          ],
        ),
      ),
    ),
  );
}

编辑 2

IntrinsicWidth工作正常,第一个编辑中上面代码的问题是我用作IntrinsicWidth子小部件(行)而不是父小部件(外列)的包装器。

标签: layoutflutter-desktop

解决方案


尝试在内部宽度小部件中添加包含行(长和短)的列。内在宽度使所有子宽度成为最长的子宽度。


推荐阅读