Draw and paint on a canvas in Flutter
In Flutter, you can use the CustomPaint and CustomPainter classes to draw to the canvas.
The following example shows how to draw during the paint phase using the CustomPaint widget. It implements the abstract class, CustomPainter, and passes it to CustomPaint’s painter property. CustomPaint subclasses must implement the paint() and shouldRepaint() methods.
class MyCanvasPainter extends CustomPainter { const MyCanvasPainter(); @override void paint(Canvas canvas, Size size) { final Paint paint = Paint()..color = Colors.amber; canvas.drawCircle(const Offset(100.0, 200.0), 40.0, paint); final Paint paintRect = Paint()..color = Colors.lightBlue; final Rect rect = Rect.fromPoints( const Offset(150.0, 300.0), const Offset(300.0, 400.0), ); canvas.drawRect(rect, paintRect); } @override bool shouldRepaint(MyCanvasPainter oldDelegate) => false; } class MyCanvasWidget extends StatelessWidget { const MyCanvasWidget({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return const Scaffold( body: CustomPaint(painter: MyCanvasPainter()), ); } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts