o7planning

Flutter Tween Tutorial with Examples

  1. Tween
  2. Tween classes
  3. Tween constructor
  4. transform(t)
  5. lerp(t)

1. Tween

In Flutter, Tween class tries to simulate a "linear interpolation". So before we talk about it, we need to clarify the concept of interpolation and linear interpolation.
Interpolation is the process of estimating unknown data points that fall between known data points.
To simplify, look at the illustration below. The pink curve includes actual data points, but we only know a few actual data points (in red). The question is, how can we estimate other points?
Newton's linear interpolation method says that connect points with increasing X-coordinates to create a polylines (Blue) and you can estimate other data points.
According to the illustration above, P is a point on the meandering line (blue), estimated by the linear interpolation method, and P* is the actual data point. Obviously, there is a bit of an error.
Tweet<T>
Back to Tweet<T> class, it simulates a linear interpolation with 2 known data points (starting and ending points). In this case, the polylines is only a straight line.
const Offset(
    double dx,
    double dy
)
As usual we start with a simple example: A Tweet<T> with the parameter <T> is Offset type. In 0 to 1 time period, an object moves steadily in a straight line from position P0 (2.10) to position P1 (20.4). We can calculate the position of this object at any given time.
Tween<Offset> tween = Tween<Offset>(begin: Offset(2, 10), end: Offset(20,4));

var times = [0.0, 0.25, 0.5, 0.75, 1.0];

for(var t in times) {
    Offset point = tween.transform(t);
    print("t = " + t.toString() +". x/y = " + point.dx.toString() +"/" + point.dy.toString());
}
Output:
I/flutter (22119): t = 0.0. x/y = 2.0/10.0
I/flutter (22119): t = 0.25. x/y = 6.5/8.5
I/flutter (22119): t = 0.5. x/y = 11.0/7.0
I/flutter (22119): t = 0.75. x/y = 15.5/5.5
I/flutter (22119): t = 1.0. x/y = 20.0/4.0
Thus, just providing 2 data points, Tweet<T> will estimate a lot of other data points, they can be used as different states in an animation process.

2. Tween classes

Hierarchy of classes:
List of descendants of Tween class:
  • AlignmentGeometryTween
  • AlignmentTween
  • BorderRadiusTween
  • BorderTween
  • BoxConstraintsTween
  • ColorTween
  • ConstantTween
  • DecorationTween
  • EdgeInsetsGeometryTween
  • EdgeInsetsTween
  • FractionalOffsetTween
  • IntTween
  • MaterialPointArcTween
  • Matrix4Tween
  • RectTween
    1. MaterialRectArcTween
    2. MaterialRectCenterArcTween
  • RelativeRectTween
  • ReverseTween
  • ShapeBorderTween
  • SizeTween
  • StepTween
  • TextStyleTween
  • ThemeDataTween
Tweet<T> class has quite a few subclasses, some of which are created for specific types of <T> parameter. Example: AlignmentGeometryTween class extends from Tween<AlignmentGeometry>, which is a linear interpolation between 2 AlignmentGeomery(s).

3. Tween constructor

Tween<T extends dynamic>(
  {T? begin,
  T? end}
)
TODO

4. transform(t)

TODO

5. lerp(t)

TODO

Flutter Programming Tutorials

Show More