自訂緩動曲線

``````class _Linear extends Curve {
const _Linear._();

@override
double transformInternal(double t) => t;
}
``````

`transformInternal` 收到的 `t` 必然是在 0.0 與 1.0 之間，因為 `Curve``transform` 就這麼限制了，`super.transform(t)` 中會呼叫 `transformInternal`

``````@immutable
abstract class Curve extends ParametricCurve<double> {
...
@override
double transform(double t) {
if (t == 0.0 || t == 1.0) {
return t;
}
return super.transform(t);
}

...
}
``````

``````class SineCurve extends Curve {
double period = 1;

SineCurve({this.period});

@override
double transformInternal(double t) {
return sin(period * 2 * pi * t);
}
}
``````

``````import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(
MaterialApp(
home: Caterpillar(),
)
);

class Caterpillar extends StatefulWidget {
@override
State<StatefulWidget> createState() => _CaterpillarState();
}

class _CaterpillarState extends State<Caterpillar> {
var forward = true;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('openhome.cc'),
),
body: Center(
child: AnimatedContainer(
transform: Matrix4.translationValues(forward ? -150: 150, 0, 0),
width: 150,
child: Image.network('https://openhome.cc/Gossip/images/caterpillar.jpg'),
duration: Duration(seconds: 1),
curve: SineCurve(period: 1.25),
)
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
forward = !forward;
}),  // 註冊 onPressed 處理器
tooltip: forward ? 'forward' : 'back',
child: Icon(forward ? Icons.arrow_forward : Icons.arrow_back),
),
);
}
}

class SineCurve extends Curve {
double period = 1;

SineCurve({this.period});

@override
double transformInternal(double t) {
return sin(period * 2 * pi * t);
}
}
``````