Map card WIP

This commit is contained in:
estevez-dev 2020-07-20 13:53:18 +03:00
parent bf7983d72e
commit d2d037e468
7 changed files with 118 additions and 17 deletions

View File

@ -21,7 +21,6 @@ class CardData {
switch (rawData['type']) {
case CardType.ENTITIES:
case CardType.HISTORY_GRAPH:
case CardType.MAP:
case CardType.PICTURE_GLANCE:
case CardType.SENSOR:
case CardType.ENTITY:
@ -47,6 +46,9 @@ class CardData {
return CardData(null);
}
break;
case CardType.MAP:
return MapCardData(rawData);
break;
case CardType.ENTITY_BUTTON:
case CardType.BUTTON:
case CardType.PICTURE_ENTITY:
@ -656,7 +658,7 @@ class MapCardData extends CardData {
MapCardData(rawData) : super(rawData) {
//Parsing card data
title = rawData['title'];
List<String> geoLocationSources = rawData['geo_location_sources'] ?? [];
List<dynamic> geoLocationSources = rawData['geo_location_sources'] ?? [];
if (geoLocationSources.isNotEmpty) {
//TODO add entities by source
}
@ -664,8 +666,25 @@ class MapCardData extends CardData {
rawEntities.forEach((rawEntity) {
if (rawEntity is String) {
if (HomeAssistant().entities.isExist(rawEntity)) {
entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawEntity)));
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity)));
}
} else {
if (HomeAssistant().entities.isExist(rawEntity["entity"])) {
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
entities.add(
EntityWrapper(entity: HomeAssistant().entities.get(rawEntity)));
EntityWrapper(
entity: e,
stateColor: stateColor,
overrideName: rawEntity["name"]?.toString(),
overrideIcon: rawEntity["icon"],
stateFilter: rawEntity['state_filter'] ?? [],
uiAction: EntityUIAction(rawEntityData: rawEntity)
)
);
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity["entity"])));
}
}
});

View File

@ -1,25 +1,75 @@
part of '../main.dart';
class MapCard extends StatelessWidget {
class MapCard extends StatefulWidget {
final MapCardData card;
const MapCard({Key key, this.card}) : super(key: key);
@override
_MapCardState createState() => _MapCardState();
}
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: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CardHeader(name: card.title)
],
),
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,
),
],
)
),
)
],
)
);
}
}

View File

@ -27,12 +27,15 @@ import 'package:flutter_webview_plugin/flutter_webview_plugin.dart' as standalon
import 'package:webview_flutter/webview_flutter.dart';
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 'utils/logger.dart';
import '.secrets.dart';
part 'const.dart';
part 'utils/launcher.dart';
part 'utils/RandomColorGenerator.dart';
part 'entities/entity.class.dart';
part 'entities/entity_wrapper.class.dart';
part 'entities/timer/timer_entity.class.dart';

View File

@ -408,7 +408,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
int currentViewCount = HomeAssistant().ui?.views?.length ?? 0;
if (_previousViewCount != currentViewCount) {
Logger.d("Views count changed ($_previousViewCount->$currentViewCount). Creating new tabs controller.");
//Logger.d("Views count changed ($_previousViewCount->$currentViewCount). Creating new tabs controller.");
_viewsTabController = TabController(vsync: this, length: currentViewCount);
_previousViewCount = currentViewCount;
}

View File

@ -0,0 +1,28 @@
part of '../main.dart';
class RandomColorGenerator {
static const colorsList = [
Colors.green,
Colors.purple,
Colors.indigo,
Colors.red,
Colors.orange,
Colors.cyan
];
int _index = 0;
Color getCurrent() {
return colorsList[_index];
}
Color getNext() {
if (_index < colorsList.length - 1) {
_index += 1;
} else {
_index = 1;
}
return getCurrent();
}
}

View File

@ -74,7 +74,7 @@ class HAView {
Widget build(BuildContext context) {
return ViewWidget(
view: this,
view: this
);
}
}

View File

@ -29,6 +29,7 @@ dependencies:
firebase_crashlytics: ^0.1.3+3
syncfusion_flutter_core: ^18.2.44
syncfusion_flutter_gauges: ^18.2.44
flutter_map: ^0.10.1
dev_dependencies: