InfoWindow Size自适应便宜量

This commit is contained in:
Kuloud 2024-01-05 09:27:48 +08:00
parent 4b1a1326e8
commit 36518f3757
4 changed files with 21 additions and 6 deletions

View File

@ -28,7 +28,8 @@ class _State extends State<CustomInfoWindowDemoPage> {
color: Colors.lightBlue.shade400,
child: Text('info'),
),
option: InfoWindowOption(latLng: mapCenter));
option: InfoWindowOption(
latLng: mapCenter, offset: EdgeInsets.only(bottom: 32)));
Future<void> _onMapCreated(AMapController controller) async {}

View File

@ -58,6 +58,7 @@ class _AMapLoaderState extends State<AMapLoader> {
buildContext: context,
currentStep: CurrentStep.preparing,
loader: widget);
widget.prepareFromExtension(aMapContext);
super.didChangeDependencies();
}

View File

@ -234,7 +234,6 @@ class _MapState extends State<AMapWidget> {
if (_extensions.isNotEmpty) {
debugPrint('[onPlatformViewCreated] $controller');
await Future.forEach(_extensions.values, (e) {
debugPrint('[onPlatformViewCreated] controller: ${controller.mapId}');
e.bindMethodChannel(controller.channel);
e.bindMapController(controller);
});

View File

@ -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<InfoWindowExtension> _streamController =
StreamController<InfoWindowExtension>.broadcast();
final _streamController = StreamController<InfoWindowExtension>.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<InfoWindowExtension>(
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 {