listview.builder底部溢出和使用灵活和扩展小部件的错误

时间:2020-01-21 21:33:00

标签: flutter dart flutter-layout rxdart

我正在尝试构建ListView,但是我无法做到列表没有溢出就触底,我发现工作的方式是在容器上设置高度,但是当我使用个人设备时我的清单下面有一个空白。

我已经在列表之前尝试过灵活和扩展的窗口小部件,但都没有用,我总是遇到相同的错误:

RenderFlex children have non-zero flex but incoming height constraints are unbounded

Vertical viewport was given unbounded height error

您对此有什么解决方案?

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.blueAccent,
        title: Padding(
          padding: EdgeInsets.only(left: 5.0),
          child: Text(
            user.name,
            style: TextStyle(fontSize: 30.0),
          ),
        ),
        actions: <Widget>[
          IconButton(
            alignment: Alignment.center,
            icon: Icon(Icons.forward),
            onPressed: () {},
          ),
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Container(
            color: Colors.blueAccent,
            child: Padding(
              padding: EdgeInsets.only(top: 30.0, left: 20.0, right: 20.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text("Conta",
                      style: TextStyle(fontSize: 17.0, color: Colors.white)),
                  Divider(color: Colors.blueAccent),
                  Text("${user.bankAccount} / ${user.agency}",
                      style: TextStyle(fontSize: 30.0, color: Colors.white)),
                  Padding(
                    padding: EdgeInsets.only(top: 30.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("Saldo",
                            style:
                                TextStyle(fontSize: 17.0, color: Colors.white)),
                        Divider(color: Colors.blueAccent),
                        Text("${user.balance}",
                            style:
                                TextStyle(fontSize: 30.0, color: Colors.white)),
                        Divider(color: Colors.blueAccent),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          Divider(color: Colors.white),
          Padding(
            padding: EdgeInsets.only(left: 20.0, right: 20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text("Recentes",
                    style: TextStyle(fontSize: 20.0, color: Colors.black)),
                FutureBuilder(
                    future: getuserStatement(user.userId),
                    builder: getstatements,
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Future getuserStatement(int id) {
    return api.getUserExtract(id);
  }

  Widget getstatements(BuildContext context, AsyncSnapshot snapshot) {
    return !snapshot.hasData
        ? Center(child: CircularProgressIndicator())
        : Container(
          height: 340,
          child: ListView.builder(
            itemCount: snapshot.data.length,
            itemBuilder: (context, index) {
              print(snapshot.data[index].title);
              return Card(
                elevation: 5,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(top: 10.0, left: 10.0),
                      child: Row(
                        children: <Widget>[
                          Text(snapshot.data[index].title),
                          Expanded(
                            child: Align(
                              alignment: Alignment(0.90, 0.00),
                              child: Text(snapshot.data[index].date),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding:
                          EdgeInsets.only(top: 20.0, bottom: 10.0, left: 10.0),
                      child: Row(
                        children: <Widget>[
                          Text(snapshot.data[index].desc),
                          Expanded(
                            child: Align(
                              alignment: Alignment(0.90, 0.00),
                              child:
                                  Text(snapshot.data[index].value.toString()),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              );
            },
          ),
        );
  }
}

1 个答案:

答案 0 :(得分:0)

解决方案:

花了很多时间后,我找到了解决方案:

我看到在代码的这一部分,我在填充内放了另一列:

Padding(
        padding: EdgeInsets.only(left: 20.0, right: 20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text("Recentes",
                style: TextStyle(fontSize: 20.0, color: Colors.black)),
            Expanded(
              child: FutureBuilder(
                future: getuserStatement(user.userId),
                builder: getstatements,
              ),
            ),
          ],
        ),
      ),

所以我要做的是,只删除了Column并保留了Text小部件,并将展开的小部件放在了填充下方。

Padding(
        padding: EdgeInsets.only(left: 20.0, right: 20.0),
        child: Text("Recentes",
            style: TextStyle(fontSize: 20.0, color: Colors.black)),
      ),
      Expanded(
              child: FutureBuilder(
                future: getuserStatement(user.userId),
                builder: getstatements,
              ),
            ),

使用设备的最大空间创建了我的列表,因此我无需使用Container或sizedBox设置高度。

在我看来,发生这种情况是因为我将一个列表放在了已经在列内的Column中,所以该列表不知道她有多少空间