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/widgets/gauge_card_body.dart

177 lines
5.8 KiB
Dart
Raw Normal View History

2019-09-07 15:47:09 +03:00
part of '../../main.dart';
2020-04-14 21:39:21 +03:00
class GaugeCardBody extends StatelessWidget {
2019-09-07 15:47:09 +03:00
final int min;
final int max;
final Map severity;
2019-09-07 17:04:40 +03:00
GaugeCardBody({Key key, this.min, this.max, this.severity}) : super(key: key);
2019-09-07 15:47:09 +03:00
2020-04-07 23:20:57 +03:00
@override
Widget build(BuildContext context) {
EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper;
2019-09-07 15:47:09 +03:00
double fixedValue;
2020-04-07 23:20:57 +03:00
double value = entityWrapper.entity.doubleState;
2020-04-14 21:39:21 +03:00
if (value > max) {
fixedValue = max.toDouble();
} else if (value < min) {
fixedValue = min.toDouble();
2019-09-07 15:47:09 +03:00
} else {
fixedValue = value;
}
2020-04-07 23:20:57 +03:00
List<GaugeRange> ranges;
2020-04-13 17:01:17 +03:00
Color currentColor;
2020-04-14 21:39:21 +03:00
if (severity != null && severity["green"] is int && severity["red"] is int && severity["yellow"] is int) {
2020-04-07 23:20:57 +03:00
List<RangeContainer> rangesList = <RangeContainer>[
2020-04-14 21:39:21 +03:00
RangeContainer(severity["green"], HAClientTheme().getGreenGaugeColor()),
RangeContainer(severity["red"], HAClientTheme().getRedGaugeColor()),
RangeContainer(severity["yellow"], HAClientTheme().getYellowGaugeColor())
2020-04-07 23:20:57 +03:00
];
rangesList.sort((current, next) {
if (current.startFrom > next.startFrom) {
return 1;
}
if (current.startFrom < next.startFrom) {
return -1;
}
return 0;
});
2020-04-13 17:01:17 +03:00
if (fixedValue < rangesList[1].startFrom) {
currentColor = rangesList[0].color;
} else if (fixedValue < rangesList[2].startFrom && fixedValue >= rangesList[1].startFrom) {
currentColor = rangesList[1].color;
} else {
currentColor = rangesList[2].color;
}
2020-04-07 23:20:57 +03:00
ranges = [
GaugeRange(
startValue: rangesList[0].startFrom.toDouble(),
endValue: rangesList[1].startFrom.toDouble(),
2020-04-13 17:01:17 +03:00
color: rangesList[0].color.withOpacity(0.1),
2020-04-07 23:20:57 +03:00
sizeUnit: GaugeSizeUnit.factor,
endWidth: 0.3,
startWidth: 0.3
),
GaugeRange(
startValue: rangesList[1].startFrom.toDouble(),
endValue: rangesList[2].startFrom.toDouble(),
2020-04-13 17:01:17 +03:00
color: rangesList[1].color.withOpacity(0.1),
2020-04-07 23:20:57 +03:00
sizeUnit: GaugeSizeUnit.factor,
endWidth: 0.3,
startWidth: 0.3
),
GaugeRange(
startValue: rangesList[2].startFrom.toDouble(),
2020-04-14 21:39:21 +03:00
endValue: max.toDouble(),
2020-04-13 17:01:17 +03:00
color: rangesList[2].color.withOpacity(0.1),
2020-04-07 23:20:57 +03:00
sizeUnit: GaugeSizeUnit.factor,
endWidth: 0.3,
startWidth: 0.3
)
];
}
if (ranges == null) {
2020-04-13 17:01:17 +03:00
currentColor = Theme.of(context).primaryColorDark;
2020-04-07 23:20:57 +03:00
ranges = <GaugeRange>[
GaugeRange(
2020-04-14 21:39:21 +03:00
startValue: min.toDouble(),
endValue: max.toDouble(),
2020-04-13 17:01:17 +03:00
color: Theme.of(context).primaryColorDark.withOpacity(0.1),
2020-04-07 23:20:57 +03:00
sizeUnit: GaugeSizeUnit.factor,
endWidth: 0.3,
2020-04-13 17:01:17 +03:00
startWidth: 0.3,
2020-04-07 23:20:57 +03:00
)
];
2019-09-07 15:47:09 +03:00
}
return InkWell(
onTap: () => entityWrapper.handleTap(),
onLongPress: () => entityWrapper.handleHold(),
2020-03-14 20:12:11 +02:00
onDoubleTap: () => entityWrapper.handleDoubleTap(),
2019-09-07 15:47:09 +03:00
child: AspectRatio(
2020-04-07 23:20:57 +03:00
aspectRatio: 2,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double fontSizeFactor;
if (constraints.maxWidth > 300.0) {
fontSizeFactor = 1.6;
} else if (constraints.maxWidth > 150.0) {
fontSizeFactor = 1;
} else if (constraints.maxWidth > 100.0) {
fontSizeFactor = 0.6;
} else {
fontSizeFactor = 0.4;
}
return SfRadialGauge(
axes: <RadialAxis>[
RadialAxis(
2020-04-14 21:39:21 +03:00
maximum: max.toDouble(),
minimum: min.toDouble(),
2020-04-07 23:20:57 +03:00
showLabels: false,
2020-04-13 17:01:17 +03:00
useRangeColorForAxis: true,
2020-04-07 23:20:57 +03:00
showTicks: false,
canScaleToFit: true,
ranges: ranges,
2020-04-13 17:01:17 +03:00
axisLineStyle: AxisLineStyle(
thickness: 0.3,
thicknessUnit: GaugeSizeUnit.factor,
color: Colors.transparent
),
2020-04-07 23:20:57 +03:00
annotations: <GaugeAnnotation>[
GaugeAnnotation(
angle: -90,
positionFactor: 1.3,
//verticalAlignment: GaugeAlignment.far,
widget: EntityName(
textStyle: Theme.of(context).textTheme.body1.copyWith(
fontSize: Theme.of(context).textTheme.body1.fontSize * fontSizeFactor
2019-09-07 15:47:09 +03:00
),
2020-04-07 23:20:57 +03:00
),
),
GaugeAnnotation(
angle: 180,
positionFactor: 0,
verticalAlignment: GaugeAlignment.center,
widget: SimpleEntityState(
expanded: false,
maxLines: 1,
textAlign: TextAlign.center,
textStyle: Theme.of(context).textTheme.title.copyWith(
fontSize: Theme.of(context).textTheme.title.fontSize * fontSizeFactor,
),
),
)
],
startAngle: 180,
endAngle: 0,
pointers: <GaugePointer>[
2020-04-13 17:01:17 +03:00
RangePointer(
2020-04-07 23:20:57 +03:00
value: fixedValue,
2020-04-13 17:01:17 +03:00
sizeUnit: GaugeSizeUnit.factor,
width: 0.3,
color: currentColor,
2020-04-07 23:20:57 +03:00
enableAnimation: true,
animationType: AnimationType.bounceOut,
)
]
2019-09-07 15:47:09 +03:00
)
2020-04-07 23:20:57 +03:00
],
);
},
),
2019-09-07 15:47:09 +03:00
),
);
}
2020-04-14 21:39:21 +03:00
2019-09-07 15:47:09 +03:00
}
2020-04-07 23:20:57 +03:00
class RangeContainer {
final int startFrom;
Color color;
2019-09-07 15:47:09 +03:00
2020-04-07 23:20:57 +03:00
RangeContainer(this.startFrom, this.color);
2019-09-07 15:47:09 +03:00
}