Skip to content

Instantly share code, notes, and snippets.

@erdoganbavas
Created September 18, 2019 20:34
Show Gist options
  • Save erdoganbavas/a93c40986ec1157221c526cd88e77ab8 to your computer and use it in GitHub Desktop.
Save erdoganbavas/a93c40986ec1157221c526cd88e77ab8 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';
class CustomPainterPractice extends StatefulWidget {
@override
_CustomPainterPracticeState createState() => _CustomPainterPracticeState();
}
class _CustomPainterPracticeState extends State<CustomPainterPractice>
with SingleTickerProviderStateMixin {
Animation<double> animation;
AnimationController controller;
double drawTime = 0.0;
double drawDuration = 4.0;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this, duration: Duration(seconds: drawDuration.toInt()));
animation =
Tween<double>(begin: 0.001, end: drawDuration).animate(controller)
..addListener(() {
setState(() {
drawTime = animation.value;
});
});
controller.forward();
}
@override
Widget build(BuildContext context) {
TextStyle textStyle = TextStyle(
fontSize: 18,
color: Colors.white
);
const double padding = 40.0;
return Material(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: CustomPaint(
painter: BobRoss(drawTime, drawDuration),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: padding),
child: Text(
"Curves.bounceInOut",
style: textStyle,
),
),
Padding(
padding: const EdgeInsets.only(bottom: padding),
child: Text(
"Curves.easeInOutCubic",
style: textStyle,
),
),
Padding(
padding: const EdgeInsets.only(bottom: padding),
child: Text(
"Curves.elasticInOut",
style: textStyle,
),
),
Padding(
padding: const EdgeInsets.only(bottom: padding),
child: Text(
"Curves.fastLinearToSlowEaseIn",
style: textStyle,
),
),
],
),
),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
class BobRoss extends CustomPainter {
final double drawTime;
final double drawDuration;
BobRoss(this.drawTime, this.drawDuration);
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..style = PaintingStyle.fill
..color = Colors.greenAccent;
canvas.drawRect(
Rect.fromLTWH(
0.0,
0.0,
size.width *
Curves.bounceInOut.transformInternal(drawTime / drawDuration),
50),
paint,
);
canvas.drawRect(
Rect.fromLTWH(
0.0,
60.0,
size.width *
Curves.easeInOutCubic.transformInternal(drawTime / drawDuration),
50),
paint,
);
canvas.drawRect(
Rect.fromLTWH(
0.0,
120.0,
size.width *
Curves.elasticInOut.transformInternal(drawTime / drawDuration),
50),
paint,
);
canvas.drawRect(
Rect.fromLTWH(
0.0,
180.0,
size.width *
Curves.fastLinearToSlowEaseIn
.transformInternal(drawTime / drawDuration),
50),
paint,
);
}
@override
bool shouldRepaint(BobRoss oldDelegate) => true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment