Flutter offers a rich set of tools for creating beautiful and interactive user interfaces. One of the most powerful features is the CustomPaint
widget, which allows you to draw custom graphics directly on the screen. In this tutorial, we’ll delve into the process of creating a custom paint widget and explore some practical examples.
Understanding the CustomPaint
Widget
The CustomPaint
widget takes a single child, a CustomPainter
object. This object is responsible for defining the painting logic. The CustomPainter
class provides two key methods:
paint(Canvas canvas, Size size)
: This method is called to draw the graphics onto the canvas. Thecanvas
object provides various methods for drawing shapes, lines, and text. Thesize
object represents the dimensions of the canvas.shouldRepaint(CustomPainter oldDelegate)
: This method is called to determine whether the widget needs to be repainted. It’s often used to optimize performance by avoiding unnecessary repaints.
Basic Example: Drawing a Circle
Let’s start with a simple example: drawing a circle on the screen.
import 'package:flutter/material.dart';
class CustomCirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
final
center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2;
canvas.drawCircle(center, radius, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate)
{
return false;
}
}
class CustomCircleWidget extends StatelessWidget
{
@override
Widget build(BuildContext context) {
return CustomPaint(
size: Size(200, 200),
painter: CustomCirclePainter(),
);
}
}
In this example:
- We create a
CustomCirclePainter
class that extendsCustomPainter
. - In the
paint
method, we create anPaint
object to define the color and style of the circle. - We calculate the center and radius of the circle based on the canvas size.
- Finally, we use the
canvas.drawCircle
method to draw the circle.
Advanced Example: Creating a Custom Progress Bar
Now, let’s create a more complex example: a custom progress bar.
class CustomProgressBar extends CustomPainter {
final double progress;
CustomProgressBar({required this.progress});
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
final
progressBarWidth = size.width * progress;
final progressBarRect = Rect.fromLTWH(0, 0, progressBarWidth, size.height);
canvas.drawRect(progressBarRect, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return progress != (oldDelegate as CustomProgressBar).progress;
}
}
In this example:
- We create a
CustomProgressBar
class that takes aprogress
value as a parameter. - In the
paint
method, we calculate the width of the progress bar based on theprogress
value. - We create a
Rect
object to represent the area of the progress bar. - Finally, we use the
canvas.drawRect
method to draw the progress bar.
Conclusion
The CustomPaint
widget is a powerful tool for creating custom graphics in Flutter. By understanding the basics of the CustomPainter
class and the various drawing methods provided by the Canvas
object, you can unleash your creativity and build stunning visual experiences.
Additional Tips:
- Optimize Performance: Use the
shouldRepaint
method to avoid unnecessary repaints. - Complex Graphics: For complex graphics, consider using paths and transformations.
- Text Rendering: Use the
TextPainter
class for advanced text rendering. - Image Drawing: Use the
canvas.drawImage
method to draw images.
By following these tips and experimenting with the CustomPaint
widget, you can create a wide range of custom graphics to enhance your Flutter apps.