This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/cards/map_card.dart

75 lines
2.2 KiB
Dart
Raw Normal View History

2020-07-08 20:21:34 +03:00
part of '../main.dart';
2020-07-20 13:53:18 +03:00
class MapCard extends StatefulWidget {
2020-07-08 20:21:34 +03:00
final MapCardData card;
const MapCard({Key key, this.card}) : super(key: key);
2020-07-20 13:53:18 +03:00
@override
_MapCardState createState() => _MapCardState();
}
class _MapCardState extends State<MapCard> {
GlobalKey _mapKey = new GlobalKey();
MapController mapController = MapController();
2020-07-08 20:21:34 +03:00
@override
Widget build(BuildContext context) {
2020-07-20 13:53:18 +03:00
List<Marker> markers = [];
List<LatLng> points = [];
widget.card.entities.forEach((entityWrapper) {
double lat = entityWrapper.entity._getDoubleAttributeValue("latitude");
double long = entityWrapper.entity._getDoubleAttributeValue("longitude");
if (lat != null && long != null) {
points.add(LatLng(lat, long));
markers.add(
Marker(
width: 36,
height: 36,
point: LatLng(lat, long),
builder: (ctx) => EntityModel(
handleTap: true,
entityWrapper: entityWrapper,
child: EntityIcon(
size: 36,
),
)
)
);
}
});
2020-07-08 20:21:34 +03:00
return CardWrapper(
2020-07-20 13:53:18 +03:00
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CardHeader(name: widget.card.title),
AspectRatio(
aspectRatio: 1,
child: GestureDetector(
child: FlutterMap(
key: _mapKey,
mapController: mapController,
options: new MapOptions(
interactive: true,
bounds: LatLngBounds.fromPoints(points),
boundsOptions: FitBoundsOptions(padding: EdgeInsets.all(30)),
),
layers: [
new TileLayerOptions(
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c']
),
new MarkerLayerOptions(
markers: markers,
),
],
)
),
)
],
2020-07-08 20:21:34 +03:00
)
);
}
}