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

[BREAKING CHANGE] update to null-safety #77

Open
wants to merge 3 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 5.0.1

* Removed unnecessary null checks
* Fixed markers not displaying on mobile

## 5.0.0

* Updated to null-safety
* [BREAKING CHANGE] Removed `WebMapStyleExtension` as it is no longer supported by the underlying package [google_maps](https://pub.dev/packages/google_maps)

## 4.0.1

* TypeError: this.widget.markers is not iterable (thanks to [slovnicki](https://github.com/slovnicki))
Expand Down
95 changes: 48 additions & 47 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_google_maps/flutter_google_maps.dart';

void main() {
Expand Down Expand Up @@ -35,37 +35,43 @@ class _MyHomePageState extends State<MyHomePage> {
final _key = GlobalKey<GoogleMapStateBase>();
bool _polygonAdded = false;
bool _darkMapStyle = false;
String _mapStyle;
String? _mapStyle;

List<Widget> _buildClearButtons() => [
RaisedButton.icon(
color: Colors.red,
textColor: Colors.white,
ElevatedButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
icon: Icon(Icons.bubble_chart),
label: Text('CLEAR POLYGONS'),
onPressed: () {
GoogleMap.of(_key).clearPolygons();
GoogleMap.of(_key)!.clearPolygons();
setState(() => _polygonAdded = false);
},
),
const SizedBox(width: 16),
RaisedButton.icon(
color: Colors.red,
textColor: Colors.white,
ElevatedButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
icon: Icon(Icons.pin_drop),
label: Text('CLEAR MARKERS'),
onPressed: () {
GoogleMap.of(_key).clearMarkers();
GoogleMap.of(_key)!.clearMarkers();
},
),
const SizedBox(width: 16),
RaisedButton.icon(
color: Colors.red,
textColor: Colors.white,
ElevatedButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
icon: Icon(Icons.directions),
label: Text('CLEAR DIRECTIONS'),
onPressed: () {
GoogleMap.of(_key).clearDirections();
GoogleMap.of(_key)!.clearDirections();
},
),
];
Expand All @@ -76,7 +82,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: _polygonAdded ? Colors.orange : null,
onPressed: () {
if (!_polygonAdded) {
GoogleMap.of(_key).addPolygon(
GoogleMap.of(_key)!.addPolygon(
'1',
polygon,
onTap: (polygonId) async {
Expand All @@ -88,7 +94,7 @@ class _MyHomePageState extends State<MyHomePage> {
'Polygon ID is $polygonId',
),
actions: <Widget>[
FlatButton(
TextButton(
onPressed: Navigator.of(context).pop,
child: Text('CLOSE'),
),
Expand All @@ -98,7 +104,7 @@ class _MyHomePageState extends State<MyHomePage> {
},
);
} else {
GoogleMap.of(_key).editPolygon(
GoogleMap.of(_key)!.editPolygon(
'1',
polygon,
fillColor: Colors.purple,
Expand All @@ -113,17 +119,16 @@ class _MyHomePageState extends State<MyHomePage> {
FloatingActionButton(
child: Icon(Icons.pin_drop),
onPressed: () {
GoogleMap.of(_key).addMarkerRaw(
GoogleMap.of(_key)!.addMarkerRaw(
GeoCoord(33.875513, -117.550257),
info: 'test info',
onInfoWindowTap: () async {
await showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
'This dialog was opened by tapping on the InfoWindow!'),
content: Text('This dialog was opened by tapping on the InfoWindow!'),
actions: <Widget>[
FlatButton(
TextButton(
onPressed: Navigator.of(context).pop,
child: Text('CLOSE'),
),
Expand All @@ -132,7 +137,7 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
);
GoogleMap.of(_key).addMarkerRaw(
GoogleMap.of(_key)!.addMarkerRaw(
GeoCoord(33.775513, -117.450257),
icon: 'assets/images/map-marker-warehouse.png',
info: contentString,
Expand All @@ -143,7 +148,7 @@ class _MyHomePageState extends State<MyHomePage> {
FloatingActionButton(
child: Icon(Icons.directions),
onPressed: () {
GoogleMap.of(_key).addDirection(
GoogleMap.of(_key)!.addDirection(
'San Francisco, CA',
'San Jose, CA',
startLabel: '1',
Expand Down Expand Up @@ -172,16 +177,16 @@ class _MyHomePageState extends State<MyHomePage> {
),
},
initialZoom: 12,
initialPosition:
GeoCoord(34.0469058, -118.3503948), // Los Angeles, CA
initialPosition: GeoCoord(34.0469058, -118.3503948), // Los Angeles, CA
mapType: MapType.roadmap,
mapStyle: _mapStyle,
interactive: true,
onTap: (coord) =>
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(coord?.toString()),
duration: const Duration(seconds: 2),
)),
onTap: (coord) => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(coord.toString()),
duration: const Duration(seconds: 2),
),
),
mobilePreferences: const MobileMapPreferences(
trafficEnabled: true,
zoomControlsEnabled: false,
Expand All @@ -202,14 +207,11 @@ class _MyHomePageState extends State<MyHomePage> {
northeast: GeoCoord(34.021307, -117.432317),
southwest: GeoCoord(33.835745, -117.712785),
);
GoogleMap.of(_key).moveCameraBounds(bounds);
GoogleMap.of(_key).addMarkerRaw(
GoogleMap.of(_key)!.moveCameraBounds(bounds);
GoogleMap.of(_key)!.addMarkerRaw(
GeoCoord(
(bounds.northeast.latitude + bounds.southwest.latitude) /
2,
(bounds.northeast.longitude +
bounds.southwest.longitude) /
2,
(bounds.northeast.latitude + bounds.southwest.latitude) / 2,
(bounds.northeast.longitude + bounds.southwest.longitude) / 2,
),
onTap: (markerId) async {
await showDialog(
Expand All @@ -220,7 +222,7 @@ class _MyHomePageState extends State<MyHomePage> {
'Marker ID is $markerId',
),
actions: <Widget>[
FlatButton(
TextButton(
onPressed: Navigator.of(context).pop,
child: Text('CLOSE'),
),
Expand All @@ -238,10 +240,10 @@ class _MyHomePageState extends State<MyHomePage> {
child: FloatingActionButton(
onPressed: () {
if (_darkMapStyle) {
GoogleMap.of(_key).changeMapStyle(null);
GoogleMap.of(_key)!.changeMapStyle(null);
_mapStyle = null;
} else {
GoogleMap.of(_key).changeMapStyle(darkMapStyle);
GoogleMap.of(_key)!.changeMapStyle(darkMapStyle);
_mapStyle = darkMapStyle;
}

Expand All @@ -261,13 +263,12 @@ class _MyHomePageState extends State<MyHomePage> {
child: Row(
children: <Widget>[
LayoutBuilder(
builder: (context, constraints) =>
constraints.maxWidth < 1000
? Row(children: _buildClearButtons())
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _buildClearButtons(),
),
builder: (context, constraints) => constraints.maxWidth < 1000
? Row(children: _buildClearButtons())
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _buildClearButtons(),
),
),
Spacer(),
..._buildAddButtons(),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_google_maps_example
description: Demonstrates how to use the flutter_google_maps package.

environment:
sdk: ">=2.6.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
41 changes: 13 additions & 28 deletions lib/src/core/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:google_directions_api/google_directions_api.dart' show GeoCoord, DirectionsService;

import 'package:google_directions_api/google_directions_api.dart'
show GeoCoord, DirectionsService;

import 'google_map.state.dart' if (dart.library.html) '../web/google_map.state.dart' if (dart.library.io) '../mobile/google_map.state.dart';
import 'map_items.dart';
import 'map_operations.dart';
import 'map_preferences.dart';

import 'google_map.state.dart'
if (dart.library.html) '../web/google_map.state.dart'
if (dart.library.io) '../mobile/google_map.state.dart';

/// This widget will try to occupy all available space
class GoogleMap extends StatefulWidget {
/// Creates an instance of [GoogleMap].
const GoogleMap({
Key key,
Key? key,
this.minZoom,
this.maxZoom,
this.mapStyle,
Expand All @@ -33,13 +28,7 @@ class GoogleMap extends StatefulWidget {
this.initialPosition = const GeoCoord(_defaultLat, _defaultLng),
this.mobilePreferences = const MobileMapPreferences(),
this.webPreferences = const WebMapPreferences(),
}) : assert(mapType != null),
assert(interactive != null),
assert(initialPosition != null),
assert(initialZoom != null),
assert(mobilePreferences != null),
assert(webPreferences != null),
super(key: key);
}) : super(key: key);

/// The initial position of the map's camera.
final GeoCoord initialPosition;
Expand All @@ -51,10 +40,10 @@ class GoogleMap extends StatefulWidget {
final MapType mapType;

/// The preferred minimum zoom level or null, if unbounded from below.
final double minZoom;
final double? minZoom;

/// The preferred maximum zoom level or null, if unbounded from above.
final double maxZoom;
final double? maxZoom;

/// Sets the styling of the base map.
///
Expand All @@ -69,21 +58,21 @@ class GoogleMap extends StatefulWidget {
/// Also, refer [iOS](https://developers.google.com/maps/documentation/ios-sdk/style-reference)
/// and [Android](https://developers.google.com/maps/documentation/android-sdk/style-reference)
/// style reference for more information regarding the supported styles.
final String mapStyle;
final String? mapStyle;

/// Defines whether map is interactive or not.
final bool interactive;

/// Called every time a [GoogleMap] is tapped.
final ValueChanged<GeoCoord> onTap;
final ValueChanged<GeoCoord>? onTap;

/// Markers to be placed on the map.
final Set<Marker> markers;

/// Called every time a [GoogleMap] is long pressed.
///
/// For `web` this will be called when `right mouse clicked`.
final ValueChanged<GeoCoord> onLongPress;
final ValueChanged<GeoCoord>? onLongPress;

/// Set of mobile map preferences.
final MobileMapPreferences mobilePreferences;
Expand All @@ -97,8 +86,7 @@ class GoogleMap extends StatefulWidget {

/// Gets [MapOperations] interface via provided `key` of
/// [GoogleMapStateBase] state.
static MapOperations of(GlobalKey<GoogleMapStateBase> key) =>
key.currentState;
static MapOperations? of(GlobalKey<GoogleMapStateBase> key) => key.currentState;

/// Initializer of [GoogleMap].
///
Expand All @@ -110,11 +98,8 @@ class GoogleMap extends StatefulWidget {
GoogleMapState createState() => GoogleMapState();
}

abstract class GoogleMapStateBase extends State<GoogleMap>
implements MapOperations {
abstract class GoogleMapStateBase extends State<GoogleMap> implements MapOperations {
@protected
String fixAssetPath(String icon) =>
icon.endsWith('/marker_a.png') || icon.endsWith('/marker_b.png')
? 'packages/flutter_google_maps/'
: '';
icon.endsWith('/marker_a.png') || icon.endsWith('/marker_b.png') ? 'packages/flutter_google_maps/' : '';
}
Loading