diff --git a/lib/cards/map_card.dart b/lib/cards/map_card.dart index e38318a..1569040 100644 --- a/lib/cards/map_card.dart +++ b/lib/cards/map_card.dart @@ -11,62 +11,18 @@ class MapCard extends StatefulWidget { class _MapCardState extends State { - GlobalKey _mapKey = new GlobalKey(); - MapController mapController = MapController(); - @override Widget build(BuildContext context) { - List markers = []; - List 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, - ), - ) - ) - ); - } - }); + return CardWrapper( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ CardHeader(name: widget.card.title), - AspectRatio( + EntitiesMap( 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, - ), - ], - ) - ), + entities: widget.card.entities, ) ], ) diff --git a/lib/cards/widgets/entities_map.dart b/lib/cards/widgets/entities_map.dart new file mode 100644 index 0000000..f07b4d4 --- /dev/null +++ b/lib/cards/widgets/entities_map.dart @@ -0,0 +1,62 @@ +part of '../../main.dart'; + + +class EntitiesMap extends StatelessWidget { + + final List entities; + final bool interactive; + final double aspectRatio; + + const EntitiesMap({Key key, this.entities: const [], this.aspectRatio, this.interactive: false}) : super(key: key); + + @override + Widget build(BuildContext context) { + List markers = []; + List points = []; + 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, + ), + ) + ) + ); + } + }); + Widget map = FlutterMap( + options: new MapOptions( + interactive: false, + bounds: LatLngBounds.fromPoints(points), + boundsOptions: FitBoundsOptions(padding: EdgeInsets.all(40)), + ), + layers: [ + new TileLayerOptions( + urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", + subdomains: ['a', 'b', 'c'] + ), + new MarkerLayerOptions( + markers: markers, + ), + ], + ); + if (aspectRatio != null) { + return AspectRatio( + aspectRatio: aspectRatio, + child: map + ); + } + return map; + } + +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 790e76b..92d0e13 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -29,6 +29,7 @@ import 'package:syncfusion_flutter_core/core.dart'; import 'package:syncfusion_flutter_gauges/gauges.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong/latlong.dart'; +import 'package:flutter/gestures.dart'; import 'utils/logger.dart'; import '.secrets.dart'; @@ -48,6 +49,7 @@ part 'entities/date_time/date_time_entity.class.dart'; part 'entities/light/light_entity.class.dart'; part 'entities/select/select_entity.class.dart'; part 'entities/sun/sun_entity.class.dart'; +part 'cards/widgets/entities_map.dart'; part 'entities/sensor/sensor_entity.class.dart'; part 'entities/slider/slider_entity.dart'; part 'entities/media_player/media_player_entity.class.dart';