1+ import 'package:flutter/material.dart' ;
2+ import 'package:flutter_test/flutter_test.dart' ;
3+ import 'package:flutter/rendering.dart' ;
4+ import 'package:flutter/widgets.dart' ;
5+
6+ void main () {
7+ testWidgets ('AnimatedContainer with properties' , (WidgetTester tester) async {
8+ final AnimatedContainer container = AnimatedContainer (
9+ constraints: const BoxConstraints .tightFor (width: 22 , height: 32 ),
10+ decoration: const BoxDecoration (color: Colors .red),
11+ foregroundDecoration: const BoxDecoration (color: Colors .amber),
12+ margin: const EdgeInsets .all (12 ),
13+ padding: const EdgeInsets .all (6 ),
14+ transform: Matrix4 .translationValues (5 , 4 , 0 ),
15+ width: 55 ,
16+ height: 76 ,
17+ curve: Curves .ease,
18+ duration: const Duration (milliseconds: 300 ),
19+ );
20+ expect (container, hasOneLineDescription);
21+ });
22+ testWidgets ('AnimatedContainer test animation control ' , (WidgetTester tester) async {
23+ final GlobalKey key = GlobalKey ();
24+ const BoxDecoration decorationA = BoxDecoration (
25+ color: Colors .amberAccent,
26+ );
27+ const BoxDecoration decoration = BoxDecoration (
28+ color: Colors .blueAccent,
29+ );
30+ BoxDecoration actualDecoration;
31+ await tester.pumpWidget (
32+ AnimatedContainer (
33+ key: key,
34+ duration: const Duration (milliseconds: 300 ),
35+ decoration: decorationA
36+ )
37+ );
38+ final RenderDecoratedBox box = key.currentContext.findRenderObject ();
39+ actualDecoration = box.decoration;
40+ expect (actualDecoration.color, equals (decorationA.color));
41+ await tester.pumpWidget (
42+ AnimatedContainer (
43+ key: key,
44+ duration: const Duration (milliseconds: 300 ),
45+ decoration: decoration
46+ )
47+ );
48+ expect (key.currentContext.findRenderObject (), equals (box));
49+ actualDecoration = box.decoration;
50+ expect (actualDecoration.color, equals (decorationA.color));
51+ await tester.pump (const Duration (seconds: 1 ));
52+ actualDecoration = box.decoration;
53+ expect (actualDecoration.color, equals (decoration.color));
54+ expect (box, hasAGoodToStringDeep);
55+ expect (
56+ box.toStringDeep (minLevel: DiagnosticLevel .info),
57+ equalsIgnoringHashCodes (
58+ 'RenderDecoratedBox#00000\n '
59+ ' │ parentData: <none>\n '
60+ ' │ constraints: BoxConstraints(w=800, h=600)\n '
61+ ' │ size: Size(800, 600)\n '
62+ ' │ decoration: BoxDecoration:\n '
63+ ' │ color: Color(0xff0000ff)\n '
64+ ' │ configuration: ImageConfiguration(bundle:\n '
65+ ' │ PlatformAssetBundle#00000(), devicePixelRatio: 1, platform:\n '
66+ ' │ android)\n '
67+ ' │\n '
68+ ' └─child: RenderLimitedBox#00000\n '
69+ ' │ parentData: <none> (can use size)\n '
70+ ' │ constraints: BoxConstraints(w=800, h=600)\n '
71+ ' │ size: Size(800, 600)\n '
72+ ' │ maxWidth: 0\n '
73+ ' │ maxHeight: 0\n '
74+ ' │\n '
75+ ' └─child: RenderConstrainedBox#00000\n '
76+ ' parentData: <none> (can use size)\n '
77+ ' constraints: BoxConstraints(w=800, h=600)\n '
78+ ' size: Size(800, 600)\n '
79+ ' additionalConstraints: BoxConstraints(biggest)\n ' ,
80+ ),
81+ );
82+ });
83+ testWidgets ('AnimatedContainer testing over animate ' , (WidgetTester tester) async {
84+ await tester.pumpWidget (
85+ AnimatedContainer (
86+ duration: const Duration (milliseconds: 300 ),
87+ color: Colors .teal,
88+ )
89+ );
90+ expect (tester.binding.transientCallbackCount, 0 );
91+ await tester.pump (const Duration (seconds: 1 ));
92+ expect (tester.binding.transientCallbackCount, 0 );
93+ await tester.pumpWidget (
94+ AnimatedContainer (
95+ duration: const Duration (milliseconds: 200 ),
96+ color: Colors .greenAccent,
97+ )
98+ );
99+ expect (tester.binding.transientCallbackCount, 0 );
100+ await tester.pump (const Duration (seconds: 1 ));
101+ expect (tester.binding.transientCallbackCount, 0 );
102+ await tester.pumpWidget (
103+ AnimatedContainer (
104+ duration: const Duration (milliseconds: 200 ),
105+ color: Colors .amber,
106+ )
107+ );
108+ expect (tester.binding.transientCallbackCount, 1 ); // this is the only time an animation should have started!
109+ await tester.pump (const Duration (seconds: 1 ));
110+ expect (tester.binding.transientCallbackCount, 0 );
111+ await tester.pumpWidget (
112+ AnimatedContainer (
113+ duration: const Duration (milliseconds: 200 ),
114+ color: Colors .amberAccent,
115+ )
116+ );
117+ expect (tester.binding.transientCallbackCount, 0 );
118+ });
119+ testWidgets ('AnimatedContainer padding visual-to-directional animation' , (WidgetTester tester) async {
120+ final Key target = UniqueKey ();
121+ await tester.pumpWidget (
122+ Directionality (
123+ textDirection: TextDirection .rtl,
124+ child: AnimatedContainer (
125+ duration: const Duration (milliseconds: 300 ),
126+ padding: const EdgeInsets .only (right: 50 ),
127+ child: SizedBox .expand (key: target),
128+ ),
129+ ),
130+ );
131+ expect (tester.getSize (find.byKey (target)), const Size (750 , 600 ));
132+ expect (tester.getTopRight (find.byKey (target)), const Offset (750 , 0 ));
133+ await tester.pumpWidget (
134+ Directionality (
135+ textDirection: TextDirection .rtl,
136+ child: AnimatedContainer (
137+ duration: const Duration (milliseconds: 300 ),
138+ padding: const EdgeInsetsDirectional .only (start: 100 ),
139+ child: SizedBox .expand (key: target),
140+ ),
141+ ),
142+ );
143+ expect (tester.getSize (find.byKey (target)), const Size (750 , 600 ));
144+ expect (tester.getTopRight (find.byKey (target)), const Offset (750 , 0 ));
145+ await tester.pump (const Duration (milliseconds: 200 ));
146+ expect (tester.getSize (find.byKey (target)), const Size (725 , 600 ));
147+ expect (tester.getTopRight (find.byKey (target)), const Offset (725 , 0 ));
148+ await tester.pump (const Duration (milliseconds: 600 ));
149+ expect (tester.getSize (find.byKey (target)), const Size (700 , 600 ));
150+ expect (tester.getTopRight (find.byKey (target)), const Offset (700 , 0 ));
151+ });
152+ testWidgets ('AnimatedContainer alignment visual-to-directional animation' , (WidgetTester tester) async {
153+ final Key target = UniqueKey ();
154+ await tester.pumpWidget (
155+ Directionality (
156+ textDirection: TextDirection .rtl,
157+ child: AnimatedContainer (
158+ duration: const Duration (milliseconds: 200 ),
159+ alignment: Alignment .topRight,
160+ child: SizedBox (key: target, width: 100 , height: 200 ),
161+ ),
162+ ),
163+ );
164+ expect (tester.getSize (find.byKey (target)), const Size (100 , 200 ));
165+ expect (tester.getTopRight (find.byKey (target)), const Offset (800 , 0 ));
166+ await tester.pumpWidget (
167+ Directionality (
168+ textDirection: TextDirection .rtl,
169+ child: AnimatedContainer (
170+ duration: const Duration (milliseconds: 200 ),
171+ alignment: AlignmentDirectional .bottomStart,
172+ child: SizedBox (key: target, width: 100 , height: 200 ),
173+ ),
174+ ),
175+ );
176+ expect (tester.getSize (find.byKey (target)), const Size (100 , 200 ));
177+ expect (tester.getTopRight (find.byKey (target)), const Offset (800 , 0 ));
178+ await tester.pump (const Duration (milliseconds: 100 ));
179+ expect (tester.getSize (find.byKey (target)), const Size (100 , 200 ));
180+ expect (tester.getTopRight (find.byKey (target)), const Offset (800 , 200 ));
181+ await tester.pump (const Duration (milliseconds: 500 ));
182+ expect (tester.getSize (find.byKey (target)), const Size (100 , 200 ));
183+ expect (tester.getTopRight (find.byKey (target)), const Offset (800 , 400 ));
184+ });
185+ testWidgets ('Animation rerun' , (WidgetTester tester) async {
186+ await tester.pumpWidget (
187+ Center (
188+ child: AnimatedContainer (
189+ duration: const Duration (milliseconds: 200 ),
190+ width: 100 ,
191+ height: 100 ,
192+ child: const Text ('X' , textDirection: TextDirection .ltr)
193+ )
194+ )
195+ );
196+ await tester.pump ();
197+ await tester.pump (const Duration (milliseconds: 100 ));
198+ RenderBox text = tester.renderObject (find.text ('X' ));
199+ expect (text.size.width, equals (100 ));
200+ expect (text.size.height, equals (100 ));
201+ await tester.pump (const Duration (milliseconds: 1000 ));
202+ await tester.pumpWidget (
203+ Center (
204+ child: AnimatedContainer (
205+ duration: const Duration (milliseconds: 200 ),
206+ width: 200 ,
207+ height: 200 ,
208+ child: const Text ('X' , textDirection: TextDirection .ltr)
209+ )
210+ )
211+ );
212+ await tester.pump ();
213+ await tester.pump (const Duration (milliseconds: 100 ));
214+ text = tester.renderObject (find.text ('X' ));
215+ expect (text.size.width, greaterThan (110 ));
216+ expect (text.size.width, lessThan (190 ));
217+ expect (text.size.height, greaterThan (110 ));
218+ expect (text.size.height, lessThan (190 ));
219+ await tester.pump (const Duration (milliseconds: 1000 ));
220+ expect (text.size.width, equals (200 ));
221+ expect (text.size.height, equals (200 ));
222+ await tester.pumpWidget (
223+ Center (
224+ child: AnimatedContainer (
225+ duration: const Duration (milliseconds: 200 ),
226+ width: 200 ,
227+ height: 100 ,
228+ child: const Text ('X' , textDirection: TextDirection .ltr)
229+ )
230+ )
231+ );
232+ await tester.pump ();
233+ await tester.pump (const Duration (milliseconds: 100 ));
234+ expect (text.size.width, equals (200 ));
235+ expect (text.size.height, greaterThan (110 ));
236+ expect (text.size.height, lessThan (190 ));
237+ await tester.pump (const Duration (milliseconds: 1000 ));
238+ expect (text.size.width, equals (200 ));
239+ expect (text.size.height, equals (100 ));
240+ });
241+ }
0 commit comments