What is the purpose of the CustomPaint widget in Flutter?
The CustomPaint widget lets you draw graphics directly on the screen by using a CustomPainter. It is used when you want to create custom shapes, designs, or animations beyond standard widgets.
Click to reveal answer
beginner
What method must you override when creating a CustomPainter?
You must override the paint(Canvas canvas, Size size) method. This method contains the drawing instructions using the canvas and the available size.
Click to reveal answer
intermediate
What does the shouldRepaint method do in a CustomPainter?
The shouldRepaint method tells Flutter whether it needs to redraw the painting when the widget updates. Returning true means the painter should repaint, false means no repaint is needed.
Click to reveal answer
beginner
How do you use CustomPaint to add a custom drawing behind a widget?
You wrap the widget inside CustomPaint and provide your CustomPainter to the painter property. The custom drawing appears behind the child widget.
Click to reveal answer
intermediate
Why is it important to keep the paint method efficient in CustomPainter?
Because paint can be called many times during animations or screen updates, slow or complex drawing can cause the app to lag or drop frames. Keeping it efficient ensures smooth performance.
Click to reveal answer
Which method do you override to draw on the canvas in a CustomPainter?
Abuild(BuildContext context)
Bpaint(Canvas canvas, Size size)
Cdraw(Canvas canvas)
Drender(Canvas canvas)
✗ Incorrect
The paint(Canvas canvas, Size size) method is where you write your drawing code.
What does the shouldRepaint method control?
AWhether the painter should repaint when the widget updates
BThe color of the paint
CThe size of the canvas
DThe position of the widget
✗ Incorrect
It tells Flutter if the painting needs to be redrawn when the widget changes.
How do you add a custom drawing behind a button widget?
AUse <code>Stack</code> without <code>CustomPaint</code>
BPut the button inside a <code>Container</code>
CWrap the button with <code>CustomPaint</code> and set the painter
DUse <code>Text</code> widget
✗ Incorrect
Wrapping the button with CustomPaint and providing a painter draws behind the button.
Which object do you use inside paint to draw shapes?
AWidget
BState
CBuildContext
DCanvas
✗ Incorrect
The Canvas object provides methods to draw shapes, lines, and paths.
Why should you avoid heavy computations inside the paint method?
AIt can cause slow UI and dropped frames
BIt will crash the app immediately
CIt changes the widget tree
DIt disables animations
✗ Incorrect
Heavy computations slow down drawing and make the app feel laggy.
Explain how CustomPaint and CustomPainter work together to create custom drawings in Flutter.
Think about how the widget uses the painter to draw on the screen.
You got /5 concepts.
Describe best practices to keep your custom painting efficient and smooth in a Flutter app.
Consider what happens if painting is slow during animations.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a CustomPainter in Flutter?
easy
A. To draw custom graphics directly on the screen
B. To manage app navigation between screens
C. To handle user input events like taps and swipes
D. To create standard UI buttons and text fields
Solution
Step 1: Understand CustomPainter role
A CustomPainter is used to draw graphics directly on the canvas in Flutter apps.
Step 2: Compare with other options
Options A, B, and C relate to standard UI, navigation, and input events, not drawing.
Final Answer:
To draw custom graphics directly on the screen -> Option A
Quick Check:
CustomPainter = Draw graphics [OK]
Hint: CustomPainter is for drawing, not UI controls or navigation [OK]
Common Mistakes:
Confusing CustomPainter with widget layout
Thinking it handles user input
Assuming it manages navigation
2. Which method must you override when creating a CustomPainter subclass to draw on the canvas?
easy
A. paint
B. build
C. createState
D. initState
Solution
Step 1: Identify CustomPainter methods
The paint method is where drawing commands are written.
Step 2: Exclude unrelated methods
build, createState, and initState belong to widgets, not painters.
Final Answer:
paint -> Option A
Quick Check:
Draw in paint() method [OK]
Hint: paint() draws; build() is for widgets [OK]
Common Mistakes:
Using build() instead of paint()
Confusing widget lifecycle methods with painter methods
Forgetting to override paint()
3. What will be the visible result of this Flutter code snippet?
The return statement lacks a semicolon after true, causing a syntax error.
Step 2: Verify other parts
paint method correctly draws a rectangle; canvas supports rectangles; shouldRepaint can return true.
Final Answer:
Missing semicolon after return true -> Option C
Quick Check:
Syntax error = missing semicolon [OK]
Hint: Check semicolons after return statements [OK]
Common Mistakes:
Forgetting semicolon after return
Thinking paint must return a value
Assuming canvas can't draw rectangles
5. You want to create a CustomPainter that draws a circle only when the color changes. Which is the best way to implement shouldRepaint to optimize performance?
class CirclePainter extends CustomPainter {
final Color color;
CirclePainter(this.color);
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = color;
canvas.drawCircle(Offset(size.width/2, size.height/2), 40, paint);
}
@override
bool shouldRepaint(covariant CirclePainter oldDelegate) {
// What to write here?
}
}
hard
A. return oldDelegate.color == color;
B. return true;
C. return false;
D. return oldDelegate.color != color;
Solution
Step 1: Understand shouldRepaint purpose
shouldRepaint returns true if the painter needs to redraw due to changes.
Step 2: Compare colors to decide repaint
Repaint only if the new color differs from the old one, so compare oldDelegate.color and color.
Step 3: Evaluate options
return oldDelegate.color != color; returns true when colors differ, optimizing redraw. return true; always repaints (less efficient). return false; never repaints (ignores changes). return oldDelegate.color == color; repaints when colors are same (wrong logic).
Final Answer:
return oldDelegate.color != color; -> Option D
Quick Check:
Repaint if color changed = oldDelegate.color != color [OK]
Hint: Repaint only if properties differ for efficiency [OK]
Common Mistakes:
Always returning true wastes performance
Always returning false ignores changes
Using equality check wrongly in shouldRepaint
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.