Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Horizontal ListView.builder inside a Vertical ScrollView in Flutter

What I want to do,

(in below how I tried)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My try:

Scaffold(
        body: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection("posts")
              .orderBy('datePublished', descending: true)
              .snapshots(),
          builder: (context,
              AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
            //connection no problem
            return SingleChildScrollView(
              child: Column(
                children: [
                  Expanded(
                    child: ListView.builder(
                      shrinkWrap: true,
                      scrollDirection: Axis.horizontal,
                      itemCount: 5,
                      itemBuilder: (context, index) => Container(
                        padding: const EdgeInsets.all(12),
                        margin: const EdgeInsets.all(12),
                        height: 100,
                        width: 150,
                        color: Colors.pink,
                      ),
                    ),
                  ),
                  Expanded(
                    child: ListView.builder(
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context, index) => PostCard(
                        snap: snapshot.data!.docs[index].data(),
                      ),
                    ),
                  ),
                ],
              ),
            );
          },
        ));

It didn’t work. showing this error:

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderRepaintBoundary#aa89e relayoutBoundary=up1 NEEDS-PAINT
‘package:flutter/src/rendering/box.dart’:
package:flutter/…/rendering/box.dart:1
Failed assertion: line 2001 pos 12: ‘hasSize’

The relevant error-causing widget was
Scaffold

>Solution :

You can’t call Expanded in column that wrapped with SingleChildScrollView, try this:

SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: 100 + 12 + 12,
              child: ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.horizontal,
                itemCount: 5,
                itemBuilder: (context, index) => Container(
                  padding: const EdgeInsets.all(12),
                  margin: const EdgeInsets.all(12),
                  height: 100,
                  width: 150,
                  color: Colors.pink,
                ),
              ),
            ),
            ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) => PostCard(
                snap: snapshot.data!.docs[index].data(),
              ),
            ),
          ],
        ),
      )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading