Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to set map colorscheme #63

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.4.0

* Add option to set a `colorScheme` for the map (system, light, dark)

## 1.3.0

* Animate marker position changes instead of removing and re-adding
Expand Down
15 changes: 15 additions & 0 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _myLocationButtonEnabled = true;
MinMaxZoomPreference _minMaxZoomPreference = MinMaxZoomPreference.unbounded;
MapType _mapType = MapType.standard;
MapColorScheme _colorScheme = MapColorScheme.system;
bool _rotateGesturesEnabled = true;
bool _scrollGesturesEnabled = true;
bool _pitchGesturesEnabled = true;
Expand Down Expand Up @@ -98,6 +99,18 @@ class MapUiBodyState extends State<MapUiBody> {
);
}

Widget _colorSchemeCycler() {
final MapColorScheme nextScheme = MapColorScheme
.values[(_colorScheme.index + 1) % MapColorScheme.values.length];
return TextButton(
child: Text('change color scheme to $nextScheme'),
onPressed: () {
setState(() {
_colorScheme = nextScheme;
});
});
}

Widget _rotateToggler() {
return TextButton(
child: Text('${_rotateGesturesEnabled ? 'disable' : 'enable'} rotate'),
Expand Down Expand Up @@ -170,6 +183,7 @@ class MapUiBodyState extends State<MapUiBody> {
Widget build(BuildContext context) {
final AppleMap appleMap = AppleMap(
onMapCreated: onMapCreated,
colorScheme: _colorScheme,
trackingMode: _trackingMode,
initialCameraPosition: _kInitialPosition,
compassEnabled: _compassEnabled,
Expand Down Expand Up @@ -207,6 +221,7 @@ class MapUiBodyState extends State<MapUiBody> {
children: <Widget>[
_compassToggler(),
_mapTypeCycler(),
_colorSchemeCycler(),
_zoomBoundsToggler(),
_rotateToggler(),
_scrollToggler(),
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/MapView/FlutterMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ class FlutterMapView: MKMapView, UIGestureRecognizerDelegate {
if let mapType: Int = options["mapType"] as? Int {
self.mapType = self.mapTypes[mapType]
}

if let colorScheme: Int = options["colorScheme"] as? Int {
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: colorScheme) ?? .unspecified
}
}

if let trafficEnabled: Bool = options["trafficEnabled"] as? Bool {
if #available(iOS 9.0, *) {
Expand Down
25 changes: 23 additions & 2 deletions lib/src/apple_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AppleMap extends StatefulWidget {
this.compassEnabled = true,
this.trafficEnabled = false,
this.mapType = MapType.standard,
this.colorScheme = MapColorScheme.system,
this.minMaxZoomPreference = MinMaxZoomPreference.unbounded,
this.trackingMode = TrackingMode.none,
this.rotateGesturesEnabled = true,
Expand Down Expand Up @@ -59,6 +60,9 @@ class AppleMap extends StatefulWidget {
/// Type of map tiles to be rendered.
final MapType mapType;

/// Color scheme for the standard map to use.
final MapColorScheme colorScheme;

/// The mode used to track the user location.
final TrackingMode trackingMode;

Expand Down Expand Up @@ -188,7 +192,7 @@ class _AppleMapState extends State<AppleMap> {
Widget build(BuildContext context) {
final Map<String, dynamic> creationParams = <String, dynamic>{
'initialCameraPosition': widget.initialCameraPosition._toMap(),
'options': _appleMapOptions.toMap(),
'options': _appleMapOptions.toMap(context: context),
'annotationsToAdd': _serializeAnnotationSet(widget.annotations),
'polylinesToAdd': _serializePolylineSet(widget.polylines),
'polygonsToAdd': _serializePolygonSet(widget.polygons),
Expand Down Expand Up @@ -332,6 +336,7 @@ class _AppleMapOptions {
this.compassEnabled,
this.trafficEnabled,
this.mapType,
this.colorScheme,
this.minMaxZoomPreference,
this.rotateGesturesEnabled,
this.scrollGesturesEnabled,
Expand All @@ -349,6 +354,7 @@ class _AppleMapOptions {
compassEnabled: map.compassEnabled,
trafficEnabled: map.trafficEnabled,
mapType: map.mapType,
colorScheme: map.colorScheme,
minMaxZoomPreference: map.minMaxZoomPreference,
rotateGesturesEnabled: map.rotateGesturesEnabled,
scrollGesturesEnabled: map.scrollGesturesEnabled,
Expand All @@ -368,6 +374,8 @@ class _AppleMapOptions {

final MapType? mapType;

final MapColorScheme? colorScheme;

final MinMaxZoomPreference? minMaxZoomPreference;

final bool? rotateGesturesEnabled;
Expand All @@ -388,7 +396,7 @@ class _AppleMapOptions {

final bool? insetsLayoutMarginsFromSafeArea;

Map<String, dynamic> toMap() {
Map<String, dynamic> toMap({BuildContext? context}) {
final Map<String, dynamic> optionsMap = <String, dynamic>{};

void addIfNonNull(String fieldName, dynamic value) {
Expand All @@ -397,6 +405,19 @@ class _AppleMapOptions {
}
}

if (context != null) {
final systemScheme = Theme.of(context).brightness == Brightness.dark
? MapColorScheme.dark
: MapColorScheme.light;
addIfNonNull(
'colorScheme',
colorScheme == MapColorScheme.system
? systemScheme.index
: colorScheme?.index);
} else {
addIfNonNull('colorScheme', colorScheme?.index);
}

addIfNonNull('compassEnabled', compassEnabled);
addIfNonNull('trafficEnabled', trafficEnabled);
addIfNonNull('mapType', mapType?.index);
Expand Down
8 changes: 8 additions & 0 deletions lib/src/ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ enum MapType {
hybrid,
}

enum MapColorScheme {
/// Follow system style
system,

light,
dark,
}

enum TrackingMode {
// the user's location is not followed
none,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: apple_maps_flutter
description: This plugin uses the Flutter platform view to display an Apple Maps widget.
version: 1.3.0
version: 1.4.0
homepage: https://luisthein.de
repository: https://github.com/LuisThein/apple_maps_flutter
issue_tracker: https://github.com/LuisThein/apple_maps_flutter/issues
Expand Down