I want to know how can I animate a widget in Flutter along a path like following image:
Let's say I have a simple curved line:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SamplePath(),
);
}
}
class SamplePath extends StatefulWidget {
@override
State<StatefulWidget> createState() => _SamplePathState();
}
class _SamplePathState extends State<SamplePath> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: CustomPaint( //
size: Size(MediaQuery.of(context).size.width, 300),
painter: MyPainter(),
),
),
);
}
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 8.0;
Path path = Path();
path.moveTo(0, size.height / 2);
path.quadraticBezierTo(size.width / 2, size.height, size.width, size.height / 2);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter old) {
return false;
}
}
above code generates following result:
and I want to animate a Container
or a CustomePaint
from begin to end of it. How should I do that?
Thanks in advance