微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有一种更好的异步方式将数据加载到CupertinoPicker中?

如何解决有没有一种更好的异步方式将数据加载到CupertinoPicker中?

是否有更好的异步方式将数据按需加载到Cupertinopicker?

我基本上要做的是监听滚动事件,并检测Cupertinopicker是否滚动到起点之外,并在“数据”加载期间显示一个微调框。我将微调框插入到根据数据构建的小部件列表中,并在将新的小部件添加到列表中时将其删除

我不能说是什么让我想知道是否有更好的方法,但是我认为应该有解决这个问题的更好的方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?