概述

#

PopScope 类添加了泛型,并用新方法 onPopInvokedWithResult 取代了 onPopInvoked。新方法将布尔值 didPopresult 作为位置参数。

出于同样的原因,也用 Form.onPopInvokedWithResult 取代了 Form.onPopInvoked

背景

#

之前,当调用 onPopInvoked 时,PopScope 无法访问弹出结果。现在为 PopScope 类添加了泛型,以便新方法 onPopInvokedWithResult 可以访问类型安全的结果。

变更说明

#

PopScope 类添加了泛型 (<T>) 和一个新方法 onPopInvokedWithResultonPopInvoked 属性已弃用,取而代之的是 onPopInvokedWithResult

还为 Form 添加了新方法 onPopInvokedWithResult 以取代 onPopInvoked

迁移指南

#

迁移前的代码

Dart
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(),
          ),
        ],
      ),
    ),
  );
}

迁移后的代码

Dart
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(),
          ),
        ],
      ),
    ),
  );
}

泛型应与 PopScope 所在的 Route 的泛型匹配。例如,如果路由使用 int 作为其泛型,请考虑使用 PopScope<int>

如果 PopScope 小部件在具有不同类型的多个路由之间共享,您可以使用 PopScope<Object?> 来捕获所有可能的类型。

时间线

#

落地版本: 3.22.0-26.0.pre
稳定版本:3.24.0

参考资料

#

API 文档

相关议题

相关 PR