diff options
Diffstat (limited to 'plugins/aves_map/lib/src/marker')
| -rw-r--r-- | plugins/aves_map/lib/src/marker/arrow_painter.dart | 44 | ||||
| -rw-r--r-- | plugins/aves_map/lib/src/marker/dot.dart | 18 | ||||
| -rw-r--r-- | plugins/aves_map/lib/src/marker/generator.dart | 138 | ||||
| -rw-r--r-- | plugins/aves_map/lib/src/marker/image.dart | 166 | ||||
| -rw-r--r-- | plugins/aves_map/lib/src/marker/key.dart | 13 |
5 files changed, 379 insertions, 0 deletions
diff --git a/plugins/aves_map/lib/src/marker/arrow_painter.dart b/plugins/aves_map/lib/src/marker/arrow_painter.dart new file mode 100644 index 0000000..c38367f --- /dev/null +++ b/plugins/aves_map/lib/src/marker/arrow_painter.dart @@ -0,0 +1,44 @@ +import 'package:flutter/material.dart'; + +class MarkerArrowPainter extends CustomPainter { + final Color color, outlineColor; + final double outlineWidth; + final Size size; + + const MarkerArrowPainter({ + required this.color, + required this.outlineColor, + required this.outlineWidth, + required this.size, + }); + + @override + void paint(Canvas canvas, Size size) { + final triangleWidth = this.size.width; + final triangleHeight = this.size.height; + + final bottomCenter = Offset(size.width / 2, size.height); + final topLeft = bottomCenter + Offset(-triangleWidth / 2, -triangleHeight); + final topRight = bottomCenter + Offset(triangleWidth / 2, -triangleHeight); + + canvas.drawPath( + Path() + ..moveTo(bottomCenter.dx, bottomCenter.dy) + ..lineTo(topRight.dx, topRight.dy) + ..lineTo(topLeft.dx, topLeft.dy) + ..close(), + Paint()..color = outlineColor); + + canvas.translate(0, -outlineWidth.ceilToDouble()); + canvas.drawPath( + Path() + ..moveTo(bottomCenter.dx, bottomCenter.dy) + ..lineTo(topRight.dx, topRight.dy) + ..lineTo(topLeft.dx, topLeft.dy) + ..close(), + Paint()..color = color); + } + + @override + bool shouldRepaint(covariant CustomPainter oldDelegate) => false; +} diff --git a/plugins/aves_map/lib/src/marker/dot.dart b/plugins/aves_map/lib/src/marker/dot.dart new file mode 100644 index 0000000..b43b81b --- /dev/null +++ b/plugins/aves_map/lib/src/marker/dot.dart @@ -0,0 +1,18 @@ +import 'package:aves_map/src/theme.dart'; +import 'package:aves_ui/aves_ui.dart'; +import 'package:flutter/material.dart'; + +class DotMarker extends StatelessWidget { + const DotMarker({super.key}); + + @override + Widget build(BuildContext context) { + return const AvesDot( + diameter: MapThemeData.markerDotDiameter, + outerBorderWidth: MapThemeData.markerOuterBorderWidth, + innerBorderWidth: MapThemeData.markerInnerBorderWidth, + getOuterBorderColor: MapThemeData.markerThemedOuterBorderColor, + getInnerBorderColor: MapThemeData.markerThemedInnerBorderColor, + ); + } +} diff --git a/plugins/aves_map/lib/src/marker/generator.dart b/plugins/aves_map/lib/src/marker/generator.dart new file mode 100644 index 0000000..ca536b3 --- /dev/null +++ b/plugins/aves_map/lib/src/marker/generator.dart @@ -0,0 +1,138 @@ +import 'dart:ui' as ui; + +import 'package:collection/collection.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +// generate bitmap from widget, for Google map +class MarkerGeneratorWidget<T extends Key> extends StatefulWidget { + final List<Widget> markers; + final bool Function(T markerKey) isReadyToRender; + final void Function(T markerKey, Uint8List bitmap) onRendered; + + const MarkerGeneratorWidget({ + super.key, + required this.markers, + required this.isReadyToRender, + required this.onRendered, + }); + + @override + State<MarkerGeneratorWidget<T>> createState() => _MarkerGeneratorWidgetState<T>(); +} + +class _MarkerGeneratorWidgetState<T extends Key> extends State<MarkerGeneratorWidget<T>> { + final Set<_MarkerGeneratorItem<T>> _items = {}; + + @override + void initState() { + super.initState(); + _checkNextFrame(); + } + + @override + void didUpdateWidget(covariant MarkerGeneratorWidget<T> oldWidget) { + super.didUpdateWidget(oldWidget); + final devicePixelRatio = View.of(context).devicePixelRatio; + widget.markers.forEach((markerWidget) { + final item = _getOrCreate( + markerKey: markerWidget.key as T, + devicePixelRatio: devicePixelRatio, + ); + item.globalKey = GlobalKey(); + }); + _checkNextFrame(); + } + + void _checkNextFrame() { + WidgetsBinding.instance.addPostFrameCallback((_) async { + if (!mounted) return; + final waitingItems = _items.where((v) => v.isWaiting).toSet(); + final readyItems = waitingItems.where((v) => widget.isReadyToRender(v.markerKey)).toSet(); + readyItems.forEach((v) async { + final bitmap = await v.render(); + if (bitmap != null) { + widget.onRendered(v.markerKey, bitmap); + } + }); + if (readyItems.length < waitingItems.length) { + _checkNextFrame(); + setState(() {}); + } + }); + } + + @override + Widget build(BuildContext context) { + return Transform.translate( + offset: Offset(MediaQuery.sizeOf(context).width, 0), + child: Material( + type: MaterialType.transparency, + child: Stack( + children: _items.map((item) { + return RepaintBoundary( + key: item.globalKey, + child: widget.markers.firstWhereOrNull((v) => v.key == item.markerKey) ?? const SizedBox(), + ); + }).toList(), + ), + ), + ); + } + + _MarkerGeneratorItem _getOrCreate({ + required T markerKey, + required double devicePixelRatio, + }) { + final existingItem = _items.firstWhereOrNull((v) => v.markerKey == markerKey); + if (existingItem != null) return existingItem; + + final newItem = _MarkerGeneratorItem( + markerKey: markerKey, + devicePixelRatio: devicePixelRatio, + ); + _items.add(newItem); + return newItem; + } +} + +enum MarkerGeneratorItemState { waiting, rendering, done } + +class _MarkerGeneratorItem<T extends Key> { + final T markerKey; + final double devicePixelRatio; + GlobalKey? globalKey; + MarkerGeneratorItemState state = MarkerGeneratorItemState.waiting; + + _MarkerGeneratorItem({ + required this.markerKey, + required this.devicePixelRatio, + }); + + bool get isWaiting => state == MarkerGeneratorItemState.waiting; + + Future<Uint8List?> render() async { + Uint8List? bytes; + final _globalKey = globalKey; + if (_globalKey != null) { + state = MarkerGeneratorItemState.rendering; + final boundary = _globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary; + if (boundary.hasSize && boundary.size != Size.zero) { + try { + final image = await boundary.toImage(pixelRatio: devicePixelRatio); + final byteData = await image.toByteData(format: ui.ImageByteFormat.png); + bytes = byteData?.buffer.asUint8List(); + } catch (error) { + // happens when widget is offscreen + debugPrint('failed to render image for key=$_globalKey with error=$error'); + } + } + state = bytes != null ? MarkerGeneratorItemState.done : MarkerGeneratorItemState.waiting; + } + return bytes; + } + + @override + String toString() => '$runtimeType#${shortHash(this)}{markerKey=$markerKey, globalKey=$globalKey, state=$state}'; +} diff --git a/plugins/aves_map/lib/src/marker/image.dart b/plugins/aves_map/lib/src/marker/image.dart new file mode 100644 index 0000000..58f0377 --- /dev/null +++ b/plugins/aves_map/lib/src/marker/image.dart @@ -0,0 +1,166 @@ +import 'package:aves_map/aves_map.dart'; +import 'package:aves_map/src/marker/arrow_painter.dart'; +import 'package:collection/collection.dart'; +import 'package:custom_rounded_rectangle_border/custom_rounded_rectangle_border.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_map/flutter_map.dart'; +import 'package:intl/intl.dart' as intl; +import 'package:latlong2/latlong.dart'; + +class ImageMarker extends StatelessWidget { + final int? count; + final intl.NumberFormat countFormatter; + final bool drawArrow; + final Widget Function(double extent) buildThumbnailImage; + + static const double outerBorderRadiusDim = 8; + static const outerBorderWidth = MapThemeData.markerOuterBorderWidth; + static const innerBorderWidth = MapThemeData.markerInnerBorderWidth; + static const extent = MapThemeData.markerImageExtent; + static const arrowSize = MapThemeData.markerArrowSize; + static const outerBorderRadius = BorderRadius.all(Radius.circular(outerBorderRadiusDim)); + static const innerRadius = Radius.circular(outerBorderRadiusDim - outerBorderWidth); + static const innerBorderRadius = BorderRadius.all(innerRadius); + + ImageMarker({ + super.key, + required this.count, + required String? locale, + this.drawArrow = true, + required this.buildThumbnailImage, + }) : countFormatter = intl.NumberFormat.decimalPattern(locale); + + @override + Widget build(BuildContext context) { + Widget child = buildThumbnailImage(extent); + + // need to be sized for the Google map marker generator + child = SizedBox( + width: extent, + height: extent, + child: child, + ); + + final theme = Theme.of(context); + final isDark = theme.brightness == Brightness.dark; + final outerBorderColor = MapThemeData.markerThemedOuterBorderColor(isDark); + final innerBorderColor = MapThemeData.markerThemedInnerBorderColor(isDark); + + final outerDecoration = BoxDecoration( + border: Border.fromBorderSide(BorderSide( + color: outerBorderColor, + width: outerBorderWidth, + )), + borderRadius: outerBorderRadius, + ); + + final innerDecoration = BoxDecoration( + border: Border.fromBorderSide(BorderSide( + color: innerBorderColor, + width: innerBorderWidth, + )), + borderRadius: innerBorderRadius, + ); + + child = DecoratedBox( + decoration: innerDecoration, + position: DecorationPosition.foreground, + child: ClipRRect( + borderRadius: innerBorderRadius, + child: child, + ), + ); + + if (count != null) { + final borderSide = BorderSide( + color: innerBorderColor, + width: innerBorderWidth, + ); + child = Stack( + children: [ + child, + Container( + padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 2), + decoration: ShapeDecoration( + color: theme.colorScheme.primary, + shape: _buildCornerDetailShape(context, borderSide), + ), + child: Text( + countFormatter.format(count), + style: TextStyle( + fontSize: 12, + color: theme.colorScheme.onPrimary, + ), + ), + ), + ], + ); + } + + child = Container( + decoration: outerDecoration, + child: child, + ); + + if (drawArrow) { + child = CustomPaint( + foregroundPainter: MarkerArrowPainter( + color: innerBorderColor, + outlineColor: outerBorderColor, + outlineWidth: outerBorderWidth, + size: arrowSize, + ), + child: Padding( + padding: EdgeInsets.only(bottom: arrowSize.height), + child: child, + ), + ); + } + + return child; + } + + ShapeBorder _buildCornerDetailShape(BuildContext context, BorderSide side) { + final isRtl = Directionality.of(context) == TextDirection.rtl; + return CustomRoundedRectangleBorder( + leftSide: side, + rightSide: side, + topSide: side, + bottomSide: side, + topLeftCornerSide: isRtl ? null : side, + topRightCornerSide: isRtl ? side : null, + bottomLeftCornerSide: isRtl ? side : null, + bottomRightCornerSide: isRtl ? null : side, + borderRadius: BorderRadius.only( + topLeft: isRtl ? Radius.zero : innerRadius, + topRight: isRtl ? innerRadius : Radius.zero, + bottomLeft: isRtl ? innerRadius : Radius.zero, + bottomRight: isRtl ? Radius.zero : innerRadius, + ), + ); + } + + static const _crs = Epsg3857(); + + static GeoEntry<T>? markerMatch<T>(LatLng position, double zoom, Set<GeoEntry<T>> markers) { + final pressOffset = _crs.latLngToOffset(position, zoom); + + const double markerWidth = extent; + const double markerHeight = extent; + + return markers.firstWhereOrNull((marker) { + final latitude = marker.latitude; + final longitude = marker.longitude; + if (latitude == null || longitude == null) return false; + + final markerAnchorPoint = _crs.latLngToOffset(LatLng(latitude, longitude), zoom); + final bottom = markerAnchorPoint.dy; + final top = bottom - markerHeight; + final left = markerAnchorPoint.dx - markerWidth / 2; + final right = left + markerWidth; + final markerRect = Rect.fromLTRB(left, top, right, bottom); + + return markerRect.contains(pressOffset); + }); + } +} diff --git a/plugins/aves_map/lib/src/marker/key.dart b/plugins/aves_map/lib/src/marker/key.dart new file mode 100644 index 0000000..188ec01 --- /dev/null +++ b/plugins/aves_map/lib/src/marker/key.dart @@ -0,0 +1,13 @@ +import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; + +@immutable +class MarkerKey<T> extends LocalKey with EquatableMixin { + final T entry; + final int? count; + + @override + List<Object?> get props => [entry, count]; + + const MarkerKey(this.entry, this.count); +} |
