preface
We usually have such a requirement: click to modify user information needs to jump to the page, after the modification is completed to return to the previous page, we need to get new data to refresh the modified data. So I looked for ways to do that, and so far I’ve used the following three methods.
The code address
Navigator.of(context).pushNamed().then()
advantages
- You can do that on the return page
pop
Parameter passed inNavigator.pop(context, 'data pass parameter ')
..then(value => print(value))
The value of value isData transmission and
. Use gesture return to receive no parameter value printed asnull
) .then
Method can listen for the gesture return sumNavigator.pop
To return.
disadvantages
- Jump to different pages on each page as many as needed
then
Method, even if you write it as a public method, you need to add it once.
usage
Navigator-of (context).pushnamed (context)'/newView',
arguments: NewView(
content: 'Internet Search Results In Chinese - Wikipedia, the free encyclopedia',
),
).then((value) => print(value)); // Return to the previous page with the parameter Navigator. Pop (context,'Data pass parameter');
Copy the code
Results show
deactivate
advantages
- When we return to the page, we only need to send a request for new data in one place, not separately for each page jump
then
Within the method, it theoretically meets our needs.
disadvantages
- although
deactivate
Will be triggered, but either going to the page or going back to the page will be triggered, so useModalRoute.of(context).isCurrent
Check whether the current page is displayedtrue
Just send a request for a new page. Always returns when using a gesturefalse
Will cause the request to refresh data not to be made. - The parameter cannot be obtained directly
usage
void deactivate() {
super.deactivate();
var bool = ModalRoute.of(context).isCurrent;
if (bool) {
// Listen to the page return, send a request to refresh the page operation
print('Return to NewView page'); }}Copy the code
Results show
didPopNext
advantages
didPopNext
It makes up for the fact that gesture returns don’t trigger, and we don’t need to write additional judgments.
disadvantages
- The parameter cannot be obtained directly
usage
Class DynamicTheme extends StatefulWidget {const DynamicTheme(); static final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>(); @override _DynamicThemeState createState() => _DynamicThemeState(); } @override Widget build(BuildContext context) {return MaterialApp(
title: 'Dynamic Theme', theme: lightTheme.copyWith(platform: _options.platform), darkTheme: darkTheme.copyWith(platform: _options.platform), themeMode: _options.themeMode, ... navigatorObservers: [DynamicTheme.routeObserver], ... routes: _buildRoutes(), ); } // The page uses import'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class NewView extends StatefulWidget {
final String content;
const NewView({
this.content,
});
static const String routeName = '/newView';
@override
_NewViewState createState() => _NewViewState();
}
class _NewViewState extends State<NewView> with RouteAware {
@override
void didChangeDependencies() {
super.didChangeDependencies();
DynamicTheme.routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void didPopNext() {
// Covering route was popped off the navigator.
print('return NewView');
}
@override
void didPush() {
// Route was pushed onto navigator and is now topmost route.
print('enter NewView');
}
@override
void dispose() {
DynamicTheme.routeObserver.unsubscribe(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
// final NewView param = ModalRoute.of(context).settings.arguments;
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
leading: CupertinoButton(
padding: EdgeInsets.zero,
child: Text('return'),
onPressed: () {
// The demo is on the root navigator.
// Navigator.of(context, rootNavigator: true).maybePop();
Navigator.pop(context, 'Data pass parameter');
},
),
),
child: Material(
child: ScrollConfiguration(
behavior: CustomBehavior(),
child: ListView.builder(
primary: true,
itemCount: 60,
itemBuilder: (BuildContext context, int index) {
return Ink(
child: InkWell(
splashColor: Colors.transparent,
onTap: () => Navigator.of(context).pushNamed(
Detail.routeName,
arguments: Detail(value: 'parameters'),), child: Container(height: 44.0, width: mediaquery.of (context).size.width, child: Center(child: Text('Data-$index')),),),); },),),); }}Copy the code
Results show
conclusion
You want to listen to the page and go back and get new data and there’s only one jump and you can listen to it with the THEN method, but if you have a lot of jumps and you need to refresh all the way back you’re advised to do didPopNext.