Add test for animated_size.0.dart API example. (#147828)

This PR contributes to https://github.com/flutter/flutter/issues/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`
This commit is contained in:
Kostia Sokolovskyi 2024-05-10 23:55:51 +02:00 committed by GitHub
parent fdfc408fd7
commit 65e8c615b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 108 additions and 18 deletions

View File

@ -406,7 +406,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/media_query/media_query_data.system_gesture_insets.0_test.dart', 'examples/api/test/widgets/media_query/media_query_data.system_gesture_insets.0_test.dart',
'examples/api/test/widgets/async/future_builder.0_test.dart', 'examples/api/test/widgets/async/future_builder.0_test.dart',
'examples/api/test/widgets/restoration_properties/restorable_value.0_test.dart', 'examples/api/test/widgets/restoration_properties/restorable_value.0_test.dart',
'examples/api/test/widgets/animated_size/animated_size.0_test.dart',
'examples/api/test/widgets/animated_switcher/animated_switcher.0_test.dart', 'examples/api/test/widgets/animated_switcher/animated_switcher.0_test.dart',
'examples/api/test/widgets/transitions/relative_positioned_transition.0_test.dart', 'examples/api/test/widgets/transitions/relative_positioned_transition.0_test.dart',
'examples/api/test/widgets/transitions/positioned_transition.0_test.dart', 'examples/api/test/widgets/transitions/positioned_transition.0_test.dart',

View File

@ -11,13 +11,19 @@ void main() => runApp(const AnimatedSizeExampleApp());
class AnimatedSizeExampleApp extends StatelessWidget { class AnimatedSizeExampleApp extends StatelessWidget {
const AnimatedSizeExampleApp({super.key}); const AnimatedSizeExampleApp({super.key});
static const Duration duration = Duration(seconds: 1);
static const Curve curve = Curves.easeIn;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
appBar: AppBar(title: const Text('AnimatedSize Sample')), appBar: AppBar(title: const Text('AnimatedSize Sample')),
body: const Center( body: const Center(
child: AnimatedSizeExample(), child: AnimatedSizeExample(
duration: duration,
curve: curve,
),
), ),
), ),
); );
@ -25,33 +31,42 @@ class AnimatedSizeExampleApp extends StatelessWidget {
} }
class AnimatedSizeExample extends StatefulWidget { class AnimatedSizeExample extends StatefulWidget {
const AnimatedSizeExample({super.key}); const AnimatedSizeExample({
required this.duration,
required this.curve,
super.key,
});
final Duration duration;
final Curve curve;
@override @override
State<AnimatedSizeExample> createState() => _AnimatedSizeExampleState(); State<AnimatedSizeExample> createState() => _AnimatedSizeExampleState();
} }
class _AnimatedSizeExampleState extends State<AnimatedSizeExample> { class _AnimatedSizeExampleState extends State<AnimatedSizeExample> {
double _size = 50.0; bool _isSelected = false;
bool _large = false;
void _updateSize() {
setState(() {
_size = _large ? 250.0 : 100.0;
_large = !_large;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
onTap: () => _updateSize(), onTap: () {
setState(() {
_isSelected = !_isSelected;
});
},
child: ColoredBox( child: ColoredBox(
color: Colors.amberAccent, color: Colors.amberAccent,
child: AnimatedSize( child: AnimatedSize(
curve: Curves.easeIn, duration: widget.duration,
duration: const Duration(seconds: 1), curve: widget.curve,
child: FlutterLogo(size: _size), child: SizedBox.square(
dimension: _isSelected ? 250.0 : 100.0,
child: const Center(
child: FlutterLogo(size: 75.0),
),
),
), ),
), ),
); );

View File

@ -0,0 +1,76 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/animated_size/animated_size.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('AnimatedSize animates on tap', (WidgetTester tester) async {
await tester.pumpWidget(
const example.AnimatedSizeExampleApp(),
);
const Size beginSize = Size.square(100.0);
const Size endSize = Size.square(250.0);
RenderBox box = tester.renderObject(find.byType(AnimatedSize));
expect(box.size, equals(beginSize));
// Tap on the AnimatedSize to start the forward animation.
await tester.tap(find.byType(AnimatedSize));
await tester.pump();
box = tester.renderObject(find.byType(AnimatedSize));
expect(box.size, equals(beginSize));
// Advance animation to the middle.
await tester.pump(
example.AnimatedSizeExampleApp.duration ~/ 2,
);
final double t = example.AnimatedSizeExampleApp.curve.transform(0.5);
box = tester.renderObject(find.byType(AnimatedSize));
expect(
box.size,
equals(Size.lerp(beginSize, endSize, t)),
);
// Advance animation to the end.
await tester.pump(
example.AnimatedSizeExampleApp.duration ~/ 2,
);
box = tester.renderObject(find.byType(AnimatedSize));
expect(box.size, equals(endSize));
// Tap on the AnimatedSize again to start the reverse animation.
await tester.tap(find.byType(AnimatedSize));
await tester.pump();
box = tester.renderObject(find.byType(AnimatedSize));
expect(box.size, equals(endSize));
// Advance animation to the middle.
await tester.pump(
example.AnimatedSizeExampleApp.duration ~/ 2,
);
box = tester.renderObject(find.byType(AnimatedSize));
expect(
box.size,
equals(Size.lerp(endSize, beginSize, t)),
);
// Advance animation to the end.
await tester.pump(
example.AnimatedSizeExampleApp.duration ~/ 2,
);
box = tester.renderObject(find.byType(AnimatedSize));
expect(box.size, equals(beginSize));
});
}

View File

@ -12,8 +12,8 @@ import 'ticker_provider.dart';
/// duration whenever the given child's size changes. /// duration whenever the given child's size changes.
/// ///
/// {@tool dartpad} /// {@tool dartpad}
/// This example makes a [Container] react to being touched, causing the child /// This example defines a widget that uses [AnimatedSize] to change the size of
/// of the [AnimatedSize] widget, here a [FlutterLogo], to animate. /// the [SizedBox] on tap.
/// ///
/// ** See code in examples/api/lib/widgets/animated_size/animated_size.0.dart ** /// ** See code in examples/api/lib/widgets/animated_size/animated_size.0.dart **
/// {@end-tool} /// {@end-tool}