概述

#

PopScope 類添加了泛型型別,並將 onPopInvoked 替換為新方法 onPopInvokedWithResult。新方法將布林值 didPopresult 作為位置引數。

同樣,出於相同的原因,將 Form.onPopInvoked 替換為 Form.onPopInvokedWithResult

背景

#

以前,當呼叫 onPopInvoked 時,PopScope 沒有辦法訪問 pop 結果。泛型型別被新增到 PopScope 類中,以便新方法 onPopInvokedWithResult 可以訪問型別安全的(type-safe)結果。

變更說明

#

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 widget 在具有不同型別的多個路由之間共享,則可以使用 PopScope<Object?> 來捕獲所有可能的型別。

時間線

#

已合併到版本:3.22.0-26.0.pre
穩定版本:3.24.0

參考資料

#

API 文件

相關議題

相關 PR