【问题标题】:How to fill vertical space in nested Column in ListView如何在ListView的嵌套列中填充垂直空间
【发布时间】:2018-10-17 08:06:51
【问题描述】:

我希望绿色框填充垂直空间,以便MainAxisAlignment.spaceBetween 可以工作。

import 'package:flutter/material.dart';

void main() => runApp(_app);

const title = "Layout test";

var _app = new MaterialApp(
  title: title,
  home: new Scaffold(
    appBar: new AppBar(
      title: const Text(title),
    ),
    body: new _LayoutTest(),
  ),
);

class _LayoutTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return new ListView(
      children: [
        new Container(
          color: Colors.orangeAccent,
          margin: const EdgeInsets.only(bottom: 20.0),
          child: new Row(
            children: [
              new Image.network(
                // this can be any image
                "http://via.placeholder.com/185x278",
                width: size.width / 5,
              ),
              new Expanded(
                child: new Container(
                  margin: const EdgeInsets.all(8.0),
                  padding: const EdgeInsets.all(8.0),
                  color: Colors.lightGreenAccent,
                  // this should fill vertical space
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: const [
                      const Text("Top"),
                      const Text("Bottom"),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    你可以给内部容器和外部容器一样的高度:

    class _LayoutTest extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final size = MediaQuery.of(context).size;
        final newRandom = new Random().nextInt(50);
    
        return new ListView(
          children: [
            new Container(
              color: Colors.orangeAccent,
              margin: const EdgeInsets.only(bottom: 20.0),
              child: new Row(
                children: [
                  new Container(
                    color: Colors.pinkAccent,
                    width: size.width / 5,
                    height: 200.0 + newRandom,
                  ),
                  new Expanded(
                    child: new Container(
                      margin: const EdgeInsets.all(8.0),
                      padding: const EdgeInsets.all(8.0),
                      color: Colors.lightGreenAccent,
                      child: new Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: const [
                          const Text("Top"),
                          const Text("Bottom"),
                        ],
                      ),
                      height: 200.0 + newRandom,
                    ),
                  ),
                ],
              ),
            ),
          ],
        );
      }
    }
    

    【讨论】:

    • 你是对的。但我有点不清楚。 “随机”只是未知高度的占位符(例如图像)。所以这并不能解决我的问题:(
    • 你可以给那个图像一个固定的高度。
    • 在构建时未知
    • 我的问题现在已更新为网络图像。
    • 即使是网络图片你也可以给它一个固定高度。如果列表中的所有小部件都具有统一的高度,它也会很好看。
    猜你喜欢
    • 2010-11-23
    • 2015-05-16
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多