Flutter 动画容器

使用 AnimatedContainer 组件,可以在组件属性改变时生成简单的动画。

** 注意:child属性改变不会有动画效果,因为child组件是另外一个组件,需要使用 AnimatedSwitcher 做切换动画。 **

源码下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter动画01',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
double width = 200;
double height = 100;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter动画01')),
body: Center(
child: AnimatedContainer(
duration: const Duration(milliseconds: 300),
width: width,
height: height,
color: Colors.blue,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
double _width = width;
width = height;
height = _width;
});
},
child: const Icon(Icons.animation),
),
);
}
}