I'm testing flutter FFI by getting colour data from C/C++ side and painting the screen using CustomPainter
.
But to my surprise, even though I set the painter to always repaint, the paint()
function is only called twice.
Code
class _ColorViewPainter extends CustomPainter {
Color clrBackground;
_ColorViewPainter({
this.clrBackground = Colors.black
});
@override
bool shouldRepaint(_ColorViewPainter old) => true;
@override
void paint(Canvas canvas, Size size) {
print("paint: start");
final color = ffiGetColor().ref;
final r = color.r;
final g = color.g;
final b = color.b;
print("color: $r, $g, $b");
final paint = Paint()
..strokeJoin = StrokeJoin.round
..strokeWidth = 1.0
..color = Color.fromARGB(255, r, g, b)
..style = PaintingStyle.fill;
final width = size.width;
final height = size.height;
final content = Offset(0.0, 0.0) & Size(width, height);
canvas.drawRect(content, paint);
print("paint: end");
}
}
The ffiGetColor()
function simply retrieves a colour RGB struct from C/C++ side. I can see the screen being updated twice and the log says:
I/flutter (12096): paint: start
I/flutter (12096): color: 255, 0, 0
I/flutter (12096): paint: end
I/flutter (12096): paint: start
I/flutter (12096): color: 0, 255, 0
I/flutter (12096): paint: end
I/Surface (12096): opservice is null false
But that's it. Even though I clearly want it to repaint with shouldRepaint
. flutter failed to do so.
What's wrong?
here is my environment
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, v1.18.0-dev.5.0, on Mac OS X 10.15.5 19F101, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[✓] Android Studio (version 4.0)
[✗] Cannot determine if IntelliJ is installed
✗ Directory listing failed
[✓] VS Code (version 1.44.2)
[✓] Connected device (1 available)
! Doctor found issues in 1 category.