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)

相关推荐
kirk_wang2 分钟前
Flutter艺术探索-Flutter相机与相册:camera库与image_picker集成
flutter·移动开发·flutter教程·移动开发教程
寄存器漫游者12 分钟前
数据结构 C语言 顺序栈
java·c语言·数据结构
heartbeat..13 分钟前
Redis 性能优化全指南:从基础配置到架构升级
java·redis·性能优化·架构
tao35566714 分钟前
【用AI学前端】HTML-02-HTML 常用标签(基础)
前端·html
2601_9495328418 分钟前
Psello HTML Template: A Developer‘s Deep-Dive Review and Guide - Download Free
前端·windows·html·seo·wordpress·gpl
子春一18 分钟前
Flutter for OpenHarmony:构建一个 Flutter 贪吃蛇游戏,深入解析状态机、碰撞检测与响应式游戏循环
flutter·游戏
CappuccinoRose19 分钟前
CSS前端布局总指南
前端·css·学习·布局·flex布局·grid布局·float布局
2601_9495430119 分钟前
Flutter for OpenHarmony垃圾分类指南App实战:主题配置实现
android·flutter
m0_7482331721 分钟前
C#与C语言:5大核心语法共性
java·jvm·算法
JavaGuide22 分钟前
推荐一个基于 Spring Boot 4.0 + Java 21 + Spring AI 2.0 的大模型项目!
java·spring boot·spring