Created
August 9, 2022 08:09
-
-
Save aloisdeniel/d3b4cdd8005dce491fa864e156b3f760 to your computer and use it in GitHub Desktop.
Flutter vector graphics - Custom painter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Heart(), | |
), | |
), | |
); | |
} | |
} | |
class Heart extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
width: 64, | |
height: 64, | |
child: CustomPaint( | |
painter: HeartPainter(), | |
), | |
); | |
} | |
} | |
class HeartPainter extends CustomPainter { | |
static final path = Path() | |
..moveTo(72, 15) | |
..cubicTo(39, -21, -14, 13, 4, 57) | |
..cubicTo(12, 75, 62, 119, 67, 124) | |
..cubicTo(70, 126, 74, 126, 77, 124) | |
..cubicTo(82, 119, 132, 75, 140, 57) | |
..cubicTo(159, 12, 101, -21, 72, 15) | |
..close(); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final fill = Paint() | |
..style = PaintingStyle.fill | |
..color = const Color(0xffFF3D5B); | |
canvas.drawPath(path, fill); | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment