From 7436cc25eb93776b16a04668204336f631242bfb Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 2 May 2024 15:59:07 -0600 Subject: [PATCH] Add default arguments to `AnimatedPhysicalModel` (#147424) Currently, `PhysicalModel` has [default arguments](https://github.com/flutter/flutter/blob/2e806700b9287c4b4f9fc49fbdfbd81089fc65dd/packages/flutter/lib/src/widgets/basic.dart#L1093) for `shape` and `elevation`, but `AnimatedPhysicalModel` [does not](https://github.com/flutter/flutter/blob/2e806700b9287c4b4f9fc49fbdfbd81089fc65dd/packages/flutter/lib/src/widgets/implicit_animations.dart#L1998). This pull request makes both classes consistent. --- .../flutter/lib/src/material/material.dart | 1 - .../lib/src/widgets/implicit_animations.dart | 34 ++++++++++++++----- .../widgets/implicit_animations_test.dart | 25 ++++++++++++-- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/material/material.dart b/packages/flutter/lib/src/material/material.dart index 891c2625be..7cdb162550 100644 --- a/packages/flutter/lib/src/material/material.dart +++ b/packages/flutter/lib/src/material/material.dart @@ -503,7 +503,6 @@ class _MaterialState extends State with TickerProviderStateMixin { return AnimatedPhysicalModel( curve: Curves.fastOutSlowIn, duration: widget.animationDuration, - shape: BoxShape.rectangle, clipBehavior: widget.clipBehavior, elevation: modelElevation, color: color, diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index 20fec44e0b..ce60af7e6a 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -1995,10 +1995,10 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget { const AnimatedPhysicalModel({ super.key, required this.child, - required this.shape, + this.shape = BoxShape.rectangle, this.clipBehavior = Clip.none, - this.borderRadius = BorderRadius.zero, - required this.elevation, + this.borderRadius, + this.elevation = 0.0, required this.color, this.animateColor = true, required this.shadowColor, @@ -2024,7 +2024,9 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget { final Clip clipBehavior; /// The target border radius of the rounded corners for a rectangle shape. - final BorderRadius borderRadius; + /// + /// If null, treated as [BorderRadius.zero]. + final BorderRadius? borderRadius; /// The target z-coordinate relative to the parent at which to place this /// physical object. @@ -2068,10 +2070,26 @@ class _AnimatedPhysicalModelState extends AnimatedWidgetBaseState visitor) { - _borderRadius = visitor(_borderRadius, widget.borderRadius, (dynamic value) => BorderRadiusTween(begin: value as BorderRadius)) as BorderRadiusTween?; - _elevation = visitor(_elevation, widget.elevation, (dynamic value) => Tween(begin: value as double)) as Tween?; - _color = visitor(_color, widget.color, (dynamic value) => ColorTween(begin: value as Color)) as ColorTween?; - _shadowColor = visitor(_shadowColor, widget.shadowColor, (dynamic value) => ColorTween(begin: value as Color)) as ColorTween?; + _borderRadius = visitor( + _borderRadius, + widget.borderRadius ?? BorderRadius.zero, + (dynamic value) => BorderRadiusTween(begin: value as BorderRadius), + ) as BorderRadiusTween?; + _elevation = visitor( + _elevation, + widget.elevation, + (dynamic value) => Tween(begin: value as double), + ) as Tween?; + _color = visitor( + _color, + widget.color, + (dynamic value) => ColorTween(begin: value as Color), + ) as ColorTween?; + _shadowColor = visitor( + _shadowColor, + widget.shadowColor, + (dynamic value) => ColorTween(begin: value as Color), + ) as ColorTween?; } @override diff --git a/packages/flutter/test/widgets/implicit_animations_test.dart b/packages/flutter/test/widgets/implicit_animations_test.dart index 2815289802..fd68d66361 100644 --- a/packages/flutter/test/widgets/implicit_animations_test.dart +++ b/packages/flutter/test/widgets/implicit_animations_test.dart @@ -649,6 +649,29 @@ void main() { expect(secondCurvedAnimation.isDisposed, isTrue); }); + + group('Verify that default args match non-animated variants', () { + const Widget child = SizedBox.shrink(); + const Color color = Color(0x00000000); + + testWidgets('PhysicalModel default args', (WidgetTester tester) async { + const AnimatedPhysicalModel animatedPhysicalModel = AnimatedPhysicalModel( + duration: Duration.zero, + color: color, + shadowColor: color, + child: child, + ); + const PhysicalModel physicalModel = PhysicalModel( + color: color, + shadowColor: color, + child: child, + ); + expect(identical(animatedPhysicalModel.shape, physicalModel.shape), isTrue); + expect(identical(animatedPhysicalModel.clipBehavior, physicalModel.clipBehavior), isTrue); + expect(identical(animatedPhysicalModel.borderRadius, physicalModel.borderRadius), isTrue); + }); + // TODO(nate-thegrate): add every class! + }); } Future tapTest2and3(WidgetTester tester, Finder widgetFinder, @@ -904,9 +927,7 @@ class _TestAnimatedPhysicalModelWidgetState extends _TestAnimatedWidgetState { duration: duration, onEnd: widget.callback, color: toggle ? Colors.red : Colors.green, - elevation: 0, shadowColor: Colors.blue, - shape: BoxShape.rectangle, child: child, ); }