From 65e8c615b49235022ab105c4f900fda8f44c4365 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Fri, 10 May 2024 23:55:51 +0200 Subject: [PATCH] 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` --- dev/bots/check_code_samples.dart | 1 - .../animated_size/animated_size.0.dart | 45 +++++++---- .../animated_size/animated_size.0_test.dart | 76 +++++++++++++++++++ .../lib/src/widgets/animated_size.dart | 4 +- 4 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 examples/api/test/widgets/animated_size/animated_size.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 34ed173606..d0fffb7430 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -406,7 +406,6 @@ final Set _knownMissingTests = { '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/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/transitions/relative_positioned_transition.0_test.dart', 'examples/api/test/widgets/transitions/positioned_transition.0_test.dart', diff --git a/examples/api/lib/widgets/animated_size/animated_size.0.dart b/examples/api/lib/widgets/animated_size/animated_size.0.dart index 76f53b359a..47ea4653d0 100644 --- a/examples/api/lib/widgets/animated_size/animated_size.0.dart +++ b/examples/api/lib/widgets/animated_size/animated_size.0.dart @@ -11,13 +11,19 @@ void main() => runApp(const AnimatedSizeExampleApp()); class AnimatedSizeExampleApp extends StatelessWidget { const AnimatedSizeExampleApp({super.key}); + static const Duration duration = Duration(seconds: 1); + static const Curve curve = Curves.easeIn; + @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('AnimatedSize Sample')), body: const Center( - child: AnimatedSizeExample(), + child: AnimatedSizeExample( + duration: duration, + curve: curve, + ), ), ), ); @@ -25,33 +31,42 @@ class AnimatedSizeExampleApp extends StatelessWidget { } 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 State createState() => _AnimatedSizeExampleState(); } class _AnimatedSizeExampleState extends State { - double _size = 50.0; - bool _large = false; - - void _updateSize() { - setState(() { - _size = _large ? 250.0 : 100.0; - _large = !_large; - }); - } + bool _isSelected = false; @override Widget build(BuildContext context) { return GestureDetector( - onTap: () => _updateSize(), + onTap: () { + setState(() { + _isSelected = !_isSelected; + }); + }, child: ColoredBox( color: Colors.amberAccent, child: AnimatedSize( - curve: Curves.easeIn, - duration: const Duration(seconds: 1), - child: FlutterLogo(size: _size), + duration: widget.duration, + curve: widget.curve, + child: SizedBox.square( + dimension: _isSelected ? 250.0 : 100.0, + child: const Center( + child: FlutterLogo(size: 75.0), + ), + ), ), ), ); diff --git a/examples/api/test/widgets/animated_size/animated_size.0_test.dart b/examples/api/test/widgets/animated_size/animated_size.0_test.dart new file mode 100644 index 0000000000..822449cc10 --- /dev/null +++ b/examples/api/test/widgets/animated_size/animated_size.0_test.dart @@ -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)); + }); +} diff --git a/packages/flutter/lib/src/widgets/animated_size.dart b/packages/flutter/lib/src/widgets/animated_size.dart index 110ec3ca1a..8943c6af4d 100644 --- a/packages/flutter/lib/src/widgets/animated_size.dart +++ b/packages/flutter/lib/src/widgets/animated_size.dart @@ -12,8 +12,8 @@ import 'ticker_provider.dart'; /// duration whenever the given child's size changes. /// /// {@tool dartpad} -/// This example makes a [Container] react to being touched, causing the child -/// of the [AnimatedSize] widget, here a [FlutterLogo], to animate. +/// This example defines a widget that uses [AnimatedSize] to change the size of +/// the [SizedBox] on tap. /// /// ** See code in examples/api/lib/widgets/animated_size/animated_size.0.dart ** /// {@end-tool}