Is there a better way of async loading data into CupertinoPicker?

244 views Asked by At

Is there a better way of async on-demand loading data into CupertinoPicker?

What I basically do is listen to scroll event and detecting if CupertinoPicker is scrolled beyond the start and show a spinner for the duration of the loading of "data". I insert the spinner into the list of widgets built from the data and remove it when new widgets are added to the list.

I can't say what makes me wonder if there is a better way, but I feel there should be a better way of solving this issue.

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Widget> items = [Divider(color: Colors.red)];
  bool refreshInProgress = false;

  loadMoreData() {
    if(refreshInProgress) return;

    setState(() {
      refreshInProgress = true;
    });

    items.insert(0, FutureBuilder(
      future: Future.delayed(Duration(seconds: 2)).then((value) {
        var newItems = [Text("A"), Text("B"), Text("C")];

        setState((){
          items = [...newItems, ...items.where((w) => !(w is FutureBuilder) || w == null)];
          refreshInProgress = false;
        });

        return newItems;
      }),
      builder: (context, snapshot) {
        if(snapshot.connectionState == ConnectionState.waiting) return Center(child: CircularProgressIndicator());

        return null;
      },        
    ));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(child: Container(
          margin: EdgeInsets.all(20),
          child: Column(children: [
            Expanded(flex: 4, child: NotificationListener<ScrollNotification>(
              onNotification: (ScrollNotification notification) {
                if ((notification is ScrollEndNotification) && notification.metrics.pixels <= 0) {
                  loadMoreData();
                  return true;
                }
                
                return false;
              },
              child: CupertinoPicker.builder(
                itemExtent: 32,
                itemBuilder: (BuildContext context, int index) {
                  if(index >= 0 && index < items.length) return Center(child: items[index]);

                  return null;
                },
                onSelectedItemChanged: (int value) { 
                  print("onSelectedItemChanged: $value");
                },              
              ))
            ),
            Expanded(child: Container(child: Text("$items"))),
            Row(children: [
              RaisedButton(onPressed: () => setState(() => loadMoreData()), child: Text("Load more data")),
            ])
          ],
        )),
      )),
    );
  }
}

https://dartpad.dev/b27137f4bff39281980f5957f5e140ad

0

There are 0 answers