Curve2D class Null safety
Abstract class that defines an API for evaluating 2D parametric curves.
Curve2D differs from Curve in that the values interpolated are Offset
values instead of double values, hence the "2D" in the name. They both
take a single double t
that has a range of 0.0 to 1.0, inclusive, as input
to the transform function . Unlike Curve, Curve2D is not required to
map t=0.0
and t=1.0
to specific output values.
The interpolated t
value given to transform represents the progression
along the curve, but it doesn't necessarily progress at a constant velocity, so
incrementing t
by, say, 0.1 might move along the curve by quite a lot at one
part of the curve, or hardly at all in another part of the curve, depending
on the definition of the curve.
This example shows how to use a Curve2D to modify the position of a widget
so that it can follow an arbitrary path.
To create a local project with this code sample, run:
flutter create --sample=animation.Curve2D.1 mysample
flutter create --sample=animation.Curve2D.1 mysample
This example shows how to use a Curve2D to modify the position of a widget
so that it can follow an arbitrary path.
// This is the path that the child will follow. It's a CatmullRomSpline so
// that the coordinates can be specified that it must pass through. If the
// tension is set to 1.0, it will linearly interpolate between those points,
// instead of interpolating smoothly.
final CatmullRomSpline path = CatmullRomSpline(
const <Offset>[
Offset(0.05, 0.75),
Offset(0.18, 0.23),
Offset(0.32, 0.04),
Offset(0.73, 0.5),
Offset(0.42, 0.74),
Offset(0.73, 0.01),
Offset(0.93, 0.93),
Offset(0.05, 0.75),
],
startHandle: Offset(0.93, 0.93),
endHandle: Offset(0.18, 0.23),
tension: 0.0,
);
class FollowCurve2D extends StatefulWidget {
const FollowCurve2D({
Key key,
@required this.path,
this.curve = Curves.easeInOut,
@required this.child,
this.duration = const Duration(seconds: 1),
}) : assert(path != null),
assert(curve != null),
assert(child != null),
assert(duration != null),
super(key: key);
final Curve2D path;
final Curve curve;
final Duration duration;
final Widget child;
@override
_FollowCurve2DState createState() => _FollowCurve2DState();
}
class _FollowCurve2DState extends State<FollowCurve2D> with TickerProviderStateMixin {
// The animation controller for this animation.
AnimationController controller;
// The animation that will be used to apply the widget's animation curve.
Animation<double> animation;
@override
void initState() {
super.initState();
controller = AnimationController(duration: widget.duration, vsync: this);
animation = CurvedAnimation(parent: controller, curve: widget.curve);
// Have the controller repeat indefinitely. If you want it to "bounce" back
// and forth, set the reverse parameter to true.
controller.repeat(reverse: false);
controller.addListener(() => setState(() {}));
}
@override
void dispose() {
super.dispose();
// Always have to dispose of animation controllers when done.
controller.dispose();
}
@override
Widget build(BuildContext context) {
// Scale the path values to match the -1.0 to 1.0 domain of the Alignment widget.
final Offset position = widget.path.transform(animation.value) * 2.0 - Offset(1.0, 1.0);
return Align(
alignment: Alignment(position.dx, position.dy),
child: widget.child,
);
}
}
// ...
Widget build(BuildContext context) {
return Container(
color: Colors.white,
alignment: Alignment.center,
child: FollowCurve2D(
path: path,
curve: Curves.easeInOut,
duration: const Duration(seconds: 3),
child: CircleAvatar(
backgroundColor: Colors.yellow,
child: DefaultTextStyle(
style: Theme.of(context).textTheme.headline6,
child: Text("B"), // Buzz, buzz!
),
),
),
);
}
- Inheritance
- Object
- ParametricCurve<
Offset> - Curve2D
- Implementers
Constructors
- Curve2D()
-
Abstract const constructor to enable subclasses to provide const
constructors so that they can be used in const expressions.
const
Properties
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
- samplingSeed → int
-
Returns a seed value used by generateSamples to seed a random number
generator to avoid sample aliasing. [...]
@protected, read-only
Methods
-
findInverse(
double x ) → double -
Returns the parameter
t
that corresponds to the given x value of the spline. [...] -
generateSamples(
{double start: 0.0, double end: 1.0, double tolerance: 1e-10} ) → Iterable< Curve2DSample> -
Generates a list of samples with a recursive subdivision until a tolerance
of
tolerance
is reached. [...] -
noSuchMethod(
Invocation invocation ) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
toString(
) → String -
Returns a string representation of this object.
inherited
-
transform(
double t ) → Offset -
Returns the value of the curve at point
t
. [...]inherited -
transformInternal(
double t ) → Offset -
Returns the value of the curve at point
t
. [...]@protected, inherited
Operators
-
operator ==(
Object other ) → bool -
The equality operator. [...]
inherited