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
在极少数情况下

notablyAppBar and TabBar, where it would be undesirable for the widget to constrain its own size

特别是AppBarTabBar,小部件不希望约束自己的大小

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)

相关推荐
天空之城--10 分钟前
Android Flutter行业动态与学习参考(2026年8月)
android·flutter
东方小月21 分钟前
从零开发一个 Coding Agent(七):实现纯文本 Agent Loop
前端·人工智能
大家的林语冰22 分钟前
👉 尤雨溪再次成立新公司,同时官宣 Pinia 4 正式发布!
前端·javascript·vue.js
浮生望26 分钟前
React useRef 深度解析:从DOM操作到Web Worker多线程协作
前端
ChaHae-In28 分钟前
MyBatis入门操作
java·mybatis
浮生望32 分钟前
React Router v7 实战全景:路由配置、嵌套路由、鉴权守卫与懒加载完整指南
前端
xcLeigh33 分钟前
Go入门:短变量声明的陷阱与最佳实践
java·redis·golang·教程·变量
用户9385156350733 分钟前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformer.js 全链路实战(二)
前端·javascript·typescript
青山木39 分钟前
Hot 100 --- 最小栈
java·数据结构·算法·leetcode
疯狂打码的少年42 分钟前
【数据结构】栈的应用:表达式求值(后缀表达式)
java·数据结构·笔记·算法