我在屏幕截图中重新创建布局时遇到了问题,该布局遵循两条规则:

  • 两个Column的高度由左侧的Column决定,因为内容(蓝色容器)在不同情况下可能会有所不同.
  • 右边的Column应该和左边的柱子一样高,它的子项(黄色和红色容器)应该相应地与柱子的顶部和底部对齐.

enter image description here

这就是我现在拥有的

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Row(
          children: [
            Container(
              padding: const EdgeInsets.all(24),
              color: Colors.green,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    height: 500,
                    width: 200,
                    color: Colors.blue,
                  )
                ],
              ),
            ),
            Container(
              padding: const EdgeInsets.all(24),
              color: Colors.greenAccent,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    padding: const EdgeInsets.all(24),
                    height: 100,
                    width: 200,
                    color: Colors.yellow,
                  ),
                  Container(
                    padding: const EdgeInsets.all(24),
                    height: 100,
                    width: 200,
                    color: Colors.red,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

看起来是这样的:

enter image description here

现在谈谈我try 过的事情和问题:

  1. 将"右柱"更改为"mainAxisSize: MainAxisSize.max,"将在决定高度的右侧列中显示结果.

enter image description here

  1. 在黄色和红色容器之间添加Spacer将导致右侧列扩展到最大高度.

enter image description here

所以问题是:

  • 跟随左列的高度,但不考虑所有可用高度
  • 保持整个布局的高度动态(因此蓝色容器或左列没有硬编码的高度)
  • 将右列上的容器和右列的顶部和底部对齐.

我觉得我错过了一些东西,或者需要一个我不知道的特殊小部件.也许可以提前使用Flexible

任何 idea 或建议都是非常受欢迎的.

========================

基于@Ivo Beckers answer.的完整解决方案

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Center(
          child: IntrinsicHeight(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.green,
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Container(
                          height: 500,
                          width: 200,
                          color: Colors.blue,
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.greenAccent,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 200,
                        width: 200,
                        color: Colors.yellow,
                      ),
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 50,
                        width: 200,
                        color: Colors.red,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

推荐答案

我有你这样的例子

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(
        color: Colors.black12.withOpacity(0.1),
        padding: const EdgeInsets.all(24),
        child: Align(
          child: IntrinsicHeight(
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.green,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        height: 500,
                        width: 200,
                        color: Colors.blue,
                      )
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(24),
                  color: Colors.greenAccent,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 100,
                        width: 200,
                        color: Colors.yellow,
                      ),
                      const Spacer(),
                      Container(
                        padding: const EdgeInsets.all(24),
                        height: 100,
                        width: 200,
                        color: Colors.red,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

所以基本上Row是用IntrinsicHeight包裹的,而那是用Align包裹的.只是IntrinsicHeight似乎没用.此外,在第二个Column中的Container之间有一个Spacer()

Flutter相关问答推荐

如何等待抖动S定时器.定期取消?

文本未设置在集装箱摆动的中心

如何在Flutter 中播放音频

如何在默认情况下隐藏appBar并在向下滚动时显示它- Flutter

Flutter 中的多页表现

在Flutter 中每次点击按钮时都很难调用自定义函数

无法在仿真器上运行Flutter 项目-文件名太长错误

摆动更改标记可轻敲文本按钮样式

如何在点击查看措辞下方添加句子?

如何对齐 AppBar Actions 中的文本?

SliverAppbar 呈蓝色

如何在Flutter 中使用选定的键和值从 map 列表创建 map

Flutter中pubspec.yaml文件中的name参数是什么?

无状态和有状态小部件,我可以同时使用吗?

'type' Icon '不是类型' IconData '的子类型

有没有办法为文本中的下划线创建自定义样式?

为什么 orientation == Orientation.portrait 总是正确的,尽管我的设备已经处于横向

如何在 SingleChildScrollView 小部件中管理自定义小部件状态

带图像封面的 ElevatedButton

使用 hydrad_bloc 的预览包