轻击设备后退按钮时刷新后一页

时间:2020-03-31 13:26:23

标签: flutter

我有一个名为约会的页面,当我单击约会(将报告传递到下一页)时,报告将显示在报告页面上;当我向页面添加新报告时,以及单击设备后退按钮时然后再次进入报告页面,看不到更新的文件。我再次必须加载整个页面,然后我才能查看报告。 我正在页面的initState()中将约会加载到FutureBuilder中。 当我从设备上单击后退按钮时,有人可以建议我如何刷新数据吗

3 个答案:

答案 0 :(得分:0)

在这种情况下,您需要使用RefreshIndicator。您的刷新指示器小部件将是AppointmentsPage的父级。我们将分配一个键,以便我们可以在任何地方调用它,当报表页面关闭时,我们将使用此键刷新页面。我创建了一个带有示例的小示例,并向您展示了如何完成此操作。

class AppointmentsPage extends StatefulWidget {

  @override
  _AppointmentsPageState createState() => _AppointmentsPageState();
}


class _AppointmentsPageState extends State<AppointmentsPage> {
  Future appointments;
  var refreshKey = GlobalKey<RefreshIndicatorState>();

  @override
  void initState() {
    super.initState();
    //Your method to fetch appointments.
    getAppointments();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
          key: refreshKey,
          color: Colors.black,
          onRefresh: () async {
            //Call appointments future and fetch appointments.When they are fetched
            //call setState to refresh page
            getAppointments();
            await appointments;
            setState(() {});
          },
          child: Column(
            children: <Widget>[
              FutureBuilder(),
              RaisedButton(
                child: Text("Go To Report"),
                onPressed: () async {
                  //Use await and then navigate to report page
                  await Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => ReportPage(report: report));
                  );
                  //After popped back from report page call refresh indicator to refresh page
                  refreshKey.currentState.show();
                },
              )
            ],
          ),

      ),
    );


  }


}

答案 1 :(得分:0)

您可以这样做:

在报告页面中,单击按钮后,打开“添加报告页面”。当按下后退按钮时,它将调用Install Objective-C Compatibility Header方法。

No

_refreshData用于加载整个页面,您将在 Navigator.pushNamed(context, AddReportPage.ROUTE).then((onValue) { _refreshData() }); 中使用该页面。

答案 2 :(得分:0)

要打开新页面,您应该使用下一步

void _openButtonHandler() {
    Navigator.of(context).pushNamed("route123").then((result) {
      if (result != null) {
        refreshCall();
      }
    }).catchError((error) {});
  }

后退按钮方法应如下所示:

Navigator.pop(context, {"param1": "value1"});