|
|
@@ -1,3 +1,4 @@
|
|
|
+import 'dart:async';
|
|
|
import 'dart:convert';
|
|
|
|
|
|
import 'package:amap_location_example/map_util.dart';
|
|
|
@@ -13,7 +14,7 @@ class MapViewPage extends StatefulWidget {
|
|
|
}
|
|
|
|
|
|
class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
- final AmapViewController controller = AmapViewController();
|
|
|
+ final Completer<AmapViewController> _controller = Completer<AmapViewController>();
|
|
|
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
@@ -23,8 +24,7 @@ class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
height: double.maxFinite,
|
|
|
width: double.maxFinite,
|
|
|
child: AmapView(
|
|
|
- controller: controller,
|
|
|
- onViewCreated: _onPlatformCreate,
|
|
|
+ onPlatformViewCreated: _onPlatformCreate,
|
|
|
),
|
|
|
),
|
|
|
);
|
|
|
@@ -39,13 +39,20 @@ class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
@override
|
|
|
void dispose() {
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
- controller.dispose();
|
|
|
+ _dispose();
|
|
|
super.dispose();
|
|
|
}
|
|
|
|
|
|
@override
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
super.didChangeAppLifecycleState(state);
|
|
|
+ onStateChange(state);
|
|
|
+ }
|
|
|
+
|
|
|
+ void onStateChange(AppLifecycleState state) async {
|
|
|
+ if (!_controller.isCompleted)
|
|
|
+ return;
|
|
|
+ AmapViewController controller = await _controller.future;
|
|
|
if (state == AppLifecycleState.paused) {
|
|
|
controller.onPauseMapView();
|
|
|
} else if (state == AppLifecycleState.resumed) {
|
|
|
@@ -53,7 +60,7 @@ class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void initCityData() async {
|
|
|
+ void _initCityData(AmapViewController controller) async {
|
|
|
// kingWay 的环境
|
|
|
var httpClient = new HttpClient();
|
|
|
HttpClientRequest request = await httpClient
|
|
|
@@ -72,10 +79,11 @@ class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
}).toList());
|
|
|
}
|
|
|
|
|
|
- void _onPlatformCreate() {
|
|
|
+ void _onPlatformCreate(AmapViewController controller) {
|
|
|
print("on map view create");
|
|
|
+ _controller.complete(controller);
|
|
|
controller.onCreateMapView();
|
|
|
- initCityData();
|
|
|
+ _initCityData(controller);
|
|
|
controller.guideStream.listen((m) {
|
|
|
showModalBottomSheet(
|
|
|
context: context,
|
|
|
@@ -197,6 +205,13 @@ class _MapViewPageState extends State<MapViewPage> with WidgetsBindingObserver {
|
|
|
}
|
|
|
print("jump ${success ? "successful" : "failed"}");
|
|
|
}
|
|
|
+
|
|
|
+ void _dispose() async {
|
|
|
+ if (!_controller.isCompleted)
|
|
|
+ return;
|
|
|
+ AmapViewController controller = await _controller.future;
|
|
|
+ controller.dispose();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
enum MapType {
|