diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index fbcc5acf15..704e115f9d 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -257,6 +257,7 @@ class TextStyleTween extends Tween { /// [DefaultTextStyle]. /// * [AnimatedScale], which is an implicitly animated version of [Transform.scale]. /// * [AnimatedRotation], which is an implicitly animated version of [Transform.rotate]. +/// * [AnimatedSlide], which implicitly animates the position of a widget relative to its normal position. /// * [AnimatedOpacity], which is an implicitly animated version of [Opacity]. /// * [AnimatedPadding], which is an implicitly animated version of [Padding]. /// * [AnimatedPhysicalModel], which is an implicitly animated version of @@ -616,7 +617,7 @@ abstract class AnimatedWidgetBaseState exten /// transitions its child's position over a given duration whenever the given /// position changes. /// * [AnimatedAlign], which automatically transitions its child's -/// position over a given duration whenever the given [alignment] changes. +/// position over a given duration whenever the given [AnimatedAlign.alignment] changes. /// * [AnimatedSwitcher], which switches out a child for a new one with a customizable transition. /// * [AnimatedCrossFade], which fades between two children and interpolates their sizes. class AnimatedContainer extends ImplicitlyAnimatedWidget { @@ -973,6 +974,7 @@ class _AnimatedPaddingState extends AnimatedWidgetBaseState { /// * [AnimatedContainer], which can transition more values at once. /// * [AnimatedPadding], which can animate the padding instead of the /// alignment. +/// * [AnimatedSlide], which can animate the translation of child by a given offset relative to its size. /// * [AnimatedPositioned], which, as a child of a [Stack], automatically /// transitions its child's position over a given duration whenever the given /// position changes. @@ -1464,6 +1466,7 @@ class _AnimatedPositionedDirectionalState extends AnimatedWidgetBaseState offset -= const Offset(0, 1)); +/// } +/// +/// void _slideDown() { +/// setState(() => offset += const Offset(0, 1)); +/// } +/// +/// @override +/// Widget build(BuildContext context) { +/// return Column( +/// mainAxisSize: MainAxisSize.min, +/// children: [ +/// ElevatedButton( +/// child: const Text('Slide up'), +/// onPressed: _slideUp, +/// ), +/// ElevatedButton( +/// child: const Text('Slide down'), +/// onPressed: _slideDown, +/// ), +/// Padding( +/// padding: const EdgeInsets.all(50), +/// child: AnimatedSlide( +/// offset: offset, +/// duration: const Duration(milliseconds: 500), +/// curve: Curves.easeInOut, +/// child: const FlutterLogo(), +/// ), +/// ), +/// ], +/// ); +/// } +/// ``` +/// {@end-tool} +/// +/// See also: +/// +/// * [AnimatedPositioned], which, as a child of a [Stack], automatically +/// transitions its child's position over a given duration whenever the given +/// position changes. +/// * [AnimatedAlign], which automatically transitions its child's +/// position over a given duration whenever the given [AnimatedAlign.alignment] changes. +class AnimatedSlide extends ImplicitlyAnimatedWidget { + /// Creates a widget that animates its offset translation implicitly. + /// + /// The [offset] and [duration] arguments must not be null. + const AnimatedSlide({ + Key? key, + this.child, + required this.offset, + Curve curve = Curves.linear, + required Duration duration, + VoidCallback? onEnd, + }) : super(key: key, curve: curve, duration: duration, onEnd: onEnd); + + /// The widget below this widget in the tree. + /// + /// {@macro flutter.widgets.ProxyWidget.child} + final Widget? child; + + /// The target offset. + /// The child will be translated horizontally by `width * dx` and vertically by `height * dy` + /// + /// The offset must not be null. + final Offset offset; + + @override + ImplicitlyAnimatedWidgetState createState() => _AnimatedSlideState(); + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('offset', offset)); + } +} + +class _AnimatedSlideState extends ImplicitlyAnimatedWidgetState { + Tween? _offset; + late Animation _offsetAnimation; + + @override + void forEachTween(TweenVisitor visitor) { + _offset = visitor(_offset, widget.offset, (dynamic value) => Tween(begin: value as Offset)) as Tween?; + } + + @override + void didUpdateTweens() { + _offsetAnimation = animation.drive(_offset!); + } + + @override + Widget build(BuildContext context) { + return SlideTransition( + position: _offsetAnimation, + child: widget.child, + ); + } +} + /// Animated version of [Opacity] which automatically transitions the child's /// opacity over a given duration whenever the given opacity changes. /// diff --git a/packages/flutter/test/widgets/implicit_animations_test.dart b/packages/flutter/test/widgets/implicit_animations_test.dart index 3e6dc12828..8ef09dfa12 100644 --- a/packages/flutter/test/widgets/implicit_animations_test.dart +++ b/packages/flutter/test/widgets/implicit_animations_test.dart @@ -171,6 +171,64 @@ void main() { expect(mockOnEndFunction.called, 1); }); + testWidgets('AnimatedSlide onEnd callback test', (WidgetTester tester) async { + await tester.pumpWidget(wrap( + child: TestAnimatedWidget( + callback: mockOnEndFunction.handler, + switchKey: switchKey, + state: _TestAnimatedSlideWidgetState(), + ), + )); + + final Finder widgetFinder = find.byKey(switchKey); + + await tester.tap(widgetFinder); + await tester.pump(); + expect(mockOnEndFunction.called, 0); + await tester.pump(animationDuration); + expect(mockOnEndFunction.called, 0); + await tester.pump(additionalDelay); + expect(mockOnEndFunction.called, 1); + }); + + testWidgets('AnimatedSlide transition test', (WidgetTester tester) async { + await tester.pumpWidget(wrap( + child: TestAnimatedWidget( + switchKey: switchKey, + state: _TestAnimatedSlideWidgetState(), + ), + )); + + final RebuildCountingState state = tester.widget( + find.byType(TestAnimatedWidget) + ).rebuildState!; + final Finder switchFinder = find.byKey(switchKey); + final SlideTransition slideWidget = tester.widget( + find.ancestor( + of: find.byType(Placeholder), + matching: find.byType(SlideTransition), + ).first, + ); + + expect(state.builds, equals(1)); + + await tester.tap(switchFinder); + expect(state.builds, equals(1)); + await tester.pump(); + expect(slideWidget.position.value, equals(Offset.zero)); + expect(state.builds, equals(2)); + + await tester.pump(const Duration(milliseconds: 500)); + expect(slideWidget.position.value, equals(const Offset(0.5,0.5))); + expect(state.builds, equals(2)); + await tester.pump(const Duration(milliseconds: 250)); + expect(slideWidget.position.value, equals(const Offset(0.75,0.75))); + expect(state.builds, equals(2)); + await tester.pump(const Duration(milliseconds: 250)); + expect(slideWidget.position.value, equals(const Offset(1,1))); + expect(state.builds, equals(2)); + }); + testWidgets('AnimatedScale onEnd callback test', (WidgetTester tester) async { await tester.pumpWidget(wrap( child: TestAnimatedWidget( @@ -660,6 +718,18 @@ class _TestAnimatedPositionedDirectionalWidgetState extends _TestAnimatedWidgetS } } +class _TestAnimatedSlideWidgetState extends _TestAnimatedWidgetState { + @override + Widget getAnimatedWidget() { + return AnimatedSlide( + duration: duration, + onEnd: widget.callback, + offset: toggle ? const Offset(1,1) : Offset.zero, + child: child, + ); + } +} + class _TestAnimatedScaleWidgetState extends _TestAnimatedWidgetState { @override Widget getAnimatedWidget() {