Navigator's page APIs breaking change
Summary
#The Navigator
page APIs are refactored so that
they can integrate with Flutter's other pop mechanisms.
Context
#The onPopPage
property was added for cleaning up pages after
a page is about to be popped.
To veto pop, you'd return false
in the callback.
This did not work well with other popping mechanisms in the framework,
such as PopScope
and iOS back gestures.
To integrate the framework's pop mechanisms together, the page APIs needed to be refactored.
Description of change
#The onDidRemovePage
property replaces the onPopPage
property.
You can no longer veto a pop in the onDidRemovePage
property.
Instead, you are only responsible for updating the pages
.
The veto mechanism is now managed with the
Page.canPop
and Page.onPopInvoked
properties.
These function similar to how you use the PopScope
widget.
Migration guide
#Code before migration:
import 'package:flutter/material.dart';
const MaterialPage<void> page1 = MaterialPage<void>(child: PlaceHolder());
const MaterialPage<void> page2 = MaterialPage<void>(child: PlaceHolder());
const MaterialPage<void> page3 = MaterialPage<void>(child: PlaceHolder());
void main() {
final List<Page<void>> pages = <Page<void>>[page1, page2, page3];
runApp(
MaterialApp(
home: Navigator(
pages: pages,
onPopPage: (Route<Object?> route, Object? result) {
if (route.settings == page2) {
return false;
}
if (route.didPop) {
pages.remove(route.settings);
pages = pages.toList();
return true;
}
return false;
},
),
),
);
}
Code after migration:
import 'package:flutter/material.dart';
const MaterialPage<void> page1 = MaterialPage<void>(child: PlaceHolder());
const MaterialPage<void> page2 = MaterialPage<void>(canPop: false, child: PlaceHolder());
const MaterialPage<void> page3 = MaterialPage<void>(child: PlaceHolder());
void main() {
final List<Page<void>> pages = <Page<void>>[page1, page2, page3];
runApp(
MaterialApp(
home: Navigator(
pages: pages,
onDidRemovePage: (Page<Object?> page) {
pages.remove(page);
pages = pages.toList();
},
),
),
);
}
Timeline
#Landed in version: 3.22.0-32.0.pre
In stable release: 3.24.0
References
#API documentation:
Relevant issue:
Relevant PR:
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-09-04。 查看文档源码 或者 为本页面内容提出建议。