Skip to content

Commit b0524b3

Browse files
authored
Add test for animated_positioned.0.dart API example. (#146720)
This PR contributes to flutter/flutter#130459 ### Description - Adds `examples/api/test/widgets/implicit_animations/animated_positioned.0_test.dart` test
1 parent e10e9a9 commit b0524b3

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ final Set<String> _knownMissingTests = <String>{
472472
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',
473473
'examples/api/test/widgets/implicit_animations/animated_fractionally_sized_box.0_test.dart',
474474
'examples/api/test/widgets/implicit_animations/animated_align.0_test.dart',
475-
'examples/api/test/widgets/implicit_animations/animated_positioned.0_test.dart',
476475
'examples/api/test/widgets/implicit_animations/sliver_animated_opacity.0_test.dart',
477476
'examples/api/test/widgets/scroll_view/custom_scroll_view.1_test.dart',
478477
'examples/api/test/widgets/inherited_notifier/inherited_notifier.0_test.dart',

examples/api/lib/widgets/implicit_animations/animated_positioned.0.dart

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,39 @@ void main() => runApp(const AnimatedPositionedExampleApp());
1111
class AnimatedPositionedExampleApp extends StatelessWidget {
1212
const AnimatedPositionedExampleApp({super.key});
1313

14+
static const Duration duration = Duration(seconds: 2);
15+
static const Curve curve = Curves.fastOutSlowIn;
16+
1417
@override
1518
Widget build(BuildContext context) {
1619
return MaterialApp(
1720
home: Scaffold(
1821
appBar: AppBar(title: const Text('AnimatedPositioned Sample')),
1922
body: const Center(
20-
child: AnimatedPositionedExample(),
23+
child: AnimatedPositionedExample(
24+
duration: duration,
25+
curve: curve,
26+
),
2127
),
2228
),
2329
);
2430
}
2531
}
2632

2733
class AnimatedPositionedExample extends StatefulWidget {
28-
const AnimatedPositionedExample({super.key});
34+
const AnimatedPositionedExample({
35+
required this.duration,
36+
required this.curve,
37+
super.key,
38+
});
39+
40+
final Duration duration;
41+
42+
final Curve curve;
2943

3044
@override
31-
State<AnimatedPositionedExample> createState() => _AnimatedPositionedExampleState();
45+
State<AnimatedPositionedExample> createState() =>
46+
_AnimatedPositionedExampleState();
3247
}
3348

3449
class _AnimatedPositionedExampleState extends State<AnimatedPositionedExample> {
@@ -45,8 +60,8 @@ class _AnimatedPositionedExampleState extends State<AnimatedPositionedExample> {
4560
width: selected ? 200.0 : 50.0,
4661
height: selected ? 50.0 : 200.0,
4762
top: selected ? 50.0 : 150.0,
48-
duration: const Duration(seconds: 2),
49-
curve: Curves.fastOutSlowIn,
63+
duration: widget.duration,
64+
curve: widget.curve,
5065
child: GestureDetector(
5166
onTap: () {
5267
setState(() {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:ui';
6+
7+
import 'package:flutter/material.dart';
8+
import 'package:flutter_api_samples/widgets/implicit_animations/animated_positioned.0.dart'
9+
as example;
10+
import 'package:flutter_test/flutter_test.dart';
11+
12+
void main() {
13+
testWidgets(
14+
'AnimatedPositioned animates on tap',
15+
(WidgetTester tester) async {
16+
await tester.pumpWidget(
17+
const example.AnimatedPositionedExampleApp(),
18+
);
19+
20+
final Finder positionedFinder = find.descendant(
21+
of: find.byType(AnimatedPositioned),
22+
matching: find.byType(Positioned),
23+
);
24+
25+
const double beginWidth = 50.0;
26+
const double endWidth = 200.0;
27+
const double beginHeight = 200.0;
28+
const double endHeight = 50.0;
29+
const double beginTop = 150.0;
30+
const double endTop = 50.0;
31+
32+
Positioned positioned = tester.widget(positionedFinder);
33+
expect(positioned.width, beginWidth);
34+
expect(positioned.height, beginHeight);
35+
expect(positioned.top, beginTop);
36+
37+
// Tap on the 'Tap me' text to start the forward animation.
38+
await tester.tap(find.text('Tap me'));
39+
await tester.pump();
40+
41+
positioned = tester.widget(positionedFinder);
42+
expect(positioned.width, beginWidth);
43+
expect(positioned.height, beginHeight);
44+
expect(positioned.top, beginTop);
45+
46+
// Advance animation to the middle.
47+
await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2);
48+
49+
final double t =
50+
example.AnimatedPositionedExampleApp.curve.transform(0.5);
51+
52+
positioned = tester.widget(positionedFinder);
53+
expect(positioned.width, lerpDouble(beginWidth, endWidth, t));
54+
expect(positioned.height, lerpDouble(beginHeight, endHeight, t));
55+
expect(positioned.top, lerpDouble(beginTop, endTop, t));
56+
57+
// Advance animation to the end.
58+
await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2);
59+
60+
positioned = tester.widget(positionedFinder);
61+
expect(positioned.width, endWidth);
62+
expect(positioned.height, endHeight);
63+
expect(positioned.top, endTop);
64+
65+
// Tap on the 'Tap me' text again to start the reverse animation.
66+
await tester.tap(find.text('Tap me'));
67+
await tester.pump();
68+
69+
positioned = tester.widget(positionedFinder);
70+
expect(positioned.width, endWidth);
71+
expect(positioned.height, endHeight);
72+
expect(positioned.top, endTop);
73+
74+
// Advance animation to the middle.
75+
await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2);
76+
77+
positioned = tester.widget(positionedFinder);
78+
expect(positioned.width, lerpDouble(endWidth, beginWidth, t));
79+
expect(positioned.height, lerpDouble(endHeight, beginHeight, t));
80+
expect(positioned.top, lerpDouble(endTop, beginTop, t));
81+
82+
// Advance animation to the end.
83+
await tester.pump(example.AnimatedPositionedExampleApp.duration ~/ 2);
84+
85+
positioned = tester.widget(positionedFinder);
86+
expect(positioned.width, beginWidth);
87+
expect(positioned.height, beginHeight);
88+
expect(positioned.top, beginTop);
89+
},
90+
);
91+
}

0 commit comments

Comments
 (0)