10-AnimatedSwitcher

Catalogue

AnimatedSwitcher在2个或者多个子组件之间切换时使用动画

效果:

代码:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class GMExampleAnimatedSwitcherTest extends StatefulWidget {
GMExampleAnimatedSwitcherTest({Key key}) : super(key: key);

@override
_GMExampleAnimatedSwitcherTestState createState() =>
_GMExampleAnimatedSwitcherTestState();
}

class _GMExampleAnimatedSwitcherTestState
extends State<GMExampleAnimatedSwitcherTest> {
var _currChild;

@override
void initState() {
super.initState();

_currChild = Container(
key: ValueKey("1"),
height: 300,
width: 300,
color: Colors.red,
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedSwitcher(
duration: Duration(seconds: 1),
child: _currChild,
switchInCurve: Curves.easeIn,
layoutBuilder: (Widget currentChild, List<Widget> previousChildren) {
return Stack(
children: <Widget>[
...previousChildren,
currentChild,
],
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_currChild = Container(
key: ValueKey("2"),
height: 100,
width: 100,
color: Colors.blue,
);
});
},
),
);
// return Scaffold(
// body: Center(
// child: AnimatedSwitcher(
// duration: Duration(seconds: 1),
// child: _currChild,
// switchInCurve: Curves.easeIn,
// transitionBuilder: (Widget child, Animation<double> value) {
// return ScaleTransition(
// child: child,
// scale: value,
// );
// },
// ),
// ),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// setState(() {
// _currChild = Container(
// key: ValueKey("2"),
// height: 300,
// width: 300,
// color: Colors.blue,
// );
// });
// },
// ),
// );
}
}