|
| 1 | +import 'package:flutter/widgets.dart'; |
| 2 | +import 'package:flutter_map/flutter_map.dart'; |
| 3 | + |
| 4 | +part 'painter.dart'; |
| 5 | + |
| 6 | +/// Layer which draws a grid consisting of orthogonal major and minor strokes, |
| 7 | +/// configured by a [PlaceholderGridOptions]. |
| 8 | +/// |
| 9 | +/// This layer is usually drawn by default on the map background, but can be |
| 10 | +/// inserted manually. See [MapOptions.backgroundGridOptions] for info. |
| 11 | +/// |
| 12 | +/// This layer is intended for use in a [FlutterMap]. However, it will work |
| 13 | +/// without being placed in one. |
| 14 | +class PlaceholderGridLayer extends StatelessWidget { |
| 15 | + /// Build a widget which draws a grid consisting of orthogonal major and minor |
| 16 | + /// strokes, configured by a [PlaceholderGridOptions]. |
| 17 | + /// |
| 18 | + /// The widget does not have to be used within a map context. |
| 19 | + const PlaceholderGridLayer({ |
| 20 | + super.key, |
| 21 | + this.gridOptions = const PlaceholderGridOptions(), |
| 22 | + }); |
| 23 | + |
| 24 | + /// Configuration of the grid drawn. |
| 25 | + final PlaceholderGridOptions gridOptions; |
| 26 | + |
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + final camera = MapCamera.maybeOf(context); |
| 30 | + |
| 31 | + late final autoGridColor = () { |
| 32 | + const delta = 0.15; |
| 33 | + |
| 34 | + if (MapOptions.maybeOf(context)?.backgroundColor case final color?) { |
| 35 | + final hsl = HSLColor.fromColor(color); |
| 36 | + return hsl |
| 37 | + .withLightness( |
| 38 | + ((hsl.lightness > 0.5 ? -delta : delta) + hsl.lightness) |
| 39 | + .clamp(0, 1), |
| 40 | + ) |
| 41 | + .toColor(); |
| 42 | + } |
| 43 | + }(); |
| 44 | + final static = gridOptions.static || camera == null; |
| 45 | + final spacing = gridOptions.spacing * |
| 46 | + (static |
| 47 | + ? 1 |
| 48 | + : camera.getZoomScale(camera.zoom, camera.zoom.floorToDouble())); |
| 49 | + |
| 50 | + final painted = CustomPaint( |
| 51 | + painter: _BackgroundGridPainter( |
| 52 | + color: gridOptions.color ?? autoGridColor ?? const Color(0x14000000), |
| 53 | + spacing: spacing, |
| 54 | + pixelOrigin: static ? null : camera.pixelOrigin, |
| 55 | + numOfMinors: gridOptions.numOfMinors, |
| 56 | + majorStrokeWidth: gridOptions.majorStrokeWidth, |
| 57 | + minorStrokeWidth: gridOptions.minorStrokeWidth, |
| 58 | + ), |
| 59 | + ); |
| 60 | + if (static) return painted; |
| 61 | + return MobileLayerTransformer(child: painted); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Configuration options for a [PlaceholderGridLayer], which is usually |
| 66 | +/// displayed on the map background. |
| 67 | +/// |
| 68 | +/// The grid consists of orthogonal major and minor strokes. |
| 69 | +@immutable |
| 70 | +class PlaceholderGridOptions { |
| 71 | + /// Color of the grid strokes. |
| 72 | + /// |
| 73 | + /// Defaults to a slightly ligher or darker shade of the |
| 74 | + /// [MapOptions.backgroundColor], based on if it's dark or light respectively. |
| 75 | + /// |
| 76 | + /// If no inherited [MapOptions] is available, the default color is a |
| 77 | + /// translucent black. |
| 78 | + final Color? color; |
| 79 | + |
| 80 | + /// Gap between major strokes (at integer zoom levels when [static] is |
| 81 | + /// `false`), in logical pixels. |
| 82 | + /// |
| 83 | + /// To ensure a grid is visible while tiles are loading in holes, it's |
| 84 | + /// recommended to set this to a smaller value than the tile size. |
| 85 | + /// |
| 86 | + /// Defaults to 64. |
| 87 | + final double spacing; |
| 88 | + |
| 89 | + /// Whether the grid should be static and not move or zoom with the map, |
| 90 | + /// instead of its default behaviour of moving and scaling with zoom levels. |
| 91 | + /// |
| 92 | + /// If no inherited [MapCamera] is available, the grid is always static. |
| 93 | + final bool static; |
| 94 | + |
| 95 | + /// Thickness of major strokes. |
| 96 | + /// |
| 97 | + /// Defaults to 1.5. |
| 98 | + final double majorStrokeWidth; |
| 99 | + |
| 100 | + /// Thickness of minor strokes. |
| 101 | + /// |
| 102 | + /// Defaults to 0 (a hairline stroke). |
| 103 | + final double minorStrokeWidth; |
| 104 | + |
| 105 | + /// Number of minor strokes to insert between major strokes. |
| 106 | + /// |
| 107 | + /// Defaults to 3. Set to 0 to disable minor strokes. |
| 108 | + final int numOfMinors; |
| 109 | + |
| 110 | + /// Configuration options for a grid, usually displayed on the map background. |
| 111 | + const PlaceholderGridOptions({ |
| 112 | + this.color, |
| 113 | + this.spacing = 64, |
| 114 | + this.static = false, |
| 115 | + this.majorStrokeWidth = 1.5, |
| 116 | + this.minorStrokeWidth = 0, |
| 117 | + this.numOfMinors = 3, |
| 118 | + }) : assert( |
| 119 | + spacing >= 1 && spacing < double.infinity, |
| 120 | + '`spacing` must be equal to or greater than one, and finite', |
| 121 | + ), |
| 122 | + assert( |
| 123 | + majorStrokeWidth >= 0, |
| 124 | + '`majorStrokeWidth` must be equal to or greater than zero', |
| 125 | + ), |
| 126 | + assert( |
| 127 | + minorStrokeWidth >= 0, |
| 128 | + '`minorStrokeWidth` must be equal to or greater than zero', |
| 129 | + ), |
| 130 | + assert( |
| 131 | + numOfMinors >= 0, |
| 132 | + '`numOfMinors` must be equal to or greater than zero', |
| 133 | + ); |
| 134 | + |
| 135 | + @override |
| 136 | + int get hashCode => Object.hash( |
| 137 | + color, |
| 138 | + spacing, |
| 139 | + static, |
| 140 | + majorStrokeWidth, |
| 141 | + minorStrokeWidth, |
| 142 | + numOfMinors, |
| 143 | + ); |
| 144 | + |
| 145 | + @override |
| 146 | + bool operator ==(Object other) => |
| 147 | + identical(this, other) || |
| 148 | + other is PlaceholderGridOptions && |
| 149 | + color == other.color && |
| 150 | + spacing == other.spacing && |
| 151 | + static == other.static && |
| 152 | + majorStrokeWidth == other.majorStrokeWidth && |
| 153 | + minorStrokeWidth == other.minorStrokeWidth && |
| 154 | + numOfMinors == other.numOfMinors; |
| 155 | +} |
0 commit comments