zrhl
zrhl

Reputation: 117

Flutter - ListView.builder not scrollable

I have my ListView.builder inside Expanded widget which render widgets correctly on the screen but I cannot scroll the widgets rendered by it.

 Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPostsForUid(),
        builder: (_, snap) {
          return Expanded(
            child: ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snap.data.length,
              itemBuilder: (_, index) {
                if (snap.data[index]['post_type'] == 'real_estate') {
                  return realEstate(snap, index);
                }
                else if (snap.data[index]['post_type'] == 'video_sharing') {
                  return videoSharing(snap, index);
                }
                else {
                  return Text('');
                }
              },
            ),
          );
        },
      ),
    );
  }

Upvotes: 0

Views: 5703

Answers (3)

tafaust
tafaust

Reputation: 1518

You should set your physics to AlwaysScrollableScrollPhysics(). The docs state the following:

Scroll physics that always lets the user scroll. This overrides the default behavior which is to disable scrolling when there is no content to scroll. It does not override the handling of overscrolling. On Android, overscrolls will be clamped by default and result in an overscroll glow. On iOS, overscrolls will load a spring that will return the scroll view to its normal range when released.

Here is an image of "overscroll glow" for you to understand what that means. https://i.sstatic.net/6mMn4.png

Consider using shrinkWrap: false to expand your contents in case they are bounded.

Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPostsForUid(),
        builder: (_, snap) {
          return Expanded(
            child: ListView.builder(
              physics: AlwaysScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snap.data.length,
              itemBuilder: (_, index) {
                if (snap.data[index]['post_type'] == 'real_estate') {
                  return realEstate(snap, index);
                }
                else if (snap.data[index]['post_type'] == 'video_sharing') {
                  return videoSharing(snap, index);
                }
                else {
                  return Text('No data available.');
                }
              },
            ),
          );
        },
      ),
    );
  }

See the docs:

Upvotes: 1

Solomon Swamynathan
Solomon Swamynathan

Reputation: 43

Try using ScrollPhysics() class, physics: ScrollPhysics(), here is the link for reference for the same.

Upvotes: 1

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

Add Physics:AlwaysScrollable() in your Listview.

Upvotes: 0

Related Questions