Skip to content

Commit 34ea0c3

Browse files
Support Hybrid Composition through the GoogleMaps Widget (flutter#4082)
1 parent 93f0483 commit 34ea0c3

File tree

6 files changed

+91
-15
lines changed

6 files changed

+91
-15
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.0
2+
3+
* Add iOS unit and UI integration test targets.
4+
* Provide access to Hybrid Composition on Android through the `GoogleMap` widget.
5+
16
## 2.0.11
27

38
* Add additional marker drag events.

packages/google_maps_flutter/google_maps_flutter/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ This means that app will only be available for users that run Android SDK 20 or
4646
android:value="YOUR KEY HERE"/>
4747
```
4848

49+
#### Hybrid Composition
50+
51+
To use [Hybrid Composition](https://flutter.dev/docs/development/platform-integration/platform-views)
52+
to render the `GoogleMap` widget on Android, set `AndroidGoogleMapsFlutter.useAndroidViewSurface` to
53+
true.
54+
55+
```dart
56+
if (defaultTargetPlatform == TargetPlatform.android) {
57+
AndroidGoogleMapsFlutter.useAndroidViewSurface = true;
58+
}
59+
```
60+
4961
### iOS
5062

5163
This plugin requires iOS 9.0 or higher. To set up, specify your API key in the application delegate `ios/Runner/AppDelegate.m`:

packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
// ignore_for_file: public_member_api_docs
66

7+
import 'package:flutter/foundation.dart';
78
import 'package:flutter/material.dart';
9+
10+
import 'package:google_maps_flutter/google_maps_flutter.dart';
811
import 'package:google_maps_flutter_example/lite_mode.dart';
912
import 'animate_camera.dart';
1013
import 'map_click.dart';
@@ -66,5 +69,8 @@ class MapsDemo extends StatelessWidget {
6669
}
6770

6871
void main() {
72+
if (defaultTargetPlatform == TargetPlatform.android) {
73+
AndroidGoogleMapsFlutter.useAndroidViewSurface = true;
74+
}
6975
runApp(MaterialApp(home: MapsDemo()));
7076
}

packages/google_maps_flutter/google_maps_flutter/lib/src/google_map.dart

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@ class UnknownMapObjectIdError extends Error {
3838
}
3939
}
4040

41+
/// Android specific settings for [GoogleMap].
42+
class AndroidGoogleMapsFlutter {
43+
/// Whether to render [GoogleMap] with a [AndroidViewSurface] to build the Google Maps widget.
44+
///
45+
/// This implementation uses hybrid composition to render the Google Maps
46+
/// Widget on Android. This comes at the cost of some performance on Android
47+
/// versions below 10. See
48+
/// https://flutter.dev/docs/development/platform-integration/platform-views#performance for more
49+
/// information.
50+
///
51+
/// Defaults to false.
52+
static bool get useAndroidViewSurface {
53+
final GoogleMapsFlutterPlatform platform =
54+
GoogleMapsFlutterPlatform.instance;
55+
if (platform is MethodChannelGoogleMapsFlutter) {
56+
return platform.useAndroidViewSurface;
57+
}
58+
return false;
59+
}
60+
61+
/// Set whether to render [GoogleMap] with a [AndroidViewSurface] to build the Google Maps widget.
62+
///
63+
/// This implementation uses hybrid composition to render the Google Maps
64+
/// Widget on Android. This comes at the cost of some performance on Android
65+
/// versions below 10. See
66+
/// https://flutter.dev/docs/development/platform-integration/platform-views#performance for more
67+
/// information.
68+
///
69+
/// Defaults to false.
70+
static set useAndroidViewSurface(bool useAndroidViewSurface) {
71+
final GoogleMapsFlutterPlatform platform =
72+
GoogleMapsFlutterPlatform.instance;
73+
if (platform is MethodChannelGoogleMapsFlutter) {
74+
platform.useAndroidViewSurface = useAndroidViewSurface;
75+
}
76+
}
77+
}
78+
4179
/// A widget which displays a map with data obtained from the Google Maps service.
4280
class GoogleMap extends StatefulWidget {
4381
/// Creates a widget displaying data from Google Maps services.
@@ -61,6 +99,7 @@ class GoogleMap extends StatefulWidget {
6199
this.tiltGesturesEnabled = true,
62100
this.myLocationEnabled = false,
63101
this.myLocationButtonEnabled = true,
102+
this.layoutDirection,
64103

65104
/// If no padding is specified default padding will be 0.
66105
this.padding = const EdgeInsets.all(0),
@@ -100,6 +139,12 @@ class GoogleMap extends StatefulWidget {
100139
/// Type of map tiles to be rendered.
101140
final MapType mapType;
102141

142+
/// The layout direction to use for the embedded view.
143+
///
144+
/// If this is null, the ambient [Directionality] is used instead. If there is
145+
/// no ambient [Directionality], [TextDirection.ltr] is used.
146+
final TextDirection? layoutDirection;
147+
103148
/// Preferred bounds for the camera zoom level.
104149
///
105150
/// Actual bounds depend on map data and device.
@@ -250,9 +295,12 @@ class _GoogleMapState extends State<GoogleMap> {
250295

251296
@override
252297
Widget build(BuildContext context) {
253-
return GoogleMapsFlutterPlatform.instance.buildView(
298+
return GoogleMapsFlutterPlatform.instance.buildViewWithTextDirection(
254299
_mapId,
255300
onPlatformViewCreated,
301+
textDirection: widget.layoutDirection ??
302+
Directionality.maybeOf(context) ??
303+
TextDirection.ltr,
256304
initialCameraPosition: widget.initialCameraPosition,
257305
markers: widget.markers,
258306
polygons: widget.polygons,

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
repository: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 2.0.11
5+
version: 2.1.0
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

packages/google_maps_flutter/google_maps_flutter/test/google_map_test.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
66
import 'package:flutter/widgets.dart';
77
import 'package:flutter_test/flutter_test.dart';
88
import 'package:google_maps_flutter/google_maps_flutter.dart';
9+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
910

1011
import 'fake_maps_controllers.dart';
1112

@@ -604,17 +605,21 @@ void main() {
604605
},
605606
);
606607

607-
// TODO(bparrishMines): Uncomment once https://github.com/flutter/plugins/pull/4017 has landed.
608-
// testWidgets('Use AndroidViewSurface on Android', (WidgetTester tester) async {
609-
// await tester.pumpWidget(
610-
// const Directionality(
611-
// textDirection: TextDirection.ltr,
612-
// child: GoogleMap(
613-
// initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
614-
// ),
615-
// ),
616-
// );
617-
//
618-
// expect(find.byType(AndroidViewSurface), findsOneWidget);
619-
// });
608+
testWidgets('Use PlatformViewLink on Android', (WidgetTester tester) async {
609+
final MethodChannelGoogleMapsFlutter platform =
610+
GoogleMapsFlutterPlatform.instance as MethodChannelGoogleMapsFlutter;
611+
platform.useAndroidViewSurface = true;
612+
613+
await tester.pumpWidget(
614+
const Directionality(
615+
textDirection: TextDirection.ltr,
616+
child: GoogleMap(
617+
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
618+
),
619+
),
620+
);
621+
622+
expect(find.byType(PlatformViewLink), findsOneWidget);
623+
platform.useAndroidViewSurface = false;
624+
});
620625
}

0 commit comments

Comments
 (0)