Skip to content

Commit 7436cc2

Browse files
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.
1 parent 9d00793 commit 7436cc2

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

packages/flutter/lib/src/material/material.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
503503
return AnimatedPhysicalModel(
504504
curve: Curves.fastOutSlowIn,
505505
duration: widget.animationDuration,
506-
shape: BoxShape.rectangle,
507506
clipBehavior: widget.clipBehavior,
508507
elevation: modelElevation,
509508
color: color,

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,10 +1995,10 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget {
19951995
const AnimatedPhysicalModel({
19961996
super.key,
19971997
required this.child,
1998-
required this.shape,
1998+
this.shape = BoxShape.rectangle,
19991999
this.clipBehavior = Clip.none,
2000-
this.borderRadius = BorderRadius.zero,
2001-
required this.elevation,
2000+
this.borderRadius,
2001+
this.elevation = 0.0,
20022002
required this.color,
20032003
this.animateColor = true,
20042004
required this.shadowColor,
@@ -2024,7 +2024,9 @@ class AnimatedPhysicalModel extends ImplicitlyAnimatedWidget {
20242024
final Clip clipBehavior;
20252025

20262026
/// The target border radius of the rounded corners for a rectangle shape.
2027-
final BorderRadius borderRadius;
2027+
///
2028+
/// If null, treated as [BorderRadius.zero].
2029+
final BorderRadius? borderRadius;
20282030

20292031
/// The target z-coordinate relative to the parent at which to place this
20302032
/// physical object.
@@ -2068,10 +2070,26 @@ class _AnimatedPhysicalModelState extends AnimatedWidgetBaseState<AnimatedPhysic
20682070

20692071
@override
20702072
void forEachTween(TweenVisitor<dynamic> visitor) {
2071-
_borderRadius = visitor(_borderRadius, widget.borderRadius, (dynamic value) => BorderRadiusTween(begin: value as BorderRadius)) as BorderRadiusTween?;
2072-
_elevation = visitor(_elevation, widget.elevation, (dynamic value) => Tween<double>(begin: value as double)) as Tween<double>?;
2073-
_color = visitor(_color, widget.color, (dynamic value) => ColorTween(begin: value as Color)) as ColorTween?;
2074-
_shadowColor = visitor(_shadowColor, widget.shadowColor, (dynamic value) => ColorTween(begin: value as Color)) as ColorTween?;
2073+
_borderRadius = visitor(
2074+
_borderRadius,
2075+
widget.borderRadius ?? BorderRadius.zero,
2076+
(dynamic value) => BorderRadiusTween(begin: value as BorderRadius),
2077+
) as BorderRadiusTween?;
2078+
_elevation = visitor(
2079+
_elevation,
2080+
widget.elevation,
2081+
(dynamic value) => Tween<double>(begin: value as double),
2082+
) as Tween<double>?;
2083+
_color = visitor(
2084+
_color,
2085+
widget.color,
2086+
(dynamic value) => ColorTween(begin: value as Color),
2087+
) as ColorTween?;
2088+
_shadowColor = visitor(
2089+
_shadowColor,
2090+
widget.shadowColor,
2091+
(dynamic value) => ColorTween(begin: value as Color),
2092+
) as ColorTween?;
20752093
}
20762094

20772095
@override

packages/flutter/test/widgets/implicit_animations_test.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,29 @@ void main() {
649649

650650
expect(secondCurvedAnimation.isDisposed, isTrue);
651651
});
652+
653+
group('Verify that default args match non-animated variants', () {
654+
const Widget child = SizedBox.shrink();
655+
const Color color = Color(0x00000000);
656+
657+
testWidgets('PhysicalModel default args', (WidgetTester tester) async {
658+
const AnimatedPhysicalModel animatedPhysicalModel = AnimatedPhysicalModel(
659+
duration: Duration.zero,
660+
color: color,
661+
shadowColor: color,
662+
child: child,
663+
);
664+
const PhysicalModel physicalModel = PhysicalModel(
665+
color: color,
666+
shadowColor: color,
667+
child: child,
668+
);
669+
expect(identical(animatedPhysicalModel.shape, physicalModel.shape), isTrue);
670+
expect(identical(animatedPhysicalModel.clipBehavior, physicalModel.clipBehavior), isTrue);
671+
expect(identical(animatedPhysicalModel.borderRadius, physicalModel.borderRadius), isTrue);
672+
});
673+
// TODO(nate-thegrate): add every class!
674+
});
652675
}
653676

654677
Future<void> tapTest2and3(WidgetTester tester, Finder widgetFinder,
@@ -904,9 +927,7 @@ class _TestAnimatedPhysicalModelWidgetState extends _TestAnimatedWidgetState {
904927
duration: duration,
905928
onEnd: widget.callback,
906929
color: toggle ? Colors.red : Colors.green,
907-
elevation: 0,
908930
shadowColor: Colors.blue,
909-
shape: BoxShape.rectangle,
910931
child: child,
911932
);
912933
}

0 commit comments

Comments
 (0)