使用Column内部的ListView赋予“垂直”视口无限的高度

时间:2019-10-27 04:20:05

标签: flutter

我想在Container小部件内添加Column的列表。

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    fixedContainer(context),
),
  Widget fixedContainer(BuildContext context) {
    return ListView(
      children: <Widget>[
        FutureBuilder<List<AddCash>>(
          future: future,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Column(
                  children:
                      snapshot.data.map((todo) => nameColumn(todo)).toList());
            } else {
              return SizedBox();
            }
          },
        ),
      ],
    );
  }

  Widget nameColumn(AddCash addCash) {
    return Container(
      alignment: Alignment.center,
      width: 120.0,
      height: 60.0,
      color: Colors.white,
      margin: EdgeInsets.all(4.0),
      child: Text(
        'name: ${addCash.name}',
      ),
    );
  }

显示“垂直”视口具有无限的高度时出错。我需要设定高度吗?我没有设置高度,因为我需要动态添加容器。

整个代码

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../data/repository_service_addcash.dart';
import '../models/addcash.dart';

class CalendarPage extends StatefulWidget {
  @override
  _CalendarPageState createState() => _CalendarPageState();
}

class _CalendarPageState extends State<CalendarPage> {
  Future<List<AddCash>> future;
  int id;
  @override
  initState() {
    super.initState();

    future = RepositoryServiceAddCash.getAllAddCash();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                //THE FIRST COLUMN NEEDS TO BY DYNAMICALLY PRODUCDED
                // nameColumnContainer(context),
                Container(
                  alignment: Alignment.center,
                  width: 120.0,
                  height: 60.0,
                  color: Colors.white,
                  margin: EdgeInsets.all(4.0),
                  child: Text(
                    "ONE",
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  width: 120.0,
                  height: 60.0,
                  color: Colors.white,
                  margin: EdgeInsets.all(4.0),
                  child: Text(
                    "TWO",
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  width: 120.0,
                  height: 60.0,
                  color: Colors.white,
                  margin: EdgeInsets.all(4.0),
                  child: Text(
                    "THREE",
                  ),
                ),
              ],
            ),
            Flexible(
              child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: Row(
                        // children: getDaysInWeek(),
                        children: <Widget>[
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "A",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "B",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "C",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "D",
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      child: Row(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "H",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "F",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "3000",
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 120.0,
                            height: 60.0,
                            color: Colors.white,
                            margin: EdgeInsets.all(4.0),
                            child: Text(
                              "4000",
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      width: 120.0,
                      height: 60.0,
                      color: Colors.white,
                      margin: EdgeInsets.all(4.0),
                      child: Text(
                        "two",
                      ),
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget nameColumnContainer(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: <Widget>[
        FutureBuilder<List<AddCash>>(
          future: future,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Column(
                  children:
                      snapshot.data.map((todo) => nameColumn(todo)).toList());
            } else {
              return SizedBox();
            }
          },
        ),
      ],
    );
  }

  Widget nameColumn(AddCash addCash) {
    return Container(
      alignment: Alignment.center,
      width: 120.0,
      height: 60.0,
      color: Colors.white,
      margin: EdgeInsets.all(4.0),
      child: Text(
        'name: ${addCash.name}',
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

您可以将shrinkWrap: true设置为ListView

shrinkWrap property

  

如果滚动视图没有收缩包装,则滚动视图将   在scrollDirection中扩展到最大允许大小。如果   滚动视图在scrollDirection中具有无限制的约束,然后   收缩包装必须为真。