Flutter 之 每日翻译 PreferredSizeWidget

Dart 复制代码
/// An interface for widgets that can return the size this widget would prefer
/// if it were otherwise unconstrained.
///
/// There are a few cases, notably [AppBar] and [TabBar], where it would be
/// undesirable for the widget to constrain its own size but where the widget
/// needs to expose a preferred or "default" size. For example a primary
/// [Scaffold] sets its app bar height to the app bar's preferred height
/// plus the height of the system status bar.
///
/// Widgets that need to know the preferred size of their child can require
/// that their child implement this interface by using this class rather
/// than [Widget] as the type of their `child` property.
///
/// Use [PreferredSize] to give a preferred size to an arbitrary widget.
abstract class PreferredSizeWidget implements Widget {
  /// The size this widget would prefer if it were otherwise unconstrained.
  ///
  /// In many cases it's only necessary to define one preferred dimension.
  /// For example the [Scaffold] only depends on its app bar's preferred
  /// height. In that case implementations of this method can just return
  /// `Size.fromHeight(myAppBarHeight)`.
  Size get preferredSize;
}

An interface for widgets that can return the size this widget would prefer
一个控件接口能够返回它希望的尺寸

if it were otherwise unconstrained
如果他不受约束

There are a few cases
在极少数情况下

notably[AppBar] and [TabBar], where it would be undesirable for the widget to constrain its own size

特别是[AppBar]和[TabBar],小部件不希望约束自己的大小

but where the widget needs to exposea preferred or "default" size.

但小部件需要公开****首选或"默认"大小

For example a primary [Scaffold] sets its app bar height to the app bar's preferred height plus the height of the system status bar.

例如,主要的[Scaffold]将其应用栏的高度设置为应用栏的首选高度加上系统状态栏的高度。

Widgets that need to know the preferred size of their child can require that their child implement this interface by using this class rather than [Widget] as the type of their `child` property

需要知道其子部件的首选大小的小部件可以要求其子部件通过使用这个类而不是[Widget]作为其'子'属性的类型来实现此接口

Use [PreferredSize] to give a preferred size to an arbitrarywidget.

使用[PreferredSize]为任意小部件提供首选大小。

The size this widget would prefer if it were otherwise unconstrained.

如果这个控件大小不受约束会采用首选的大小

In many cases it's only necessary to define one preferred dimension.

在大多数情况下,只需要定义一个首选尺寸

For example the [Scaffold] only depends on its app bar's preferred height

例如在脚手架控件只依赖于它的AppBar的首选高度

In that case implementations of this method can just return `Size.fromHeight(myAppBarHeight)`.

在这种情况下,这个方法的实现可以只返回Size.fromHeight(myAppBarHeight)

相关推荐
один but you14 分钟前
从可变参数到 emplace:现代 C++ 性能优化的核心组合
java·开发语言
IT_陈寒15 分钟前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
idcu37 分钟前
深入 Lyt.js 组件系统:L2 渲染引擎层的核心
前端·typescript
是码龙不是码农41 分钟前
ThreadPoolExecutor 7 个核心参数详解
java·线程池·threadpool
这是程序猿1 小时前
Spring Boot自动配置详解
java·大数据·前端
文心快码BaiduComate1 小时前
干货|Comate Harness Engineering工程实践指南
前端·后端·程序员
还有多久拿退休金1 小时前
一张栈的图,治好你面试答不出 script 阻塞的病
前端·javascript
光辉GuangHui1 小时前
Agent Skill 也需要测试:如何搭建 Skill 评估框架
前端·后端·llm
To_OC1 小时前
我终于搞懂 Claude Code 核心逻辑!90%的人都用错了模式
前端·ai编程
MY_TEUCK1 小时前
【Java 后端 | Nacos 注册中心】微服务治理原理、选型与注册发现实战
java·开发语言·微服务