【问题标题】:Refresh flutter FUTUREBUILDER with a swipe up向上滑动刷新 Flutter FUTUREBUILDER
【发布时间】:2021-05-04 18:57:46
【问题描述】:

我有一个应用程序,它从 firebase 获取一些数据,然后调用一个类来显示基于 firebase 数据的小部件。我尝试添加向上滑动刷新,但我不知道将它放在哪里以及在刷新时调用什么。我正在尝试使用 RefreshIndicator。

在这里,我将把调用数据库(firebase)的代码放入其中,然后为数据库中的每个事件创建一个小部件。

如果您需要更多信息,请随时发表评论。非常感谢您的帮助!

FutureBuilder(
      future: databaseReference.once(),
      builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
        List lists = [];
        if (snapshot.hasData) {
        lists.clear();
        Map<dynamic, dynamic> values = snapshot.data.value;
        values.forEach((key, values) {
            lists.add(values);
        });
        return new ListView.builder(
          primary: false,
          padding: EdgeInsets.only(left:12.0,right:12,bottom: 15,),
          shrinkWrap: true,
          itemCount: lists.length,
          itemBuilder: (BuildContext context, int index) {
          if(lists[index]["Status"]== "Active"){;
          return Container(
          child:EvendWidget(lists[index]["EventImage"], 
                Text(lists[index]["EventName"]).data,
                Text(lists[index]["EventLocation"]+ ", "+lists[index] 
                ["EventCounty"] ).data,
                Text(lists[index]["Date"]+ ", "+lists[index]["Time"]+ 
                " - "+lists[index]["Time"]).data,
                Text(lists[index]["Duration"]+ " H").data,
                Text(lists[index]["Genre"]).data,
                Text(lists[index]["Price"]).data,false));}else{return 
                SizedBox.shrink(); };
            });
        }
        return Container(
          margin: const EdgeInsets.only(top: 300),
          child:CircularProgressIndicator());
    }),

如果您需要更多信息,请随时发表评论。非常感谢您的帮助!

【问题讨论】:

    标签: flutter


    【解决方案1】:

    在滑动操作中致电setState。它会触发小部件中的build 方法并再次调用你的未来。

    onRefresh: () {
      setState(() {});
    }
    

    【讨论】:

    • 我叫什么?
    • 你只需调用它。 setState 没有动作也可以。在您调用它后,小部件重建并再次调用build 方法(您使用FutureBuilder)。
    • 我如何检查滑动操作?
    • 你能在RefreshIndicator中添加传递给onRefresh参数的函数代码吗?
    【解决方案2】:

    您可以使用 RefreshIndicator 进行滑动刷新。 https://api.flutter.dev/flutter/material/RefreshIndicator-class.html

    使用 ListView.Builder 作为 RefreshIndicator 的子节点,并在 onRfresh 中使用你的未来方法

    像这样:

    //define in widget
    var refreshkey = GlobalKey<RefreshIndicatorState>();
    
    FutureBuilder(
      future: databaseReference.once(),
      builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
        List lists = [];
        if (snapshot.hasData) {
        lists.clear();
        Map<dynamic, dynamic> values = snapshot.data.value;
        values.forEach((key, values) {
            lists.add(values);
        });
        return RefreshIndicator(
            key: refreshkey,
            onRefresh: databaseReference.once(),
            child: ListView.builder(
              primary: false,
              padding: EdgeInsets.only(left:12.0,right:12,bottom: 15,),
              shrinkWrap: true,
              itemCount: lists.length,
              itemBuilder: (BuildContext context, int index) {
              if(lists[index]["Status"]== "Active"){;
              return Container(
              child:EvendWidget(lists[index]["EventImage"], 
                    Text(lists[index]["EventName"]).data,
                    Text(lists[index]["EventLocation"]+ ", "+lists[index] 
                    ["EventCounty"] ).data,
                    Text(lists[index]["Date"]+ ", "+lists[index]["Time"]+ 
                    " - "+lists[index]["Time"]).data,
                    Text(lists[index]["Duration"]+ " H").data,
                    Text(lists[index]["Genre"]).data,
                    Text(lists[index]["Price"]).data,false));}else{return 
                    SizedBox.shrink(); };
            });
        )
          
        }
        return Container(
          margin: const EdgeInsets.only(top: 300),
          child:CircularProgressIndicator());
    }),
    

    可能需要一些修改。

    【讨论】:

    • 我添加了确切的代码并更改了一些东西,所以它可以工作,但它在向上滑动时没有任何作用,它可以正确加载但当我滑动时没有任何反应,没有刷新指示器什么都没有
    • RefreshIndicator 是下拉的默认设置,它会刷新页面,但如果你想刷新页面以向上滑动,也许 RefreshIndicator 中的位移属性可以帮助你(我不确定)。您可以查看这两个链接以获取更多信息:medium.com/codechai/…medium.com/@maffan/…
    • 是的,我想下拉,但没有任何动作
    • 您可以在RefreshIndicator中设置key。 // 在小部件中定义 var refreshkey = GlobalKey(); // 在小部件中使用 RefreshIndicator ( key: refreshkey; .... ) 我编辑答案并添加密钥
    猜你喜欢
    • 2021-04-19
    • 2020-06-02
    • 1970-01-01
    • 2021-04-18
    • 2020-05-24
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多