Skip to content

Commit 65e8c61

Browse files
authored
Add test for animated_size.0.dart API example. (#147828)
This PR contributes to flutter/flutter#130459 ### Description - Updates `examples/api/lib/widgets/animated_size/animated_size.0.dart` API example by using `SizedBox` to size the surrounding of the `FlutterLogo` while setting the constant `FlutterLogo` size. This was done because `FlutterLogo` already has size animation under the hood, and the main goal of this example is to show `AnimatedSize` usage - Adds test for `examples/api/lib/widgets/animated_size/animated_size.0.dart`
1 parent fdfc408 commit 65e8c61

4 files changed

Lines changed: 108 additions & 18 deletions

File tree

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ final Set<String> _knownMissingTests = <String>{
406406
'examples/api/test/widgets/media_query/media_query_data.system_gesture_insets.0_test.dart',
407407
'examples/api/test/widgets/async/future_builder.0_test.dart',
408408
'examples/api/test/widgets/restoration_properties/restorable_value.0_test.dart',
409-
'examples/api/test/widgets/animated_size/animated_size.0_test.dart',
410409
'examples/api/test/widgets/animated_switcher/animated_switcher.0_test.dart',
411410
'examples/api/test/widgets/transitions/relative_positioned_transition.0_test.dart',
412411
'examples/api/test/widgets/transitions/positioned_transition.0_test.dart',

examples/api/lib/widgets/animated_size/animated_size.0.dart

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,62 @@ void main() => runApp(const AnimatedSizeExampleApp());
1111
class AnimatedSizeExampleApp extends StatelessWidget {
1212
const AnimatedSizeExampleApp({super.key});
1313

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

2733
class AnimatedSizeExample extends StatefulWidget {
28-
const AnimatedSizeExample({super.key});
34+
const AnimatedSizeExample({
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
3145
State<AnimatedSizeExample> createState() => _AnimatedSizeExampleState();
3246
}
3347

3448
class _AnimatedSizeExampleState extends State<AnimatedSizeExample> {
35-
double _size = 50.0;
36-
bool _large = false;
37-
38-
void _updateSize() {
39-
setState(() {
40-
_size = _large ? 250.0 : 100.0;
41-
_large = !_large;
42-
});
43-
}
49+
bool _isSelected = false;
4450

4551
@override
4652
Widget build(BuildContext context) {
4753
return GestureDetector(
48-
onTap: () => _updateSize(),
54+
onTap: () {
55+
setState(() {
56+
_isSelected = !_isSelected;
57+
});
58+
},
4959
child: ColoredBox(
5060
color: Colors.amberAccent,
5161
child: AnimatedSize(
52-
curve: Curves.easeIn,
53-
duration: const Duration(seconds: 1),
54-
child: FlutterLogo(size: _size),
62+
duration: widget.duration,
63+
curve: widget.curve,
64+
child: SizedBox.square(
65+
dimension: _isSelected ? 250.0 : 100.0,
66+
child: const Center(
67+
child: FlutterLogo(size: 75.0),
68+
),
69+
),
5570
),
5671
),
5772
);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/widgets/animated_size/animated_size.0.dart'
7+
as example;
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
void main() {
11+
testWidgets('AnimatedSize animates on tap', (WidgetTester tester) async {
12+
await tester.pumpWidget(
13+
const example.AnimatedSizeExampleApp(),
14+
);
15+
16+
const Size beginSize = Size.square(100.0);
17+
const Size endSize = Size.square(250.0);
18+
19+
RenderBox box = tester.renderObject(find.byType(AnimatedSize));
20+
expect(box.size, equals(beginSize));
21+
22+
// Tap on the AnimatedSize to start the forward animation.
23+
await tester.tap(find.byType(AnimatedSize));
24+
await tester.pump();
25+
26+
box = tester.renderObject(find.byType(AnimatedSize));
27+
expect(box.size, equals(beginSize));
28+
29+
// Advance animation to the middle.
30+
await tester.pump(
31+
example.AnimatedSizeExampleApp.duration ~/ 2,
32+
);
33+
34+
final double t = example.AnimatedSizeExampleApp.curve.transform(0.5);
35+
36+
box = tester.renderObject(find.byType(AnimatedSize));
37+
expect(
38+
box.size,
39+
equals(Size.lerp(beginSize, endSize, t)),
40+
);
41+
42+
// Advance animation to the end.
43+
await tester.pump(
44+
example.AnimatedSizeExampleApp.duration ~/ 2,
45+
);
46+
47+
box = tester.renderObject(find.byType(AnimatedSize));
48+
expect(box.size, equals(endSize));
49+
50+
// Tap on the AnimatedSize again to start the reverse animation.
51+
await tester.tap(find.byType(AnimatedSize));
52+
await tester.pump();
53+
54+
box = tester.renderObject(find.byType(AnimatedSize));
55+
expect(box.size, equals(endSize));
56+
57+
// Advance animation to the middle.
58+
await tester.pump(
59+
example.AnimatedSizeExampleApp.duration ~/ 2,
60+
);
61+
62+
box = tester.renderObject(find.byType(AnimatedSize));
63+
expect(
64+
box.size,
65+
equals(Size.lerp(endSize, beginSize, t)),
66+
);
67+
68+
// Advance animation to the end.
69+
await tester.pump(
70+
example.AnimatedSizeExampleApp.duration ~/ 2,
71+
);
72+
73+
box = tester.renderObject(find.byType(AnimatedSize));
74+
expect(box.size, equals(beginSize));
75+
});
76+
}

packages/flutter/lib/src/widgets/animated_size.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import 'ticker_provider.dart';
1212
/// duration whenever the given child's size changes.
1313
///
1414
/// {@tool dartpad}
15-
/// This example makes a [Container] react to being touched, causing the child
16-
/// of the [AnimatedSize] widget, here a [FlutterLogo], to animate.
15+
/// This example defines a widget that uses [AnimatedSize] to change the size of
16+
/// the [SizedBox] on tap.
1717
///
1818
/// ** See code in examples/api/lib/widgets/animated_size/animated_size.0.dart **
1919
/// {@end-tool}

0 commit comments

Comments
 (0)