diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 9db394dfd2..3309b5c2be 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -280,37 +280,6 @@ class _BorderContainerState extends State<_BorderContainer> with TickerProviderS } } -// Used to "shake" the floating label to the left and right -// when the errorText first appears. -class _Shaker extends AnimatedWidget { - const _Shaker({ - required Animation animation, - this.child, - }) : super(listenable: animation); - - final Widget? child; - - Animation get animation => listenable as Animation; - - double get translateX { - const double shakeDelta = 4.0; - final double t = animation.value; - return shakeDelta * switch (t) { - <= 0.25 => -t, - < 0.75 => t - 0.5, - _ => (1.0 - t) * 4.0, - }; - } - - @override - Widget build(BuildContext context) { - return Transform( - transform: Matrix4.translationValues(translateX, 0.0, 0.0), - child: child, - ); - } -} - // Display the helper and error text. When the error text appears // it fades and the helper text fades out. The error text also // slides upwards a little when it first appears. @@ -2221,26 +2190,39 @@ class _InputDecoratorState extends State with TickerProviderStat isHovering: isHovering, ); - final Widget? label = decoration.labelText == null && decoration.label == null ? null : _Shaker( - animation: _shakingLabelController.view, - child: AnimatedOpacity( - duration: _kTransitionDuration, - curve: _kTransitionCurve, - opacity: _shouldShowLabel ? 1.0 : 0.0, - child: AnimatedDefaultTextStyle( - duration:_kTransitionDuration, + Widget? label; + if ((decoration.labelText ?? decoration.label) != null) { + label = MatrixTransition( + animation: _shakingLabelController, + onTransform: (double value) { + final double shakeOffset = switch (value) { + <= 0.25 => -value, + < 0.75 => value - 0.5, + _ => (1.0 - value) * 4.0, + }; + // Shakes the floating label to the left and right + // when the errorText first appears. + return Matrix4.translationValues(shakeOffset * 4.0, 0.0, 0.0); + }, + child: AnimatedOpacity( + duration: _kTransitionDuration, curve: _kTransitionCurve, - style: widget._labelShouldWithdraw - ? _getFloatingLabelStyle(themeData, defaults) - : labelStyle, - child: decoration.label ?? Text( - decoration.labelText!, - overflow: TextOverflow.ellipsis, - textAlign: textAlign, + opacity: _shouldShowLabel ? 1.0 : 0.0, + child: AnimatedDefaultTextStyle( + duration: _kTransitionDuration, + curve: _kTransitionCurve, + style: widget._labelShouldWithdraw + ? _getFloatingLabelStyle(themeData, defaults) + : labelStyle, + child: decoration.label ?? Text( + decoration.labelText!, + overflow: TextOverflow.ellipsis, + textAlign: textAlign, + ), ), ), - ), - ); + ); + } final bool hasPrefix = decoration.prefix != null || decoration.prefixText != null; final bool hasSuffix = decoration.suffix != null || decoration.suffixText != null; diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index cfeb0aa01f..b16089a2e2 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -113,7 +113,7 @@ double getBorderBottom(WidgetTester tester) { Finder findLabel() { return find.descendant( - of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_Shaker'), + of: find.byType(MatrixTransition), matching: find.byWidgetPredicate((Widget w) => w is Text), ); }