Skip to content

Commit b46fb32

Browse files
Factor out "shaker" class (#157748)
I saw that there was a private `_Shaker` class in input_decorator.dart that does the exact same thing as the [MatrixTransition](https://main-api.flutter.dev/flutter/widgets/MatrixTransition-class.html) API. ("hide whitespace" for tiny diffs!)
1 parent 255a941 commit b46fb32

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

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

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -280,37 +280,6 @@ class _BorderContainerState extends State<_BorderContainer> with TickerProviderS
280280
}
281281
}
282282

283-
// Used to "shake" the floating label to the left and right
284-
// when the errorText first appears.
285-
class _Shaker extends AnimatedWidget {
286-
const _Shaker({
287-
required Animation<double> animation,
288-
this.child,
289-
}) : super(listenable: animation);
290-
291-
final Widget? child;
292-
293-
Animation<double> get animation => listenable as Animation<double>;
294-
295-
double get translateX {
296-
const double shakeDelta = 4.0;
297-
final double t = animation.value;
298-
return shakeDelta * switch (t) {
299-
<= 0.25 => -t,
300-
< 0.75 => t - 0.5,
301-
_ => (1.0 - t) * 4.0,
302-
};
303-
}
304-
305-
@override
306-
Widget build(BuildContext context) {
307-
return Transform(
308-
transform: Matrix4.translationValues(translateX, 0.0, 0.0),
309-
child: child,
310-
);
311-
}
312-
}
313-
314283
// Display the helper and error text. When the error text appears
315284
// it fades and the helper text fades out. The error text also
316285
// slides upwards a little when it first appears.
@@ -2221,26 +2190,39 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
22212190
isHovering: isHovering,
22222191
);
22232192

2224-
final Widget? label = decoration.labelText == null && decoration.label == null ? null : _Shaker(
2225-
animation: _shakingLabelController.view,
2226-
child: AnimatedOpacity(
2227-
duration: _kTransitionDuration,
2228-
curve: _kTransitionCurve,
2229-
opacity: _shouldShowLabel ? 1.0 : 0.0,
2230-
child: AnimatedDefaultTextStyle(
2231-
duration:_kTransitionDuration,
2193+
Widget? label;
2194+
if ((decoration.labelText ?? decoration.label) != null) {
2195+
label = MatrixTransition(
2196+
animation: _shakingLabelController,
2197+
onTransform: (double value) {
2198+
final double shakeOffset = switch (value) {
2199+
<= 0.25 => -value,
2200+
< 0.75 => value - 0.5,
2201+
_ => (1.0 - value) * 4.0,
2202+
};
2203+
// Shakes the floating label to the left and right
2204+
// when the errorText first appears.
2205+
return Matrix4.translationValues(shakeOffset * 4.0, 0.0, 0.0);
2206+
},
2207+
child: AnimatedOpacity(
2208+
duration: _kTransitionDuration,
22322209
curve: _kTransitionCurve,
2233-
style: widget._labelShouldWithdraw
2234-
? _getFloatingLabelStyle(themeData, defaults)
2235-
: labelStyle,
2236-
child: decoration.label ?? Text(
2237-
decoration.labelText!,
2238-
overflow: TextOverflow.ellipsis,
2239-
textAlign: textAlign,
2210+
opacity: _shouldShowLabel ? 1.0 : 0.0,
2211+
child: AnimatedDefaultTextStyle(
2212+
duration: _kTransitionDuration,
2213+
curve: _kTransitionCurve,
2214+
style: widget._labelShouldWithdraw
2215+
? _getFloatingLabelStyle(themeData, defaults)
2216+
: labelStyle,
2217+
child: decoration.label ?? Text(
2218+
decoration.labelText!,
2219+
overflow: TextOverflow.ellipsis,
2220+
textAlign: textAlign,
2221+
),
22402222
),
22412223
),
2242-
),
2243-
);
2224+
);
2225+
}
22442226

22452227
final bool hasPrefix = decoration.prefix != null || decoration.prefixText != null;
22462228
final bool hasSuffix = decoration.suffix != null || decoration.suffixText != null;

packages/flutter/test/material/input_decorator_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ double getBorderBottom(WidgetTester tester) {
113113

114114
Finder findLabel() {
115115
return find.descendant(
116-
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_Shaker'),
116+
of: find.byType(MatrixTransition),
117117
matching: find.byWidgetPredicate((Widget w) => w is Text),
118118
);
119119
}

0 commit comments

Comments
 (0)