drawParkingMarker function Null safety
Draw a marker (for the parking) and return a BitmapDescriptor to use in the map view
Implementation
Future<BitmapDescriptor> drawParkingMarker(ThemeData theme, Parking parking) async {
print("drawing bb");
final recorder = new ui.PictureRecorder();
final double w = 120;
final double h = 140;
final double ht = 30;
final double wt = 40;
final canvas = new Canvas(
recorder,
Rect.fromPoints(Offset(0.0, 0.0), Offset(w+wt, h+ht)));
final fill = Paint()
..color = theme.primaryColor
..style = PaintingStyle.fill;
final bg = RRect.fromRectAndRadius(
Rect.fromPoints( Offset(0.0, 0.0), Offset(w, h)),
Radius.circular(20),
);
final rectPath = Path()..addRRect(bg);
canvas.drawShadow(rectPath, Colors.grey, 4, false);
canvas.drawPath(rectPath, fill);
//triangle
var path = Path();
path.moveTo(w/2, h+ht);
path.lineTo((w-wt)/2, h);
path.lineTo((w+wt)/2, h);
path.close();
final filltriangle = Paint()
..color = theme.primaryColor
..style = PaintingStyle.fill;
canvas.drawPath(path, filltriangle);
//title
final pText = TextSpan(
text: 'P',
style: theme.textTheme.headline2!.apply(
color: Colors.white
)
);
final pTextPainter = TextPainter(
text: pText,
textDirection: TextDirection.ltr,
);
pTextPainter.layout(
minWidth: 0,
maxWidth: w,
);
// spaces
final sText = TextSpan(
text: parking.spacesAvailable.toString(),
style: theme.textTheme.headline4!.apply(
color: Colors.white,
fontWeightDelta: 2
)
);
final sTextPainter = TextPainter(
text: sText,
textDirection: TextDirection.ltr,
);
sTextPainter.layout(
minWidth: 0,
maxWidth: w,
);
var spacing = (h - pTextPainter.height - sTextPainter.height)/5;
pTextPainter.paint(canvas, Offset(
(w - pTextPainter.width)/2,
2*spacing)
);
sTextPainter.paint(canvas, Offset(
(w - sTextPainter.width)/2,
(3*spacing)+pTextPainter.height)
);
final picture = recorder.endRecording();
final img = await picture.toImage(124, 170);
final byteData = await img.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData!.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
print("drawing_bmp");
return BitmapDescriptor.fromBytes(pngBytes);
}