Add implicit animations to Input

Now the label animates from its inline position to above the text and
the focus highlight grows into place.
This commit is contained in:
Adam Barth
2016-01-23 12:22:15 -08:00
parent 61f82ee18c
commit 7faee3e18e
2 changed files with 72 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/animation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
@@ -73,6 +74,9 @@ class Input extends StatefulComponent {
_InputState createState() => new _InputState();
}
const Duration _kTransitionDuration = const Duration(milliseconds: 200);
const Curve _kTransitionCurve = Curves.ease;
class _InputState extends State<Input> {
String _value;
EditableString _editableString;
@@ -139,21 +143,34 @@ class _InputState extends State<Input> {
List<Widget> stackChildren = <Widget>[];
bool hasInlineLabel = config.labelText != null && !focused && !_value.isNotEmpty;
if (config.labelText != null) {
TextStyle labelStyle = themeData.text.caption.copyWith(color: focused ? focusHighlightColor : themeData.hintColor);
stackChildren.add(new Positioned(
TextStyle labelStyle = hasInlineLabel ?
themeData.text.subhead.copyWith(color: themeData.hintColor) :
themeData.text.caption.copyWith(color: focused ? focusHighlightColor : themeData.hintColor);
double topPaddingIncrement = themeData.text.caption.fontSize + (config.isDense ? 4.0 : 8.0);
double top = topPadding;
if (hasInlineLabel)
top += topPaddingIncrement + textStyle.fontSize - labelStyle.fontSize;
stackChildren.add(new AnimatedPositioned(
left: 0.0,
top: topPadding,
top: top,
duration: _kTransitionDuration,
curve: _kTransitionCurve,
child: new Text(config.labelText, style: labelStyle)
));
topPadding += labelStyle.fontSize + (config.isDense ? 4.0 : 8.0);
topPadding += topPaddingIncrement;
}
if (config.hintText != null && _value.isEmpty) {
TextStyle hintStyle = textStyle.copyWith(color: themeData.hintColor);
if (config.hintText != null && _value.isEmpty && !hasInlineLabel) {
TextStyle hintStyle = themeData.text.subhead.copyWith(color: themeData.hintColor);
stackChildren.add(new Positioned(
left: 0.0,
top: topPadding,
top: topPadding + textStyle.fontSize - hintStyle.fontSize,
child: new Text(config.hintText, style: hintStyle)
));
}
@@ -176,9 +193,11 @@ class _InputState extends State<Input> {
}
}
stackChildren.add(new Container(
stackChildren.add(new AnimatedContainer(
margin: margin,
padding: padding,
duration: _kTransitionDuration,
curve: _kTransitionCurve,
decoration: new BoxDecoration(
border: new Border(
bottom: new BorderSide(

View File

@@ -29,6 +29,25 @@ class BorderSide {
/// A black border side of zero width.
static const none = const BorderSide(width: 0.0);
BorderSide copyWith({
Color color,
double width
}) {
return new BorderSide(
color: color ?? this.color,
width: width ?? this.width
);
}
static BorderSide lerp(BorderSide a, BorderSide b, double t) {
assert(a != null);
assert(b != null);
return new BorderSide(
color: Color.lerp(a.color, b.color, t),
width: ui.lerpDouble(a.width, b.width, t)
);
}
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
@@ -79,6 +98,30 @@ class Border {
return new EdgeDims.TRBL(top.width, right.width, bottom.width, left.width);
}
Border scale(double t) {
return new Border(
top: top.copyWith(width: t * top.width),
right: right.copyWith(width: t * right.width),
bottom: bottom.copyWith(width: t * bottom.width),
left: left.copyWith(width: t * left.width)
);
}
static Border lerp(Border a, Border b, double t) {
if (a == null && b == null)
return null;
if (a == null)
return b.scale(t);
if (b == null)
return a.scale(1.0 - t);
return new Border(
top: BorderSide.lerp(a.top, b.top, t),
right: BorderSide.lerp(a.right, b.right, t),
bottom: BorderSide.lerp(a.bottom, b.bottom, t),
left: BorderSide.lerp(a.left, b.left, t)
);
}
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
@@ -718,7 +761,7 @@ class BoxDecoration extends Decoration {
return new BoxDecoration(
backgroundColor: Color.lerp(null, backgroundColor, factor),
backgroundImage: backgroundImage,
border: border,
border: Border.lerp(null, border, factor),
borderRadius: ui.lerpDouble(null, borderRadius, factor),
boxShadow: BoxShadow.lerpList(null, boxShadow, factor),
gradient: gradient,
@@ -740,7 +783,7 @@ class BoxDecoration extends Decoration {
return new BoxDecoration(
backgroundColor: Color.lerp(a.backgroundColor, b.backgroundColor, t),
backgroundImage: b.backgroundImage,
border: b.border,
border: Border.lerp(a.border, b.border, t),
borderRadius: ui.lerpDouble(a.borderRadius, b.borderRadius, t),
boxShadow: BoxShadow.lerpList(a.boxShadow, b.boxShadow, t),
gradient: b.gradient,