-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathwaypoint_marker.dart
96 lines (82 loc) · 3.14 KB
/
waypoint_marker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:google_navigation_flutter/google_navigation_flutter.dart';
const Size _markerSize = Size(80, 80); // Example size in logical pixels
/// Creates a waypoint marker with the given waypoint number and registers it.
///
/// Returns an [ImageDescriptor] for the registered image.
Future<ImageDescriptor> registerWaypointMarkerImage(
int waypointNumber, double imagePixelRatio) async {
final ui.PictureRecorder recorder = ui.PictureRecorder();
final Canvas canvas = Canvas(recorder);
final _WaypointMarkerPainter painter = _WaypointMarkerPainter(waypointNumber);
painter.paint(canvas, _markerSize);
final ui.Image image = await recorder
.endRecording()
.toImage(_markerSize.width.floor(), _markerSize.height.floor());
final ByteData? bytes =
await image.toByteData(format: ui.ImageByteFormat.png);
// Call registerBitmapImage with ByteData
return registerBitmapImage(bitmap: bytes!, imagePixelRatio: imagePixelRatio);
}
class _WaypointMarkerPainter extends CustomPainter {
_WaypointMarkerPainter(this.waypointNumber);
final int waypointNumber;
@override
void paint(Canvas canvas, Size size) {
const double strokeWidth = 6.0;
// Draw the background circle
final Paint circlePaint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
final Offset center = Offset(size.width / 2, size.height / 2);
final double radius = size.width / 2 - strokeWidth / 2;
canvas.drawCircle(center, radius, circlePaint);
// Draw the border
final Paint borderPaint = Paint()
..color = Colors.blue[800]!
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth;
canvas.drawCircle(center, radius, borderPaint);
// Draw the waypoint number
final TextSpan textSpan = TextSpan(
text: waypointNumber.toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
);
final TextPainter textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(
maxWidth: size.width,
);
final Offset textOffset = Offset(
center.dx - textPainter.width / 2,
center.dy - textPainter.height / 2,
);
textPainter.paint(canvas, textOffset);
}
@override
bool shouldRepaint(_WaypointMarkerPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(_WaypointMarkerPainter oldDelegate) => false;
}