Sunday, December 2, 2018

Membuat infinite scroll list di flutter

Infinite Scroll adalah behaviour dimana halaman akan memuat data secara terus menerus sejalan dengan user men-scroll halaman sampai akhir konten. Fitur ini adalah fitur pengganti pagination. Layakny seperti kita scroll postingan facebook atu twitter terus menerus tanpa habisnya. Jika kita membuat aplikasi mobile dengan flutter, mengimplementasikan Infinite scrolling cukup mudah yaitu bisa menggunakan Scroll Controller dan Notification Listener.
Berikut adalah contoh kode keduanya, silahkan di uthek-uthek sendiri. saya tidak akan menjelaskan kodenya.

Scroll Controller

Untuk tahu lebih lanjut tentang Scroll Controller kunjungi Scroll Controller

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => new _MyHomeState();
}

class _MyHomeState extends State {
  ScrollController controller;

 @override
  void initState() {
    controller = new ScrollController()..addListener(_scrollListener);
    super.initState();
  }

  @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) {
              // TODO: Show LIST
            return new Container();
          },
          itemCount: items.length,
        ),
      ),
    );
  }

  void _scrollListener() {
    print(controller.position.extentAfter);
    if (controller.position.extentAfter == 500) {
      setState(() {
        // TODO: Implement infScroll
        // TODO: Fetch New Data
      });
    }
  }
}

Notification Listener

Untuk tahu lebih lanjut tentang Notification Listener kunjungi Scroll Controller

@override
  Widget build(BuildContext context) {

    return new NotificationListener(
            onNotification: (ScrollNotification value) {
              if (value.metrics.extentAfter == 0.0) {
               // TODO: Load more data
              }
            },
            child: new ListView.builder(
                padding: new EdgeInsets.all(4.0),
                itemBuilder: (BuildContext context, int index) {

                  return new Container()
                })
        );
  }


Source :