From e4d1a4f823dba95f42dbd9add0a2c97aa1abc247 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Thu, 14 May 2020 15:38:22 +0000 Subject: [PATCH] Resolves #212 --- lib/cards/card.class.dart | 2 +- lib/cards/light_card.dart | 186 +++++++++++++++++++-------- lib/entities/entity_icon.widget.dart | 5 +- pubspec.yaml | 4 +- 4 files changed, 136 insertions(+), 61 deletions(-) diff --git a/lib/cards/card.class.dart b/lib/cards/card.class.dart index 1fdfc82..918e462 100644 --- a/lib/cards/card.class.dart +++ b/lib/cards/card.class.dart @@ -380,7 +380,7 @@ class LightCardData extends CardData { entity: HomeAssistant().entities.get(entitiId), overrideName: name, overrideIcon: icon, - uiAction: EntityUIAction() + uiAction: EntityUIAction()..tapAction = EntityUIAction.toggle )); } else { entities.add(EntityWrapper(entity: Entity.missed(entitiId))); diff --git a/lib/cards/light_card.dart b/lib/cards/light_card.dart index 0f403a3..66d8ae9 100644 --- a/lib/cards/light_card.dart +++ b/lib/cards/light_card.dart @@ -1,85 +1,159 @@ part of '../main.dart'; -class LightCard extends StatelessWidget { +class LightCard extends StatefulWidget { final LightCardData card; LightCard({Key key, this.card}) : super(key: key); + @override + State createState() { + return _LightCardState(); + } +} + +class _LightCardState extends State { + + double _newBrightness; + double _actualBrightness; + bool _changedHere = false; + + @override + void initState() { + super.initState(); + } + + void _setBrightness(double value, LightEntity entity) { + setState((){ + _newBrightness = value; + _changedHere = true; + }); + ConnectionManager().callService( + domain: entity.domain, + service: "turn_on", + entityId: entity.entityId, + data: {"brightness": value.round()} + ); + } + @override Widget build(BuildContext context) { - EntityWrapper entityWrapper = card.entity; + EntityWrapper entityWrapper = widget.card.entity; LightEntity entity = entityWrapper.entity; if (entityWrapper.entity.statelessType == StatelessEntityType.missed) { return EntityModel( - entityWrapper: card.entity, + entityWrapper: widget.card.entity, child: MissedEntityWidget(), handleTap: false, ); } - entityWrapper.overrideName = card.name ?? + entityWrapper.overrideName = widget.card.name ?? entityWrapper.displayName; - entityWrapper.overrideIcon = card.icon ?? + entityWrapper.overrideIcon = widget.card.icon ?? entityWrapper.icon; - double value = (entity.brightness ?? 0).toDouble(); - + _actualBrightness = (entity.brightness ?? 0).toDouble(); + if (!_changedHere) { + _newBrightness = _actualBrightness; + } else { + _changedHere = false; + } + Color lightColor = entity.color?.toColor(); + Color color; + if (lightColor != null && lightColor != Colors.white) { + color = lightColor; + } else { + color = HAClientTheme().getOnStateColor(context); + } return CardWrapper( padding: EdgeInsets.all(4), child: EntityModel( entityWrapper: entityWrapper, - child: AspectRatio( - aspectRatio: 1.8, - child: Stack( - alignment: Alignment.center, - children: [ - SfRadialGauge( - axes: [ - RadialAxis( - maximum: 255, - minimum: 0, - showLabels: false, - showTicks: false, - canScaleToFit: true, - axisLineStyle: AxisLineStyle( - thickness: 0.05, - thicknessUnit: GaugeSizeUnit.factor, - color: HAClientTheme().getDisabledStateColor(context) + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + ConstrainedBox( + constraints: BoxConstraints.loose(Size(200, 200)), + child: AspectRatio( + aspectRatio: 1, + child: Stack( + alignment: Alignment.center, + children: [ + SfRadialGauge( + axes: [ + RadialAxis( + onAxisTapped: (val) { + _setBrightness(val, entity); + }, + maximum: 255, + minimum: 0, + showLabels: false, + showTicks: false, + axisLineStyle: AxisLineStyle( + thickness: 0.05, + thicknessUnit: GaugeSizeUnit.factor, + color: HAClientTheme().getDisabledStateColor(context) + ), + pointers: [ + RangePointer( + value: _actualBrightness, + sizeUnit: GaugeSizeUnit.factor, + width: 0.05, + color: color, + enableAnimation: true, + animationType: AnimationType.bounceOut, + ), + MarkerPointer( + value: _newBrightness, + markerType: MarkerType.circle, + markerHeight: 20, + markerWidth: 20, + enableDragging: true, + onValueChangeEnd: (val) { + _setBrightness(val, entity); + }, + color: HAClientTheme().getColorByEntityState(entity.state, context) + //enableAnimation: true, + //animationType: AnimationType.bounceOut, + ) + ] + ) + ], ), - pointers: [ - /*RangePointer( - value: value, - sizeUnit: GaugeSizeUnit.factor, - width: 0.05, - color: HAClientTheme().getOnStateColor(context), - enableAnimation: true, - animationType: AnimationType.bounceOut, - ),*/ - MarkerPointer( - value: value, - markerType: MarkerType.circle, - markerHeight: 20, - markerWidth: 20, - enableDragging: true, - onValueChangeStart: (_) { - Logger.d('Value change start'); - }, - onValueChanging: (args) { - Logger.d('Value changing: ${args.value}'); - }, - color: HAClientTheme().getOnStateColor(context), - enableAnimation: true, - animationType: AnimationType.bounceOut, + FractionallySizedBox( + heightFactor: 0.4, + widthFactor: 0.4, + child: AspectRatio( + aspectRatio: 1, + child: InkResponse( + onTap: () => entityWrapper.handleTap(), + onLongPress: () => entityWrapper.handleHold(), + child: FittedBox( + fit: BoxFit.contain, + child: EntityIcon( + showBadge: false, + padding: EdgeInsets.all(0) + ) + ) + ) ) - ] - ) - ], - ), - ], - ) + ) + ], + ) + ) + ), + EntityName( + padding: EdgeInsets.all(0), + wordsWrap: true, + maxLines: 3, + textOverflow: TextOverflow.ellipsis, + textAlign: TextAlign.center, + ) + ], ), handleTap: true ) ); } - -} \ No newline at end of file +} + \ No newline at end of file diff --git a/lib/entities/entity_icon.widget.dart b/lib/entities/entity_icon.widget.dart index a0891e3..6ee6309 100644 --- a/lib/entities/entity_icon.widget.dart +++ b/lib/entities/entity_icon.widget.dart @@ -7,8 +7,9 @@ class EntityIcon extends StatelessWidget { final EdgeInsetsGeometry imagePadding; final double size; final Color color; + final bool showBadge; - const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.all(0.0), this.iconPadding, this.imagePadding}) : super(key: key); + const EntityIcon({Key key, this.color, this.showBadge: true, this.size: Sizes.iconSize, this.padding: const EdgeInsets.all(0.0), this.iconPadding, this.imagePadding}) : super(key: key); int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { if (entityId == null) { @@ -71,7 +72,7 @@ class EntityIcon extends StatelessWidget { iconCode = getDefaultIconByEntityId(entityWrapper.entity.entityId, entityWrapper.entity.deviceClass, entityWrapper.entity.state); // } - if (entityWrapper.entity is LightEntity && + if (showBadge && entityWrapper.entity is LightEntity && (entityWrapper.entity as LightEntity).supportColor && (entityWrapper.entity as LightEntity).color != null && (entityWrapper.entity as LightEntity).color.toColor() != Colors.white diff --git a/pubspec.yaml b/pubspec.yaml index 62cdd7e..522eb7d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,8 +31,8 @@ dependencies: workmanager: ^0.2.2 battery: ^1.0.0 firebase_crashlytics: ^0.1.3+3 - syncfusion_flutter_core: ^18.1.48 - syncfusion_flutter_gauges: ^18.1.48 + syncfusion_flutter_core: ^18.1.52 + syncfusion_flutter_gauges: ^18.1.52 dev_dependencies: