当前位置:网站首页>【flutter 页面跳转后退如何刷新?】
【flutter 页面跳转后退如何刷新?】
2022-06-25 18:02:00 【摸鱼的图图】
百度了很多,但是写的不清楚,引用网上的一个例子
https://www.nstack.in/blog/flutter-refresh-on-navigator-pop-or-go-back/
代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Refresh on Go Back',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int id = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Data: $id',
style: Theme.of(context).textTheme.headline5,
),
RaisedButton(
child: Text('Second Page'),
onPressed: navigateSecondPage,
),
],
),
),
);
}
void refreshData() {
id++;
}
onGoBack(dynamic value) {
refreshData();
setState(() {
});
}
// 第一部分需要设置的地方
void navigateSecondPage() {
Route route = MaterialPageRoute(builder: (context) => SecondPage());
Navigator.push(context, route).then(onGoBack);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: RaisedButton(
onPressed: () {
//第二部分要设置的地方
Navigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}
后退刷新的关键:
从HomePage到SecondPage 使用
// 第一部分需要设置的地方
void navigateSecondPage() {
Route route = MaterialPageRoute(builder: (context) => SecondPage());
Navigator.push(context, route).then(onGoBack);
}
关键也在这里,就是有.then中的刷新方法。
onGoBack(dynamic value) {
refreshData();
setState(() {});
};
从SecondPage后退到HomePage时,使用
//第二部分要设置的地方
Navigator.pop(context);
也就是说,不需要额外的传递参数,后退到HomePage时会根据第一次跳转过来的 Navigator.push中的.then方法自动刷新。
边栏推荐
- Wechat applet reports an error: request:fail URL not in domain list
- 喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可
- Use diskgenius to expand the capacity of system disk C
- 1、对范数的理解
- SDN系统方法 | 10. SDN的未来
- 延时函数如何延时
- mysql mysql-8.0.19-winx64 安装与navicat连接
- win10安装cuda的操作步骤(不断完美中)
- CONDA modifying a mirror source
- Why do we need ankeri's active power filter in frequency converter occasions?
猜你喜欢

A simple and easy-to-use graph visualization tool developed recently

Use diskgenius to expand the capacity of system disk C

New characteristics of cultural consumption in the era of digital economy

Unity technical manual - interference / noise sub module

智能对话01-redis的安装

Article 6:clion:toolchains are not configured configure disable profile

Introduction to the container of() function

使用DiskGenius拓展系統盤C盤的容量

微服务介绍

微信小程序报错:request:fail url not in domain list
随机推荐
Swagger implements background interface automation document
Py3.6 and py3.7 installed by CONDA
智能对话01-redis的安装
什么是算子?
使用DiskGenius拓展系统盘C盘的容量
Essential characteristics of convolution operation +textcnn text classification
Android物联网应用程序开发(智慧园区)—— 图片预览界面
哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码
bert之我的小总结
Chapter 4:win10 installing mingw64
Sentinel sentinel mechanism
RuntimeError: Trying to backward through the graph a second time (or directly access saved variable
[matlab] numerical calculus and equation solving
container of()函数简介
Video production material website arrangement
Wechat applet reports an error: request:fail URL not in domain list
Is it safe for a securities company to open an account with the lowest handling fee among the top ten
Is the actual account opening complicated? Is online account opening safe?
有关均衡----简易版瓶颈模型
【工作小技巧】刚入职的软件测试工程师怎么快速上手新岗位