当前位置:网站首页>【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方法自动刷新。
边栏推荐
- 【工作小技巧】刚入职的软件测试工程师怎么快速上手新岗位
- 观察者模式之通用消息发布与订阅
- 哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码
- How Jerry used to output a clock source to the outside world [chapter]
- [matlab] numerical calculus and equation solving
- Hover effect of eight buttons
- About Equilibrium - Simplified bottleneck model
- Deeply understand and grasp the basic characteristics of digital economy
- Precautions for using Jerry's timer [chapter]
- 篇7:CLion中没有代码提示,,,
猜你喜欢

Operating steps for installing CUDA in win10 (continuous improvement)
![[compilation principle] lexical analysis](/img/b2/8f7dea3944839e27199b28d903d9f0.png)
[compilation principle] lexical analysis

什么是算子?

怎么判断自己是否适合转行软件测试

解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.
![[machine learning] case study of college entrance examination prediction based on multiple time series](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[machine learning] case study of college entrance examination prediction based on multiple time series

SQL Server实时备份库要求

Why do we need ankeri's active power filter in frequency converter occasions?

Bilstm and CRF
![Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]](/img/7a/9c4f4f800c3142ffc279b70354a0bc.png)
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
随机推荐
The stacks 2022:32 marketing technology stacks selected
[compilation principle] overview
The performance of the server's four channel memory is improved. How about the performance of the four channel memory
Use diskgenius to expand the capacity of system disk C
Deeply understand and grasp the basic characteristics of digital economy
What is public chain development? What are the public chain development projects?
lock
A simple and easy-to-use graph visualization tool developed recently
Unity technical manual - size over lifetime and size by speed
Why do we need ankeri's active power filter in frequency converter occasions?
20 provinces and cities announce the road map of the meta universe
Optimization of lazyagg query rewriting in parsing data warehouse
How to open a stock account is it safe to open an account
Bert's summary of me
Can I open an account? Is it safe to open an account
深入理解ELF文件
Utilisation de diskgenius pour augmenter la capacité du disque système C
What is an operator?
什么是泛型以及在集合中泛型的使用[通俗易懂]
图标丢失,URL附带JESSSIONID的什么来的?