Create EntitiesMap widget
This commit is contained in:
parent
9b0b90dba1
commit
f7d8bf9ae7
@ -11,62 +11,18 @@ class MapCard extends StatefulWidget {
|
||||
|
||||
class _MapCardState extends State<MapCard> {
|
||||
|
||||
GlobalKey _mapKey = new GlobalKey();
|
||||
MapController mapController = MapController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
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,
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return CardWrapper(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
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,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
62
lib/cards/widgets/entities_map.dart
Normal file
62
lib/cards/widgets/entities_map.dart
Normal file
@ -0,0 +1,62 @@
|
||||
part of '../../main.dart';
|
||||
|
||||
|
||||
class EntitiesMap extends StatelessWidget {
|
||||
|
||||
final List<EntityWrapper> 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<Marker> markers = [];
|
||||
List<LatLng> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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';
|
||||
|
Reference in New Issue
Block a user