From 36518f37579f5d8547f07900a51577869edafe19 Mon Sep 17 00:00:00 2001 From: Kuloud Date: Fri, 5 Jan 2024 09:27:48 +0800 Subject: [PATCH] =?UTF-8?q?InfoWindow=20Size=E8=87=AA=E9=80=82=E5=BA=94?= =?UTF-8?q?=E4=BE=BF=E5=AE=9C=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/overlays/custom_info_window.dart | 3 ++- lib/src/amap_loader.dart | 1 + lib/src/amap_widget.dart | 1 - .../buildin/info_window_extension.dart | 22 +++++++++++++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/example/lib/pages/overlays/custom_info_window.dart b/example/lib/pages/overlays/custom_info_window.dart index aa2b602..8f0871d 100644 --- a/example/lib/pages/overlays/custom_info_window.dart +++ b/example/lib/pages/overlays/custom_info_window.dart @@ -28,7 +28,8 @@ class _State extends State { color: Colors.lightBlue.shade400, child: Text('info'), ), - option: InfoWindowOption(latLng: mapCenter)); + option: InfoWindowOption( + latLng: mapCenter, offset: EdgeInsets.only(bottom: 32))); Future _onMapCreated(AMapController controller) async {} diff --git a/lib/src/amap_loader.dart b/lib/src/amap_loader.dart index 41b33d1..5aa1593 100644 --- a/lib/src/amap_loader.dart +++ b/lib/src/amap_loader.dart @@ -58,6 +58,7 @@ class _AMapLoaderState extends State { buildContext: context, currentStep: CurrentStep.preparing, loader: widget); + widget.prepareFromExtension(aMapContext); super.didChangeDependencies(); } diff --git a/lib/src/amap_widget.dart b/lib/src/amap_widget.dart index e9625f9..b51cf83 100644 --- a/lib/src/amap_widget.dart +++ b/lib/src/amap_widget.dart @@ -234,7 +234,6 @@ class _MapState extends State { if (_extensions.isNotEmpty) { debugPrint('[onPlatformViewCreated] $controller'); await Future.forEach(_extensions.values, (e) { - debugPrint('[onPlatformViewCreated] controller: ${controller.mapId}'); e.bindMethodChannel(controller.channel); e.bindMapController(controller); }); diff --git a/lib/src/extension/buildin/info_window_extension.dart b/lib/src/extension/buildin/info_window_extension.dart index 8f17300..c33aec1 100644 --- a/lib/src/extension/buildin/info_window_extension.dart +++ b/lib/src/extension/buildin/info_window_extension.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'package:amap_map/amap_map.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:x_amap_base/x_amap_base.dart'; /// [amap_map]插件支持默认的[InfoWindow]展示[title]和[snippet]字段,如果要自定义 @@ -37,12 +37,13 @@ import 'package:x_amap_base/x_amap_base.dart'; class InfoWindowExtension extends AMapExtension { InfoWindowExtension({required this.infoWindow, this.option}); - StreamController _streamController = - StreamController.broadcast(); + final _streamController = StreamController.broadcast(); final Widget infoWindow; InfoWindowOption? option; + final GlobalKey _infoWindowKey = GlobalKey(); + AMapController? mapController; double? _x; double? _y; @@ -67,6 +68,12 @@ class InfoWindowExtension extends AMapExtension { _x = coordinate.x.toDouble(); _y = coordinate.y.toDouble(); + final size = _infoWindowKey.currentContext?.size; + if (size != null && _x != null && _y != null) { + _x = _x! - size.width / 2; + _y = _y! - size.height; + } + _streamController.add(this); } @@ -79,10 +86,10 @@ class InfoWindowExtension extends AMapExtension { @override Widget build(AMapContext aMapContext, Widget child) { return Stack( + key: ValueKey(this), children: [ child, StreamBuilder( - key: ValueKey(this), initialData: this, stream: _streamController.stream, builder: (context, snapshot) { @@ -97,6 +104,7 @@ class InfoWindowExtension extends AMapExtension { top: data._y, left: data._x, child: Container( + key: _infoWindowKey, margin: data.option?.offset, child: infoWindow, ), @@ -105,6 +113,12 @@ class InfoWindowExtension extends AMapExtension { ], ); } + + @override + void onDispose() { + _streamController.close(); + super.onDispose(); + } } class InfoWindowOption {