動畫化容器的屬性
Container 類提供了一種方便的方式來建立具有特定屬性的 widget:寬度、高度、背景顏色、內邊距、邊框等。
簡單的動畫通常涉及隨著時間改變這些屬性。例如,你可能希望將背景顏色從灰色動畫到綠色,以指示使用者已選擇某個專案。
為了對這些屬性進行動畫處理,Flutter 提供了 AnimatedContainer widget。與 Container widget 一樣,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),
),
),
);
}
}