動畫化容器的屬性
如何使用隱式動畫為容器的屬性新增動畫。
Container 類提供了一種簡便的方法來建立具有特定屬性(如寬度、高度、背景顏色、內邊距、邊框等)的小部件。
簡單的動畫通常涉及隨時間改變這些屬性。例如,你可能希望將背景顏色從灰色漸變為綠色,以指示使用者已選中某項。
為了實現這些屬性的動畫,Flutter 提供了 AnimatedContainer 小部件。與 Container 小部件類似,AnimatedContainer 允許你定義寬度、高度、背景顏色等。然而,當 AnimatedContainer 使用新屬性重新構建時,它會自動在舊值和新值之間進行動畫過渡。在 Flutter 中,這類動畫被稱為“隱式動畫”。
本指南透過以下步驟介紹如何使用 AnimatedContainer 在使用者點選按鈕時實現尺寸、背景顏色和圓角的動畫效果:
- 建立一個具有預設屬性的 StatefulWidget。
- 使用這些屬性構建一個
AnimatedContainer。 - 透過使用新屬性重新構建來啟動動畫。
1. 建立一個具有預設屬性的 StatefulWidget
#首先,建立 StatefulWidget 和 State 類。使用自定義的 State 類來定義隨時間變化的屬性。在這個示例中,包括寬度、高度、顏色和圓角。你還可以定義每個屬性的預設值。
這些屬性屬於自定義的 State 類,以便在使用者點選按鈕時可以更新它們。
class AnimatedContainerApp extends StatefulWidget {
const AnimatedContainerApp({super.key});
@override
State<AnimatedContainerApp> createState() => _AnimatedContainerAppState();
}
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
// Define the various properties with default values. Update these properties
// when the user taps a FloatingActionButton.
double _width = 50;
double _height = 50;
Color _color = Colors.green;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);
@override
Widget build(BuildContext context) {
// Fill this out in the next steps.
}
}
2. 使用這些屬性構建一個 AnimatedContainer
#
接下來,使用上一步定義的屬性構建 AnimatedContainer。此外,提供一個 duration(持續時間)來定義動畫執行的時長。
AnimatedContainer(
// Use the properties stored in the State class.
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
// Define how long the animation should take.
duration: const Duration(seconds: 1),
// Provide an optional curve to make the animation feel smoother.
curve: Curves.fastOutSlowIn,
)
3. 透過使用新屬性重新構建來啟動動畫
#最後,透過使用新屬性重新構建 AnimatedContainer 來啟動動畫。如何觸發重新構建?使用 setState() 方法即可。
在應用中新增一個按鈕。當用戶點選該按鈕時,在 setState() 呼叫中更新屬性,設定新的寬度、高度、背景顏色和圓角。
實際應用通常在固定值之間轉換(例如,從灰色背景變為綠色背景)。對於此示例,我們會在使用者每次點選按鈕時生成新的值。
FloatingActionButton(
// When the user taps the button
onPressed: () {
// Use setState to rebuild the widget with new values.
setState(() {
// Create a random number generator.
final random = Random();
// Generate a random width and height.
_width = random.nextInt(300).toDouble();
_height = random.nextInt(300).toDouble();
// Generate a random color.
_color = Color.fromRGBO(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
1,
);
// Generate a random border radius.
_borderRadius = BorderRadius.circular(
random.nextInt(100).toDouble(),
);
});
},
child: const Icon(Icons.play_arrow),
)
互動示例
#import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const AnimatedContainerApp());
class AnimatedContainerApp extends StatefulWidget {
const AnimatedContainerApp({super.key});
@override
State<AnimatedContainerApp> createState() => _AnimatedContainerAppState();
}
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
// Define the various properties with default values. Update these properties
// when the user taps a FloatingActionButton.
double _width = 50;
double _height = 50;
Color _color = Colors.green;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AnimatedContainer Demo')),
body: Center(
child: AnimatedContainer(
// Use the properties stored in the State class.
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
// Define how long the animation should take.
duration: const Duration(seconds: 1),
// Provide an optional curve to make the animation feel smoother.
curve: Curves.fastOutSlowIn,
),
),
floatingActionButton: FloatingActionButton(
// When the user taps the button
onPressed: () {
// Use setState to rebuild the widget with new values.
setState(() {
// Create a random number generator.
final random = Random();
// Generate a random width and height.
_width = random.nextInt(300).toDouble();
_height = random.nextInt(300).toDouble();
// Generate a random color.
_color = Color.fromRGBO(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
1,
);
// Generate a random border radius.
_borderRadius = BorderRadius.circular(
random.nextInt(100).toDouble(),
);
});
},
child: const Icon(Icons.play_arrow),
),
),
);
}
}