I tried to draw image using Canvas and CustomPainter but it's not working. This is for an Android app using Flutter Framework.
I just used command flutter build apk
in Windows cmd to build the app.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text('Title!')),
body: Center(
child: AspectRatio(
aspectRatio: 1.0,
child: CustomPaint(
painter: ImageEditor(),
),
),
),
);
}
}
class ImageEditor extends CustomPainter {
@override
Future paint(Canvas canvas, Size size) async {
canvas.save();
ByteData bd = await rootBundle.load("assets/sampleImagees.jpg");
final Uint8List bytes = Uint8List.view(bd.buffer);
final ui.Codec codec = await ui.instantiateImageCodec(bytes);
final ui.Image image = (await codec.getNextFrame()).image;
canvas.drawImage(image, Offset(0.0, 0.0), Paint());
canvas.save();
canvas.restore();
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
There isn't any error but nothing happend in the app. It's just a white screen. What's wrong in my code?