Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 611

Flutter ListView lazy loading in both directions (up, down)

$
0
0

I would like to have a ListView in flutter which provides lazy loading in both directions (up, down).

Example:

  • There are 60000 items in the backend database which can be theoretically displayed.
  • First I want to display the items 100..120
  • From these indices I want to be able to scroll up and down while lazy loading the new items

Things to consider:

  • The top and bottom edge (current index < 0 or > 60000) should be bouncing if reached whilescrolling

What I've tried:

I hope that here are some pretty smart people who can help me solving this issue ;). I'm already searching and trying around since days on this issue. Thanks!

UpdateTo make things clearer: Here is an example for a ListView with lazy loading for scrolling up and down (Most of the code copied from https://stackoverflow.com/a/49509349/10905712 by Rémi Rousselet):

import 'dart:math';import 'package:flutter/cupertino.dart';import 'package:flutter/material.dart';class MyHome extends StatefulWidget {  @override  _MyHomeState createState() => new _MyHomeState();}class _MyHomeState extends State<MyHome> {  ScrollController controller;  List<String> items = new List.generate(100, (index) => 'Hello $index');  @override  void initState() {    super.initState();    controller = new ScrollController()..addListener(_scrollListener);  }  @override  void dispose() {    controller.removeListener(_scrollListener);    super.dispose();  }  @override  Widget build(BuildContext context) {    return new Scaffold(      body: new Scrollbar(        child: new ListView.builder(          controller: controller,          itemBuilder: (context, index) {            return new Text(items[index]);          },          itemCount: items.length,        ),      ),    );  }  double oldScrollPosition = 0.0;  void _scrollListener() {    bool scrollingDown = oldScrollPosition < controller.position.pixels;    print(controller.position.extentAfter);    if (controller.position.extentAfter < 500 && scrollingDown) {      setState(() {        items.addAll(new List.generate(            42, (int index) => Random().nextInt(10000).toString()));      });    } else if (controller.position.extentBefore < 500 && !scrollingDown) {      setState(() {        items.insertAll(            0,            new List.generate(                42, (index) => Random().nextInt(10000).toString()));      });    }    oldScrollPosition = controller.position.pixels;  }}

If you execute this code and try scrolling up, you'll see a "jumping" in the list. Scrolling down + lazy load works perfectly.Scrolling up + lazy load would work if the ListView would be reversed. Anyhow, with this solution we would have the same issue with scrolling down + lazy load here.


Viewing all articles
Browse latest Browse all 611

Trending Articles