迁移从 MediaQuery.fromWindow 到 MediaQuery.fromView

huangapple go评论64阅读模式
英文:

Migrate from MediaQuery.fromWindow to MediaQuery.fromView

问题

如何从MediaQuery.fromWindow迁移到MediaQuery.fromView

弃用消息是:
> info: 'fromWindow'已弃用,不应使用。请改用MediaQuery.fromView。该构造函数已在准备未来的多窗口支持时弃用。此功能在v3.7.0-32.0.pre之后弃用。 (在[flame_studio] lib/src/widgets/left_panel.dart:15处使用deprecated_member_use)

fromView方法使用FlutterView实例,我能从上下文中获取吗,或者我该如何获取它?

英文:

How do I migrate from MediaQuery.fromWindow to MediaQuery.fromView?

The deprecation message is:
> info: 'fromWindow' is deprecated and shouldn't be used. Use MediaQuery.fromView instead. This constructor was deprecated in preparation for the upcoming multi-window support. This feature was deprecated after v3.7.0-32.0.pre. (deprecated_member_use at [flame_studio] lib/src/widgets/left_panel.dart:15)

The fromView method is taking a FlutterView instance, can I get that from the context, or how can I get that?

答案1

得分: 8

Without context

如果没有上下文,请使用以下代码:

/// Without context
/// views 可以是 [single, first, last, firstOrNull]
final MediaQueryData data = MediaQueryData.fromView(WidgetsBinding.instance.platformDispatcher.views.single);

return data.size.shortestSide < 600 ? "M" : "T";

With context

/// With Context

/// 基本上 - View.of(context) === WidgetsBinding.instance.window

/// 例子:
/// final double dpr = View.of(context).devicePixelRatio;
/// final Locale locale = View.of(context).platformDispatcher.locale;

MediaQuery.fromView(
  view: View.of(context),
  child: ...
)

有关详细信息,请查阅flutter文档窗口单例已弃用

英文:

Without context

In case you don't have a context use the below code:

/// Without context
/// views can be [single, first, last, firstOrNull]
final MediaQueryData data = MediaQueryData.fromView(WidgetsBinding.instance.platformDispatcher.views.single);

return data.size.shortestSide < 600 ? "M" : "T";

With context

/// With Context

/// Basically - View.of(context) === WidgetsBinding.instance.window

/// Examples:
/// final double dpr = View.of(context).devicePixelRatio;
/// final Locale locale = View.of(context).platformDispatcher.locale;

MediaQuery.fromView(
  view: View.of(context),
  child: ...
)

For more details checkout flutter docs The window singleton is deprecated

答案2

得分: 4

解决方案比我预期的简单得多,只需使用View.of(context)获取FlutterView

MediaQuery.fromView(
  view: View.of(context),
  child: ...
)
英文:

The solution was much simpler than I expected, simply get the FlutterView with View.of(context):

MediaQuery.fromView(
  view: View.of(context),
  child: ...
)

答案3

得分: 1

> **package:flutter/src/foundation/binding.dart**<br>
> SingletonFlutterWindow get window
>
> **包含类:BindingBase**
>
> 已弃用。将在未来的 Flutter 版本中移除。此属性已被弃用,为了准备 Flutter 未来对多视图和多窗口的支持。它代表了仅有一个视图的应用程序的主视图,比如为单屏移动设备设计的应用程序。如果 embedder 支持多视图,它指向第一个被创建的视图,假定它是主视图。如果尚未创建视图或第一个视图已被移除,它会引发异常。
> 
> 以下选项用于迁移依赖于访问此弃用属性的代码:如果有 BuildContext 可用,考虑通过 View.of 查找与该上下文关联的当前 FlutterView,它提供了与此弃用属性相同的功能。然而,平台特定的功能已迁移到 PlatformDispatcher,可以通过 View.of 返回的视图上的 FlutterView.platformDispatcher 访问。在没有上下文可用于查找 FlutterView 的情况下,可以直接使用此绑定暴露的 platformDispatcher 来执行平台特定功能。它还维护了 PlatformDispatcher.views 中所有可用的 FlutterViews 的列表,以便无需上下文访问视图特定功能。
>
> 另请参阅:
> 
> - 使用 View.of 来访问与提供的 BuildContext 关联的 FlutterView 上的视图特定功能。
> - 使用 FlutterView.platformDispatcher 来从给定的 FlutterView 访问平台特定功能。
> - 使用此绑定上的 platformDispatcher 来访问 PlatformDispatcher,它提供了平台特定的功能。
>
> [Flutter 文档](https://docs.flutter.dev/release/breaking-changes/window-singleton)
英文:

If someone need this without context:

> package:flutter/src/foundation/binding.dart<br>
> SingletonFlutterWindow get window
>
> Containing class: BindingBase
>
> Deprecated. Will be removed in a future version of Flutter. This property has been deprecated to prepare for Flutter's upcoming support for multiple views and multiple windows. It represents the main view for applications where there is only one view, such as applications designed for single-display mobile devices. If the embedder supports multiple views, it points to the first view created which is assumed to be the main view. It throws if no view has been created yet or if the first view has been removed again.
>
> The following options exists to migrate code that relies on accessing this deprecated property: If a BuildContext is available, consider looking up the current FlutterView associated with that context via View.of. It gives access to the same functionality as this deprecated property. However, the platform-specific functionality has moved to the PlatformDispatcher, which may be accessed from the view returned by View.of via FlutterView.platformDispatcher. Using View.of with a BuildContext is the preferred option to migrate away from this deprecated window property.
>
> If no context is available to look up a FlutterView, the platformDispatcher exposed by this binding can be used directly for platform-specific functionality. It also maintains a list of all available FlutterViews in PlatformDispatcher.views to access view-specific functionality without a context.
>
> See also:
>
> - View.of to access view-specific functionality on the FlutterView associated with the provided BuildContext.
> - FlutterView.platformDispatcher to access platform-specific functionality from a given FlutterView.
> - platformDispatcher on this binding to access the PlatformDispatcher, which provides platform-specific functionality.

https://docs.flutter.dev/release/breaking-changes/window-singleton

huangapple
  • 本文由 发表于 2023年5月11日 18:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定