From 44e65dc84fc16604d7e957a225263499adbbeb8d Mon Sep 17 00:00:00 2001 From: Minyewoo Date: Tue, 20 Jun 2023 19:43:17 +0300 Subject: [PATCH] SmallLinearValueIndicator | Added alarm boundaries --- .../pointer_progress_indicator.dart | 11 ++--- .../small_linear_value_indicator.dart | 41 +++++++++++++++---- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/src/indicators/value_indicators/pointer_progress_indicator.dart b/lib/src/indicators/value_indicators/pointer_progress_indicator.dart index dc83d7d..caf0b73 100644 --- a/lib/src/indicators/value_indicators/pointer_progress_indicator.dart +++ b/lib/src/indicators/value_indicators/pointer_progress_indicator.dart @@ -1,20 +1,22 @@ import 'package:flutter/material.dart'; -import 'package:hmi_widgets/hmi_widgets.dart'; /// class PointerProgressIndicator extends StatelessWidget { final double _value; final double _minHeight; final double _pointerThickness; final double _scaleLineThickness; + final Color? _color; /// const PointerProgressIndicator({ super.key, - double value = 0, required double minHeight, + double value = 0, + Color? color, double pointerThickness = 4, double scaleLineThickness = 0.25, }) : - _value = value, + _value = value, + _color = color, _minHeight = minHeight, _pointerThickness = pointerThickness, _scaleLineThickness = scaleLineThickness; @@ -23,8 +25,7 @@ class PointerProgressIndicator extends StatelessWidget { Widget build(BuildContext context) { final theme = Theme.of(context); final defaultColor = theme.colorScheme.onBackground; - final blockedColor = theme.stateColors.alarmLowLevel; - final currentColor = (_value == 0 || _value == 1) ? blockedColor : defaultColor; + final currentColor = _color ?? defaultColor; return SizedBox( height: _minHeight, child: Stack( diff --git a/lib/src/indicators/value_indicators/small_linear_value_indicator.dart b/lib/src/indicators/value_indicators/small_linear_value_indicator.dart index e9501db..3cf0218 100644 --- a/lib/src/indicators/value_indicators/small_linear_value_indicator.dart +++ b/lib/src/indicators/value_indicators/small_linear_value_indicator.dart @@ -17,25 +17,34 @@ class SmallLinearValueIndicator extends StatelessWidget { final double _textIndicatorWidth; final double _min; final double _max; + final double? _high; + final double? _low; final Stream> _stream; final IndicationStyle _indicationStyle; + final Color? _defaultColor; /// const SmallLinearValueIndicator({ super.key, required Stream> stream, required double max, double min = 0.0, + double? high, + double? low, Widget? caption, String valueUnit = '', double textIndicatorWidth = 55, IndicationStyle indicationStyle = IndicationStyle.pointer, + Color? defaultColor, }) : _indicationStyle = indicationStyle, _stream = stream, _min = min, _max = max, + _high = high, + _low = low, _caption = caption, _valueUnit = valueUnit, - _textIndicatorWidth = textIndicatorWidth; + _textIndicatorWidth = textIndicatorWidth, + _defaultColor = defaultColor; // @override Widget build(BuildContext context) { @@ -43,6 +52,8 @@ class SmallLinearValueIndicator extends StatelessWidget { final caption = _caption; final padding = const Setting('padding').toDouble; final smallPadding = const Setting('smallPadding').toDouble; + final theme = Theme.of(context); + final alarmColor = theme.stateColors.alarm; return Card( margin: EdgeInsets.zero, child: Padding( @@ -68,21 +79,27 @@ class SmallLinearValueIndicator extends StatelessWidget { stream: _stream, builder:(context, snapshot) { final indicatorHeight = smallPadding * 3; - final snapshotValue = snapshot.data?.value.toDouble(); - final value = snapshotValue == null - ? 0.0 - : (max(snapshotValue, _min) - _min) / delta; + final snapshotValue = min( + max( + snapshot.data?.value.toDouble() ?? _min, + _min, + ), + _max, + ); + final percantage = (snapshotValue - _min) / delta; + final isAlarm = _isAlarm(snapshotValue); switch(_indicationStyle) { case IndicationStyle.bar: return LinearProgressIndicator( - value: value, + value: percantage, minHeight: indicatorHeight, - color: Theme.of(context).stateColors.on, + color: isAlarm ? alarmColor : _defaultColor, ); case IndicationStyle.pointer: return PointerProgressIndicator( - value: value, + value: percantage, minHeight: indicatorHeight, + color: isAlarm ? alarmColor : _defaultColor, ); } }, @@ -109,4 +126,12 @@ class SmallLinearValueIndicator extends StatelessWidget { ), ); } + + bool _isAlarm(double currentValue) { + final low = _low; + final high = _high; + final isAlarmLow = low != null && currentValue - low < 0; + final isAlarmHigh = high != null && currentValue - high > 0; + return isAlarmLow || isAlarmHigh; + } }