Generic types in PopScope
Summary
#Added a generic type to the PopScope class and replaced the onPopInvoked with a new method onPopInvokedWithResult. The new method takes a boolean didPop and a result as position parameters.
Also replaced the Form.onPopInvoked with Form.onPopInvokedWithResult for the same reason.
Context
#Previously, PopScope didn't have a way to access the pop result when onPopInvoked was called. The generic type is added to the PopScope class so that the new method onPopInvokedWithResult can access the type-safe result.
Description of change
#Added a generic type (<T>) to the PopScope class and a new method onPopInvokedWithResult. The onPopInvoked property was deprecated in favor of onPopInvokedWithResult.
Also added a new method onPopInvokedWithResult to Form to replace onPopInvoked.
Migration guide
#Code before migration:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
navigatorKey: nav,
home: Column(
children: [
Form(
canPop: false,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
launchConfirmationDialog();
},
child: MyWidget(),
),
PopScope(
canPop: false,
onPopInvoked: (bool didPop) {
if (didPop) {
return;
}
launchConfirmationDialog();
},
child: MyWidget(),
),
],
),
),
);
}Code after migration:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
navigatorKey: nav,
home: Column(
children: [
Form(
canPop: false,
onPopInvokedWithResult: (bool didPop, Object? result) {
if (didPop) {
return;
}
launchConfirmationDialog();
},
child: MyWidget(),
),
PopScope<Object?>(
canPop: false,
onPopInvokedWithResult: (bool didPop, Object? result) {
if (didPop) {
return;
}
launchConfirmationDialog();
},
child: MyWidget(),
),
],
),
),
);
}The generic type should match the generic type of the Route that the PopScope is in. For example, if the route uses int as its generic type, consider using PopScope<int>.
If the PopScope widgets are shared across multiple routes with different types, you can use PopScope<Object?> to catch all possible types.
Timeline
#Landed in version: 3.22.0-26.0.pre
In stable release: 3.24.0
References
#API documentation:
Relevant issue:
Relevant PR:
- Add generic type for result in PopScope (reverted)
- Reapply new PopScope API (final reland)
除非另有说明,否则本网站上的文档反映的是 Flutter 的最新稳定版本。页面最后更新于 2025-01-30。 查看源代码 或 报告问题。