Skip to content

Commit

Permalink
integrate changes from flutter-mapbox-gl/maps#1172
Browse files Browse the repository at this point in the history
  • Loading branch information
u221711 committed Jan 27, 2023
1 parent ddaf4f5 commit 8e34b3d
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,6 @@ Please include the `NSLocationWhenInUseUsageDescription` as described [here](#lo


[Feedback](https://github.com/m0nac0/flutter-maplibre-gl/issues) and contributions are very welcome!

## Flutter 3.x.x issues and experimental workarounds
Since Flutter 3.x.x was introduced, it exposed some race conditions resulting in occasional crashes upon map disposal. The parameter `useDelayedDisposal` was introduced as a workaround for this issue until Flutter and/or Mapbox fix this issue properly. Use with caution - this is not yet production ready since several users still report crashes after using this workaround.
13 changes: 12 additions & 1 deletion lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class MaplibreMap extends StatefulWidget {
AnnotationType.line,
AnnotationType.circle,
],
this.useDelayedDisposal,
this.useHybridCompositionOverride,
}) : assert(annotationOrder.length <= 4),
assert(annotationConsumeTapEvents.length > 0),
super(key: key);
Expand Down Expand Up @@ -211,6 +213,13 @@ class MaplibreMap extends StatefulWidget {
/// * All fade/transition animations have completed
final OnMapIdleCallback? onMapIdle;

/// Use delayed disposal of Android View Controller to avoid flutter 3.x.x crashes
/// Use with caution - this is not yet production ready since several users still report crashes after using this workaround
final bool? useDelayedDisposal;

/// Override hybrid mode per map instance
final bool? useHybridCompositionOverride;

/// Set `MapboxMap.useHybridComposition` to `false` in order use Virtual-Display
/// (better for Android 9 and below but may result in errors on Android 12)
/// or leave it `true` (default) to use Hybrid composition (Slower on Android 9 and below).
Expand Down Expand Up @@ -240,7 +249,9 @@ class _MaplibreMapState extends State<MaplibreMap> {
'initialCameraPosition': widget.initialCameraPosition.toMap(),
'options': _MapboxMapOptions.fromWidget(widget).toMap(),
//'onAttributionClickOverride': widget.onAttributionClick != null,
'dragEnabled': widget.dragEnabled
'dragEnabled': widget.dragEnabled,
'useDelayedDisposal': widget.useDelayedDisposal,
'useHybridCompositionOverride': widget.useHybridCompositionOverride,
};
return _mapboxGlPlatform.buildView(
creationParams, onPlatformViewCreated, widget.gestureRecognizers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

part 'src/view_wrappers.dart';
part 'src/annotation.dart';
part 'src/callbacks.dart';
part 'src/camera.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
OnPlatformViewCreatedCallback onPlatformViewCreated,
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers) {
if (defaultTargetPlatform == TargetPlatform.android) {
if (useHybridComposition) {
final useDelayedDisposalParam =
(creationParams['useDelayedDisposal'] ?? false) as bool;
final useHybridCompositionParam =
(creationParams['useHybridCompositionOverride'] ??
useHybridComposition) as bool;
if (useHybridCompositionParam) {
return PlatformViewLink(
viewType: 'plugins.flutter.io/mapbox_gl',
surfaceFactory: (
Expand All @@ -153,15 +158,26 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
final SurfaceAndroidViewController controller =
PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
late AndroidViewController controller;
if (useDelayedDisposalParam) {
controller = WrappedPlatformViewsService.initAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
} else {
controller = PlatformViewsService.initAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
}
controller.addOnPlatformViewCreatedListener(
params.onPlatformViewCreated,
);
Expand All @@ -174,6 +190,15 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
},
);
} else {
if (useDelayedDisposalParam) {
return AndroidViewWithWrappedController(
viewType: 'plugins.flutter.io/mapbox_gl',
onPlatformViewCreated: onPlatformViewCreated,
gestureRecognizers: gestureRecognizers,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
return AndroidView(
viewType: 'plugins.flutter.io/mapbox_gl',
onPlatformViewCreated: onPlatformViewCreated,
Expand Down
Loading

0 comments on commit 8e34b3d

Please sign in to comment.