tazboy
tazboy

Reputation: 1754

Flutter - ListView not showing

For some reason the ListView isn't showing up. I think that the height of one of widgets is getting a non-height from its parent... maybe? It seems that there are widgets that don't auto-size with its contents. I don't want to force a size for any of the containers because they could contain different amounts of information. https://codepen.io/dhust/pen/vYGZLPZ

Update: Error: Cannot hit test a render box with no size. The hitTest() method was called on this RenderBox:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Container(
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    child: Column(
                      children: [
                        Expanded(
                          child: ListView.builder(
                            itemCount: 4,
                            itemBuilder: (BuildContext context, int index) {
                              return Text("Hi");
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 936

Answers (1)

Warjeh
Warjeh

Reputation: 1220

For ListView add shrinkWrap: true, to avoid "Vertical viewport was given unbounded height." error and Use Expanded for its parent Column to avoid "constraints.hasBoundedWidth is not true".

    Column(
      children: [
        Row(
          children: [
            Expanded( // Use Expanded here
              child: Container(
                child: Column(
                  children: [
                    ListView.builder(
                      shrinkWrap: true, // and use shrinkWrap
                      itemCount: 4,
                      itemBuilder: (BuildContext context, int index) {
                        return Text("Hi");
                      },
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ],
    ),

result:

enter image description here

Upvotes: 1

Related Questions