1
1
---
2
- title : Widgets
3
- description : Learn the basic building blocks of Flutter.
2
+ # title: Widgets
3
+ title : Widget
4
+ # description: Learn the basic building blocks of Flutter.
5
+ description : 了解 Flutter 的基本构成要素。
4
6
prev :
5
- title : Intro to Dart
7
+ # title: Intro to Dart
8
+ title : Dart 入门
6
9
path : /get-started/fundamentals/dart
7
10
next :
8
- title : Layout
11
+ # title: Layout
12
+ title : 布局
9
13
path : /get-started/fundamentals/layout
10
14
---
11
15
@@ -19,6 +23,14 @@ Additional resources are listed throughout this page,
19
23
but you do not need to be an expert in either
20
24
subject in order to continue.
21
25
26
+ 在开始使用 Flutter 之前,
27
+ 你需要对 Dart 编程语言以及 Widget 有所了解,
28
+ 因为 Dart 是 Flutter 应用的开发语言,
29
+ 而 Widget 则是 Flutter UI 的基本构成要素。
30
+ 本页将简要介绍这两部分内容,后续的教程会逐步深入讲解它们。
31
+ 此外,本页还提供了若干延伸学习资源,
32
+ 你不必精通它们,也可以继续学习教程的后续内容。
33
+
22
34
## Widgets
23
35
24
36
In regard to Flutter, you'll often hear
@@ -31,12 +43,23 @@ to describe all aspects of a user interface,
31
43
including physical aspects such as text and buttons to
32
44
lay out effects like padding and alignment.
33
45
46
+ 关于 Flutter,你总是会听到「万物皆 Widget (everything is a widget)」的说法。
47
+ Widget 是 Flutter 应用程序用户界面的基本构成要素,
48
+ 每个 Widget 都是对用户界面特定部分的不可变 (immutable) 声明。
49
+ 它们用于描述用户界面的各个维度,
50
+ 包括物理外观(诸如文本和按钮)和布局效果(诸如填充和对齐)。
51
+
34
52
Widgets form a hierarchy based on composition.
35
53
Each widget nests inside its parent and
36
54
can receive context from the parent.
37
55
This structure carries all the way up to the root
38
56
widget, as this trivial example shows:
39
57
58
+ Widget 通过组合机制形成层级结构。
59
+ 每个 Widget 都嵌套于父 Widget 内,
60
+ 并且能够从父级接收上下文信息。
61
+ 正如下面的简例所示,这样的结构关系一直延伸到根 Widget:
62
+
40
63
``` dart
41
64
import 'package:flutter/material.dart';
42
65
import 'package:flutter/services.dart';
@@ -83,13 +106,24 @@ all instantiated classes are widgets:
83
106
` Center ` , ` Builder ` , ` Column ` , ` SizedBox ` , and
84
107
` ElevatedButton ` .
85
108
109
+ 在上面的代码中,所有实例化的类都是 Widget:
110
+ ` MaterialApp ` 、` Scaffold ` 、` AppBar ` 、` Text ` 、
111
+ ` Center ` 、` Builder ` 、` Column ` 、` SizedBox ` 以及
112
+ ` ElevatedButton ` 。
113
+
86
114
### Widget composition
87
115
116
+ ### Widget 的组合结构
117
+
88
118
As mentioned, Flutter emphasizes widgets as a unit
89
119
of composition. Widgets are typically composed of
90
120
many other small, single-purpose widgets that
91
121
combine to produce powerful effects.
92
122
123
+ 如前所述,Flutter 强调将 Widget 作为组合的基本单元。
124
+ 通常,Widget 由许多更加小型、功能单一的 Widget 组合而成,
125
+ 通过层层组合嵌套实现复杂的界面效果。
126
+
93
127
There are layout widgets such
94
128
as ` Padding ` , ` Alignment ` , ` Row ` , ` Column ` ,
95
129
and ` Grid ` . These layout widgets do not have a
@@ -106,6 +140,17 @@ such as `ElevatedButton` and
106
140
` Text ` in the preceding example, as well as
107
141
widgets like ` Icon ` and ` Image ` .
108
142
143
+ Flutter 提供了多种布局 Widget,
144
+ 比如 ` Padding ` 、` Alignment ` 、` Row ` 、` Column ` 和 ` Grid ` 。
145
+ 这些布局类 Widget 本身没有可视化呈现,
146
+ 它们的唯一作用是控制其他 Widget 的布局方式。
147
+ Flutter 还提供了一些利用组合方式构建的实用性 Widget,
148
+ 例如常用的 ` Container ` ,
149
+ 就是由多个负责布局、绘制、定位和尺寸调整的 Widget,各司其职组合而成的。
150
+ 另外还有一些 Widget,它们具备用户界面的可视化呈现,
151
+ 例如前面简例中出现的 ` ElevatedButton ` 和 ` Text ` ,
152
+ 以及 ` Icon ` 和 ` Image ` 等类似的可视化 Widget。
153
+
109
154
If you run the code from the preceding example,
110
155
Flutter paints a button with the text
111
156
"Hello, World!" centered on the screen, laid out vertically.
@@ -114,21 +159,38 @@ which positions its children in the center
114
159
of the available space, and a ` Column ` widget,
115
160
which lays out its children vertically one after another.
116
161
162
+ 如果你运行前面简例中的代码,
163
+ Flutter 将在屏幕中央以垂直布局的方式绘制一行「Hello, World!」文字和一个按钮。
164
+ 为了定位这些元素,
165
+ 代码使用 ` Center ` Widget 实现居中(将子 Widget 置于可用区域中心),
166
+ 使用 ` Column ` Widget 完成垂直方向的排列布局(将所有子 Widget 按垂直方向顺序排列)。
167
+
117
168
<img src =' /assets/images/docs/fwe/simple_composition_example.png ' width =" 100% " alt =" A diagram that shows widget composition with a series of lines and nodes. " >
118
169
119
170
120
171
In the [ next page] [ ] in this series, you will
121
172
learn more about layout in Flutter.
122
173
174
+ 在本章节的 [ 下一页] [ next page ] ,你会学到更多关于 Flutter 布局的知识。
175
+
123
176
### Building widgets
124
177
178
+ ### 构建 Widget
179
+
125
180
To create a user interface in Flutter,
126
181
you override the [ ` build ` ] [ ] method on widget objects.
127
182
All widgets must have a build method,
128
183
and it must return another widget. For example,
129
184
if you want to add text to the screen with some padding,
130
185
you could write it like this:
131
186
187
+ 在 Flutter 中创建用户界面时,
188
+ 你需要重写 Widget 对象的 [ ` build ` ] [ ] 方法。
189
+ 所有的 Widget 都必须包含一个 build 方法,
190
+ 并且该方法必须返回新的 Widget 实例。
191
+ 举例来说,如果你想在屏幕上添加一段带内边距的文本,
192
+ 你可以这样编写代码:
193
+
132
194
``` dart
133
195
class PaddedText extends StatelessWidget {
134
196
const PaddedText({super.key});
@@ -152,11 +214,24 @@ building a widget.
152
214
To learn more about how Flutter renders widgets,
153
215
check out the [ Flutter architectural overview] [ ] .
154
216
217
+ 当这个 Widget 被创建
218
+ 或者它的依赖项(例如传递给 Widget 的状态)发生变化时,
219
+ 框架就会调用 ` build ` 方法。
220
+ 这个方法有可能会在每一帧都被调用,
221
+ 所以它不应该有副作用,唯一职责就是完成 Widget 的构建。
222
+ 要深入了解 Flutter 如何渲染 Widget,
223
+ 请参阅 [ Flutter 架构概览] [ Flutter architectural overview ] 。
224
+
155
225
### Widget state
156
226
227
+ ### Widget 状态
228
+
157
229
The framework introduces two major classes of widget:
158
230
stateful and stateless widgets.
159
231
232
+ Flutter 框架将 Widget 分为两个大类:
233
+ 有状态 (Stateful) Widget 和 无状态 (Stateless) Widget。
234
+
160
235
Widgets that have no mutable state
161
236
(they have no class properties
162
237
that change over time) subclass [ ` StatelessWidget ` ] [ ] .
@@ -165,6 +240,13 @@ such as `Padding`, `Text`, and `Icon`.
165
240
When you create your own widgets,
166
241
you'll create ` Stateless ` widgets most of the time.
167
242
243
+ 不包含可变状态(即没有随时间变化的成员属性)的 Widget
244
+ 是无状态 Widget,均继承自 [ ` StatelessWidget ` ] [ ] 。
245
+ 许多内置的 Widget 都是无状态的,
246
+ 比如 ` Padding ` 、` Text ` 和 ` Icon ` 。
247
+ 当你构建自定义 Widget 时,
248
+ 优先采用 ` 无状态 (Stateless) ` Widget。
249
+
168
250
On the other hand,
169
251
if the unique characteristics of a widget need to change
170
252
based on user interaction or other factors,
@@ -182,6 +264,20 @@ subclasses [`State`][].
182
264
instead, their user interface is built through
183
265
their ` State ` object, as shown in the example below.
184
266
267
+ 反之,
268
+ 如果一个 Widget 的某些特性需要随用户交互或其他因素而改变,
269
+ 则这个 Widget 是有状态的。
270
+ 举例来说,如果有一个包含计数器的 Widget,
271
+ 当用户点击按钮时,计数器的数值会递增,
272
+ 那么这个数值就是 Widget 的状态。
273
+ 每当这个值改变的时候,Widget 就需要被重建以更新它在 UI 中的部分。
274
+ 有状态的 Widget 继承自 [ ` StatefulWidget ` ] [ ] ,
275
+ (因为 Widget 本身是不可变的)
276
+ 它们将可变状态存放在一个单独继承自 [ ` State ` ] [ ] 的类中。
277
+ ` StatefulWidget ` 没有 ` build ` 方法,
278
+ 它们的用户界面是通过关联其 ` State ` 对象来构建的,
279
+ 正如下面的例子所示。
280
+
185
281
``` dart
186
282
class CounterWidget extends StatefulWidget {
187
283
@override
@@ -210,6 +306,10 @@ you must call [`setState`][] to signal the framework
210
306
to update the user interface by
211
307
calling the ` State ` 's ` build ` method again.
212
308
309
+ 每当你试图修改状态时(比如,增加计数器的数值),
310
+ 你必须要调用 [ ` setState ` ] [ ] 方法来通知框架更新用户界面,
311
+ 该操作会触发关联 ` State ` 的 ` build ` 方法重新执行构建。
312
+
213
313
Separating state from widget objects
214
314
lets other widgets treat both
215
315
stateless and stateful widgets in exactly the same way,
@@ -222,19 +322,37 @@ the child's persistent state.
222
322
The framework does all the work of finding and
223
323
reusing existing state objects when appropriate.
224
324
325
+ 将状态与 Widget 分离的机制,
326
+ 使得其他 Widget 可以统一处理无状态和有状态的 Widget,
327
+ 而不用担心丢失状态。
328
+ 父 Widget 无需保留子 Widget 的状态,
329
+ 可以随时创建子 Widget 的新实例,
330
+ 这样做不会破坏子 Widget 状态的持久化。
331
+ 框架会在合适的时机自动匹配并复用现有的 State 对象。
332
+
225
333
There's more information about
226
334
[ ` StatefulWidget ` ] [ ] objects later in this
227
335
series, in the [ state management lesson] [ ] .
228
336
337
+ 入门教程的后续章节会对 [ ` StatefulWidget ` ] [ ] 进行更深入的讲解,
338
+ 具体请参阅 [ 状态管理] [ state management lesson ] 部分。
339
+
229
340
## Important widgets to know
230
341
342
+ ## 必须了解的重点 Widget
343
+
231
344
The Flutter SDK includes many built-in widgets,
232
345
from the smallest pieces of UI, like ` Text ` ,
233
346
to layout widgets, and widgets that style
234
347
your application. The following widgets are
235
348
the most important to be aware of as you move onto the
236
349
next lesson in the learning pathway.
237
350
351
+ Flutter 的 SDK 提供了许多内置的 Widget,
352
+ 小到 UI 基础类 Widget(比如 ` Text ` ),
353
+ 大到布局类 Widget 和那些影响整个应用风格的 Widget。
354
+ 以下列出的 Widget 是你在后续学习过程中必须要掌握的重点。
355
+
238
356
* [ ` Container ` ] [ ]
239
357
* [ ` Text ` ] [ ]
240
358
* [ ` Scaffold ` ] [ ]
@@ -246,6 +364,8 @@ next lesson in the learning pathway.
246
364
247
365
## Next: Layouts
248
366
367
+ ## 下一步:布局
368
+
249
369
This page is an introduction to foundational
250
370
Flutter concepts, like widgets,
251
371
and helps you become familiar with reading
@@ -255,10 +375,20 @@ this is a deep-dive on specific topics.
255
375
In the next section, you'll start building more
256
376
interesting UIs by creating more complex layouts in Flutter.
257
377
378
+ 本页介绍了 Flutter 的基础概念(例如 Widget),
379
+ 旨在帮助你熟悉 Flutter 和 Dart 代码。
380
+ 你不必要求自己充分理解现在所有的知识点,
381
+ 因为后续章节将带你继续深入学习它们。
382
+ 在下一章节中,你将使用 Flutter 创建更复杂的布局,
383
+ 从而构建更引人入胜的 UI。
384
+
258
385
If you'd like practice with the
259
386
information you learned on this page,
260
387
you can read [ Building user interfaces with Flutter] [ ] .
261
388
389
+ 如果你想通过实践来巩固本页所学的内容,
390
+ 请移步 [ 使用 Flutter 构建用户界面] [ Building user interfaces with Flutter ] 。
391
+
262
392
[ Building user interfaces with Flutter ] : /ui
263
393
[ `build` ] : {{site.api}}/flutter/widgets/StatelessWidget/build.html
264
394
[ next page ] : /get-started/fundamentals/layout
@@ -280,7 +410,11 @@ you can read [Building user interfaces with Flutter][].
280
410
281
411
## Feedback
282
412
413
+ ## 反馈
414
+
283
415
As this section of the website is evolving,
284
416
we [ welcome your feedback] [ ] !
285
417
286
- [ welcome your feedback ] : https://google.qualtrics.com/jfe/form/SV_6A9KxXR7XmMrNsy?page="widgets"
418
+ 为了把教程做得越来越好,我们 [ 期待你的反馈] [ welcome your feedback ] !
419
+
420
+ [ welcome your feedback ] : https://google.qualtrics.com/jfe/form/SV_6A9KxXR7XmMrNsy?page="widgets"
0 commit comments