@@ -20,20 +20,39 @@ In this recipe, you build a drawer menu with animated
20
20
content that is staggered and has a button that pops
21
21
in at the bottom.
22
22
23
+ 一个应用的单个屏幕可能包含多个动画。
24
+ 所有动画同时播放可能会显得过于混乱。
25
+ 而依次播放动画又可能花费太长时间。
26
+ 更好的选择是错开动画的播放时间。
27
+ 每个动画在不同的时间开始,
28
+ 但这些动画会重叠播放,从而缩短整体持续时间。
29
+ 在这个示例中,你将构建一个带有动画内容的抽屉菜单,
30
+ 动画会交错播放,并且底部会有一个弹出的按钮。
31
+
23
32
The following animation shows the app's behavior:
24
33
34
+ 以下动画展示了应用的行为:
35
+
25
36
![ Staggered Menu Animation Example] ( /assets/images/docs/cookbook/effects/StaggeredMenuAnimation.webp ) {:.site-mobile-screenshot}
26
37
27
38
## Create the menu without animations
28
39
40
+ ## 创建没有动画的菜单
41
+
29
42
The drawer menu displays a list of titles,
30
43
followed by a Get started button at
31
44
the bottom of the menu.
32
45
46
+ 抽屉菜单显示了一系列标题,
47
+ 菜单底部有一个 “Get started” 按钮。
48
+
33
49
Define a stateful widget called ` Menu `
34
50
that displays the list and button
35
51
in static locations.
36
52
53
+ 定义一个名为 ` Menu ` 的 stateful widget,
54
+ 该 widget 在固定位置显示列表和按钮。
55
+
37
56
<? code-excerpt "lib/step1.dart (step1)"?>
38
57
``` dart
39
58
class Menu extends StatefulWidget {
@@ -122,13 +141,20 @@ class _MenuState extends State<Menu> {
122
141
123
142
## Prepare for animations
124
143
144
+ ## 为动画做好准备
145
+
125
146
Control of the animation timing requires an
126
147
` AnimationController ` .
127
148
149
+ 控制动画时序需要一个 ` AnimationController ` 。
150
+
128
151
Add the ` SingleTickerProviderStateMixin `
129
152
to the ` MenuState ` class. Then, declare and
130
153
instantiate an ` AnimationController ` .
131
154
155
+ 将 ` SingleTickerProviderStateMixin ` 添加到 ` MenuState ` 类中。
156
+ 然后,声明并实例化一个 ` AnimationController ` 。
157
+
132
158
<? code-excerpt "lib/step2.dart (animation-controller)" plaster="none"?>
133
159
``` dart
134
160
class _MenuState extends State<Menu> with SingleTickerProviderStateMixin {
@@ -154,6 +180,10 @@ up to you. Define the animation delays,
154
180
individual animation durations, and the total
155
181
animation duration.
156
182
183
+ 每个动画之前的延迟时长由你决定。
184
+ 定义动画延迟、单个动画持续时间和总动画持续时间。
185
+
186
+
157
187
<? code-excerpt "lib/animation_delays.dart (delays)" plaster="none"?>
158
188
``` dart
159
189
class _MenuState extends State<Menu> with SingleTickerProviderStateMixin {
@@ -179,12 +209,25 @@ After the last list item begins to slide in,
179
209
the button at the bottom waits another 150 ms to pop in.
180
210
The button animation takes 500 ms.
181
211
212
+ 在这个示例中,所有动画都延迟了 50 毫秒。
213
+ 随后,列表项开始出现。
214
+ 每个列表项的出现会比上一个列表项开始向左滑入延迟 50 毫秒。
215
+ 每个列表项从右向左滑入需要 250 毫秒。
216
+ 在最后一个列表项开始向左滑入之后,
217
+ 底部的按钮会再等待 150 毫秒弹出。
218
+ 按钮动画需要 500 毫秒。
219
+
182
220
With each delay and animation duration defined,
183
221
the total duration is calculated so that it can be
184
222
used to calculate the individual animation times.
185
223
224
+ 在定义每个延迟和动画持续时间后,
225
+ 计算出总持续时间,以便用于计算各个动画的时间。
226
+
186
227
The desired animation times are shown in the following diagram:
187
228
229
+ 所需的动画时间如下图所示:
230
+
188
231
![ Animation Timing Diagram] ( /assets/images/docs/cookbook/effects/TimingDiagram.png ) {:.site-mobile-screenshot}
189
232
190
233
To animate a value during a subsection of a larger animation,
@@ -197,9 +240,19 @@ end times. For example, given an animation that takes 1 second,
197
240
an interval from 0.2 to 0.5 would start at 200 ms
198
241
(20%) and end at 500 ms (50%).
199
242
243
+ 为了在一个较大的动画中,对某些动画值进行分段处理,
244
+ Flutter 提供了 ` Interval ` 类。
245
+ ` Interval ` 接受一个开始时间百分比和一个结束时间百分比。
246
+ 然后,可以使用该 ` Interval ` 在开始和结束时间之间对值进行动画处理,
247
+ 而不是使用整个动画的开始和结束时间。
248
+ 例如,对于一个持续 1 秒的动画,
249
+ Interval(0.2, 0.5) 将在 200 毫秒(20%)开始,并在 500 毫秒(50%)结束。
250
+
200
251
Declare and calculate each list item's ` Interval ` and the
201
252
bottom button ` Interval ` .
202
253
254
+ 声明并且计算每个列选项的 ` Interval ` 和底部按钮的 ` Interval ` 。
255
+
203
256
<? code-excerpt "lib/step3.dart (step3)" plaster="none"?>
204
257
``` dart
205
258
class _MenuState extends State<Menu> with SingleTickerProviderStateMixin {
@@ -243,10 +296,16 @@ class _MenuState extends State<Menu> with SingleTickerProviderStateMixin {
243
296
244
297
## Animate the list items and button
245
298
299
+ ## 对列表项和按钮进行动画处理
300
+
246
301
The staggered animation plays as soon as the menu becomes visible.
247
302
303
+ 交错动画会在菜单可见时立即播放
304
+
248
305
Start the animation in ` initState() ` .
249
306
307
+ 在 ` initState() ` 中启动播放。
308
+
250
309
<? code-excerpt "lib/step4.dart (init-state)"?>
251
310
``` dart
252
311
@override
@@ -265,10 +324,15 @@ void initState() {
265
324
Each list item slides from right to left and
266
325
fades in at the same time.
267
326
327
+ 每个列表项从右到左滑动的同时逐渐淡入。
328
+
268
329
Use the list item's ` Interval ` and an ` easeOut `
269
330
curve to animate the opacity and translation
270
331
values for each list item.
271
332
333
+ 使用列表项的 ` Interval ` 和 ` easeOut ` 曲线 (curve)
334
+ 来为每个列表项的不透明度和位移值进行动画处理。
335
+
272
336
<? code-excerpt "lib/step4.dart (build-list-items)"?>
273
337
``` dart
274
338
List<Widget> _buildListItems() {
@@ -311,6 +375,9 @@ Use the same approach to animate the opacity and
311
375
scale of the bottom button. This time, use an
312
376
` elasticOut ` curve to give the button a springy effect.
313
377
378
+ 使用相同的方法来为底部按钮的不透明度和缩放比例进行动画处理。
379
+ 这次,使用 ` elasticOut ` 曲线 (curve) 为按钮提供弹性效果。
380
+
314
381
<? code-excerpt "lib/step4.dart (build-get-started)"?>
315
382
``` dart
316
383
Widget _buildGetStartedButton() {
@@ -355,8 +422,15 @@ You have an animated menu where the appearance of each
355
422
list item is staggered, followed by a bottom button that
356
423
pops into place.
357
424
425
+ 恭喜!
426
+ 你有了一个动画菜单,
427
+ 每个列表项都是交错出现的,
428
+ 接着底部的按钮会弹出显示。
429
+
358
430
## Interactive example
359
431
432
+ ## 交互示例
433
+
360
434
<? code-excerpt "lib/main.dart"?>
361
435
``` dartpad title="Flutter staggered menu animation hands-on example in DartPad" run="true"
362
436
import 'package:flutter/material.dart';
0 commit comments