Milos
Milos

Reputation: 38

Flutter two ListView.builder inside ListView

Okey first what I want to get, I want to get two ListView.builder(with physics: NeverScrollableScrollPhysics()) inside ListView which can be scrolled and here is the code how I get that

WORKING CODE!!!

      CustomScrollView(slivers: <Widget>[
      SliverToBoxAdapter(
        child: Text("Artist"),
      ),
      SliverList(
        delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
          return Padding(
            padding: const EdgeInsets.only(top: 10, left: 20),
            child: Stack(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(
                    top: maxHeight * 0.014,
                    left: maxWidth * 0.159,
                  ),
                  child: Column(
                    // mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      searchResults1[index].name == null
                          ? Container()
                          : Text(
                              searchResults1[index].name,
                              style: TextStyle(
                                fontSize: 12.0,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                    ],
                  ),
                ),
                Container(
                  // tag: 'recently-played1',
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6.0),
                    child: searchResults1[index].image == null
                        ? Container()
                        : Image(
                            height: maxHeight * 0.0635,
                            width: maxWidth * 0.1306,
                            image: NetworkImage(searchResults1[index].image),
                            fit: BoxFit.cover,
                          ),
                  ),
                ),
              ],
            ),
          );
        },
        childCount: searchResults1.length
        ),
      ),
      SliverToBoxAdapter(
        child: Text("Songs"),
      ),
      SliverList(
        key: list2,
        delegate: SliverChildBuilderDelegate((BuildContext context, int i) {
          return Padding(
            padding: const EdgeInsets.only(top: 10, left: 20),
            child: Stack(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(
                    top: maxHeight * 0.014,
                    left: maxWidth * 0.159,
                  ),
                  child: Column(
                    // mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      searchResults2[i].title == null
                          ? Container()
                          : Text(
                              searchResults2[i].title,
                              style: TextStyle(
                                fontSize: 12.0,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                      searchResults2[i].author == null
                          ? Container()
                          : Text(
                              searchResults2[i].author,
                              style: TextStyle(
                                  fontSize: 12.0,
                                  fontWeight: FontWeight.w600,
                                  color: Colors.grey),
                            )
                    ],
                  ),
                ),
                Container(
                  // tag: 'recently-played1',
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6.0),
                    child: searchResults2[i].image == null
                        ? Container()
                        : Image(
                            height: maxHeight * 0.0635,
                            width: maxWidth * 0.1306,
                            image: NetworkImage(searchResults2[i].image),
                            fit: BoxFit.cover,
                          ),
                  ),
                ),
              ],
            ),
          );
        },
        childCount: searchResults1.length
        ),
      ),
    ]);

but what is happening here that two lists are long and I have a performance issue, the scroll is not smooth and sometimes even freeze. Something is telling me it's because of shrinkWrap: true maybe I'm wrong. The important thing that I want to achieve is that I have Header then one listview.builder then again Header and then last listview.builder. Thanks for the help guys.

Upvotes: 1

Views: 2920

Answers (2)

Abhinand Valasseri
Abhinand Valasseri

Reputation: 76

Hey you are using wrong widgets for this purpose. Use a CustomScrollView with SliverList.

Don't use shrinkWrap: true in very long and dynamic lists. to make shrinkWrap to work flutter needs to determine heights of all child widgets of the list this is a heavy computational task. An example of what you can do is

CustomScrollView(
 slivers: [
  SliverToBoxAdapter(child: Text("some")),
  SliverList(),
  SliverToBoxAdapter(child: Text("some")),
 ]
)

Upvotes: 2

Candace
Candace

Reputation: 81

The problem could be that it is nested views. Instead of using the inner ListView.builder with NeverScrollableScrollPhysics, try using List.generate to populate a column widget. Then the scroll will be handled by your outer ListView. Your inner lists:

Column(
      children: List.generate(
          myList,
          (index) => myWidget

UPDATE:

Instead of loading an empty image

ClipRRect(
borderRadius: BorderRadius.circular(6.0),
child: searchResults1[index].image == null
? Container()
: Image(
height: maxHeight * 0.0635,
width: maxWidth * 0.1306,
image: NetworkImage(searchResults1[index].image),
fit: BoxFit.cover,

Upvotes: 0

Related Questions