Search code examples
flutterdartflutter-layoutrxdart

listview.builder bottom overflowing and errors with Flexible and Expanded widgets


I'm trying to build a ListView but I can't do that my list hit the bottom without overflowing it, the way that I found to work was setting the height on a container, but, when I use my personal device I got a big white space below my list.

I already tried the flexible and expanded widgets before the list, but neither worked, I always get the same errors:

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

Vertical viewport was given unbounded height error

Do you have any solutions for this?

@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()),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              );
            },
          ),
        );
  }
}

Solution

  • Solution:

    after spending so much time on this, i found the solution:

    i saw that on this part of my code i put another column inside a padding:

    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,
                  ),
                ),
              ],
            ),
          ),
    

    so what i did was, just removed the Column and left the Text widget and put the expanded widget below the padding.

    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,
                  ),
                ),
    

    that created my list with the maximum space of the device, with this i don't need to set a height with Container or a sizedBox.

    In my vision, i think that happens cause i put a list inside a Column that already is inside a column, so the list doesn't known how much space she has