WIP #120 Random color for states
This commit is contained in:
		| @@ -1,41 +1,5 @@ | |||||||
| part of '../main.dart'; | part of '../main.dart'; | ||||||
|  |  | ||||||
| class EntityColors { |  | ||||||
|   static const _stateColors = { |  | ||||||
|     "on": Colors.amber, |  | ||||||
|     "auto": Colors.amber, |  | ||||||
|     "idle": Colors.amber, |  | ||||||
|     "playing": Colors.amber, |  | ||||||
|     "above_horizon": Colors.amber, |  | ||||||
|     "home":  Colors.amber, |  | ||||||
|     "open":  Colors.amber, |  | ||||||
|     "off": Color.fromRGBO(68, 115, 158, 1.0), |  | ||||||
|     "closed": Color.fromRGBO(68, 115, 158, 1.0), |  | ||||||
|     "default": Color.fromRGBO(68, 115, 158, 1.0), |  | ||||||
|     "heat": Colors.redAccent, |  | ||||||
|     "cool": Colors.lightBlue, |  | ||||||
|     "closing": Colors.cyan, |  | ||||||
|     "opening": Colors.purple, |  | ||||||
|     "unavailable": Colors.black26, |  | ||||||
|     "unknown": Colors.black26, |  | ||||||
|   }; |  | ||||||
|  |  | ||||||
|   static Color stateColor(String state) { |  | ||||||
|     return _stateColors[state] ?? _stateColors["default"]; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   static charts.Color historyStateColor(String state) { |  | ||||||
|     Color c = stateColor(state); |  | ||||||
|     return charts.Color( |  | ||||||
|       r: c.red, |  | ||||||
|       g: c.green, |  | ||||||
|       b: c.blue, |  | ||||||
|       a: c.alpha |  | ||||||
|     ); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
| } |  | ||||||
|  |  | ||||||
| class Entity { | class Entity { | ||||||
|  |  | ||||||
|   static const badgeColors = { |   static const badgeColors = { | ||||||
|   | |||||||
							
								
								
									
										56
									
								
								lib/entity_widgets/entity_colors.class.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								lib/entity_widgets/entity_colors.class.dart
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | part of '../main.dart'; | ||||||
|  |  | ||||||
|  | class EntityColors { | ||||||
|  |   static const _stateColors = { | ||||||
|  |     "on": Colors.amber, | ||||||
|  |     "auto": Colors.amber, | ||||||
|  |     "idle": Colors.amber, | ||||||
|  |     "playing": Colors.amber, | ||||||
|  |     "above_horizon": Colors.amber, | ||||||
|  |     "home":  Colors.amber, | ||||||
|  |     "open":  Colors.amber, | ||||||
|  |     "off": Color.fromRGBO(68, 115, 158, 1.0), | ||||||
|  |     "closed": Color.fromRGBO(68, 115, 158, 1.0), | ||||||
|  |     "below_horizon": Color.fromRGBO(68, 115, 158, 1.0), | ||||||
|  |     "default": Color.fromRGBO(68, 115, 158, 1.0), | ||||||
|  |     "heat": Colors.redAccent, | ||||||
|  |     "cool": Colors.lightBlue, | ||||||
|  |     "closing": Colors.cyan, | ||||||
|  |     "opening": Colors.purple, | ||||||
|  |     "unavailable": Colors.black26, | ||||||
|  |     "unknown": Colors.black26, | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   static Color stateColor(String state) { | ||||||
|  |     return _stateColors[state] ?? _stateColors["default"]; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   static charts.Color chartHistoryStateColor(String state, int id) { | ||||||
|  |     Color c = _stateColors[state]; | ||||||
|  |     if (c != null) { | ||||||
|  |       return charts.Color( | ||||||
|  |           r: c.red, | ||||||
|  |           g: c.green, | ||||||
|  |           b: c.blue, | ||||||
|  |           a: c.alpha | ||||||
|  |       ); | ||||||
|  |     } else { | ||||||
|  |       return charts.MaterialPalette.getOrderedPalettes(id+1)[id].shadeDefault; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   static Color historyStateColor(String state, int id) { | ||||||
|  |     Color c = _stateColors[state]; | ||||||
|  |     if (c != null) { | ||||||
|  |       return c; | ||||||
|  |     } else { | ||||||
|  |       if (id > -1) { | ||||||
|  |         charts.Color c1 = charts.MaterialPalette.getOrderedPalettes(id + 1)[id].shadeDefault; | ||||||
|  |         return Color.fromARGB(c1.a, c1.r, c1.g, c1.b); | ||||||
|  |       } else { | ||||||
|  |         return _stateColors["default"]; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -22,7 +22,6 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> { | |||||||
|  |  | ||||||
|   List _history; |   List _history; | ||||||
|   bool _needToUpdateHistory; |   bool _needToUpdateHistory; | ||||||
|   int _selectedId = -1; |  | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void initState() { |   void initState() { | ||||||
| @@ -66,7 +65,7 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> { | |||||||
|       ); |       ); | ||||||
|     } else if (_history.isEmpty) { |     } else if (_history.isEmpty) { | ||||||
|       children.add( |       children.add( | ||||||
|           Text("No history for last 24h") |           Text("No history") | ||||||
|       ); |       ); | ||||||
|     } else { |     } else { | ||||||
|       children.add( |       children.add( | ||||||
|   | |||||||
| @@ -36,6 +36,7 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar | |||||||
|           selectedState: "$selectedState", |           selectedState: "$selectedState", | ||||||
|           onPrevTap: () => _selectPrev(), |           onPrevTap: () => _selectPrev(), | ||||||
|           onNextTap: () => _selectNext(), |           onNextTap: () => _selectNext(), | ||||||
|  |           colorIndex: -1, | ||||||
|         ), |         ), | ||||||
|         SizedBox( |         SizedBox( | ||||||
|           height: 150.0, |           height: 150.0, | ||||||
| @@ -79,18 +80,21 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar | |||||||
|       data.add(NumericEntityStateHistoryMoment(double.tryParse(stateData["state"]), time, i)); |       data.add(NumericEntityStateHistoryMoment(double.tryParse(stateData["state"]), time, i)); | ||||||
|     } |     } | ||||||
|     data.add(NumericEntityStateHistoryMoment(data.last.value, now, widget.rawHistory.length)); |     data.add(NumericEntityStateHistoryMoment(data.last.value, now, widget.rawHistory.length)); | ||||||
|  |     if (_selectedId == -1) { | ||||||
|  |       _selectedId = 0; | ||||||
|  |     } | ||||||
|     return [ |     return [ | ||||||
|       new charts.Series<NumericEntityStateHistoryMoment, DateTime>( |       new charts.Series<NumericEntityStateHistoryMoment, DateTime>( | ||||||
|         id: 'State', |         id: 'State', | ||||||
|         colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.historyStateColor("unavailable"), |         colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("unavailable", historyMoment.id), | ||||||
|         domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time, |         domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time, | ||||||
|         measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value, |         measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value, | ||||||
|         data: data, |         data: data, | ||||||
|       ), |       ), | ||||||
|       new charts.Series<NumericEntityStateHistoryMoment, DateTime>( |       new charts.Series<NumericEntityStateHistoryMoment, DateTime>( | ||||||
|         id: 'State', |         id: 'State', | ||||||
|         radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 4.0 : 2.0, |         radiusPxFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 2.0, | ||||||
|         colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? EntityColors.historyStateColor("on") : EntityColors.historyStateColor("off"), |         colorFn: (NumericEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("off", historyMoment.id), | ||||||
|         domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time, |         domainFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.time, | ||||||
|         measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value, |         measureFn: (NumericEntityStateHistoryMoment historyMoment, _) => historyMoment.value, | ||||||
|         data: data, |         data: data, | ||||||
|   | |||||||
| @@ -39,6 +39,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW | |||||||
|           selectedState: selectedState, |           selectedState: selectedState, | ||||||
|           onPrevTap: () => _selectPrev(), |           onPrevTap: () => _selectPrev(), | ||||||
|           onNextTap: () => _selectNext(), |           onNextTap: () => _selectNext(), | ||||||
|  |           colorIndex: _selectedId, | ||||||
|         ), |         ), | ||||||
|         SizedBox( |         SizedBox( | ||||||
|           height: 70.0, |           height: 70.0, | ||||||
| @@ -79,11 +80,14 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW | |||||||
|       data.add(SimpleEntityStateHistoryMoment(stateData["state"], startTime, endTime, i)); |       data.add(SimpleEntityStateHistoryMoment(stateData["state"], startTime, endTime, i)); | ||||||
|     } |     } | ||||||
|     data.add(SimpleEntityStateHistoryMoment(data.last.state, now, null, widget.rawHistory.length)); |     data.add(SimpleEntityStateHistoryMoment(data.last.state, now, null, widget.rawHistory.length)); | ||||||
|  |     if (_selectedId == -1) { | ||||||
|  |       _selectedId = 0; | ||||||
|  |     } | ||||||
|     return [ |     return [ | ||||||
|       new charts.Series<SimpleEntityStateHistoryMoment, DateTime>( |       new charts.Series<SimpleEntityStateHistoryMoment, DateTime>( | ||||||
|         id: 'State', |         id: 'State', | ||||||
|         strokeWidthPxFn: (SimpleEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 70.0 : 40.0, |         strokeWidthPxFn: (SimpleEntityStateHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 70.0 : 40.0, | ||||||
|         colorFn: (SimpleEntityStateHistoryMoment historyMoment, __) => EntityColors.historyStateColor(historyMoment.state), |         colorFn: (SimpleEntityStateHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.id), | ||||||
|         domainFn: (SimpleEntityStateHistoryMoment historyMoment, _) => historyMoment.startTime, |         domainFn: (SimpleEntityStateHistoryMoment historyMoment, _) => historyMoment.startTime, | ||||||
|         measureFn: (SimpleEntityStateHistoryMoment historyMoment, _) => 0, |         measureFn: (SimpleEntityStateHistoryMoment historyMoment, _) => 0, | ||||||
|         data: data, |         data: data, | ||||||
| @@ -131,8 +135,9 @@ class HistoryControlWidget extends StatelessWidget { | |||||||
|   final DateTime selectedTimeStart; |   final DateTime selectedTimeStart; | ||||||
|   final DateTime selectedTimeEnd; |   final DateTime selectedTimeEnd; | ||||||
|   final String selectedState; |   final String selectedState; | ||||||
|  |   final int colorIndex; | ||||||
|  |  | ||||||
|   const HistoryControlWidget({Key key, this.onPrevTap, this.onNextTap, this.selectedTimeStart, this.selectedTimeEnd, this.selectedState}) : super(key: key); |   const HistoryControlWidget({Key key, this.onPrevTap, this.onNextTap, this.selectedTimeStart, this.selectedTimeEnd, this.selectedState, @ required this.colorIndex}) : super(key: key); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
| @@ -156,7 +161,7 @@ class HistoryControlWidget extends StatelessWidget { | |||||||
|                     textAlign: TextAlign.right, |                     textAlign: TextAlign.right, | ||||||
|                     style: TextStyle( |                     style: TextStyle( | ||||||
|                         fontWeight: FontWeight.bold, |                         fontWeight: FontWeight.bold, | ||||||
|                         color: EntityColors.stateColor(selectedState), |                         color: EntityColors.historyStateColor(selectedState, colorIndex), | ||||||
|                         fontSize: 22.0 |                         fontSize: 22.0 | ||||||
|                     ), |                     ), | ||||||
|                   ), |                   ), | ||||||
| @@ -173,7 +178,7 @@ class HistoryControlWidget extends StatelessWidget { | |||||||
|           ); |           ); | ||||||
|  |  | ||||||
|     } else { |     } else { | ||||||
|       return Container(height: 32.0); |       return Container(height: 48.0); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -187,11 +192,9 @@ class HistoryControlWidget extends StatelessWidget { | |||||||
|           Text("${formatDate(selectedTimeEnd, [M, ' ', d, ', ', HH, ':', nn, ':', ss])}", textAlign: TextAlign.left,) |           Text("${formatDate(selectedTimeEnd, [M, ' ', d, ', ', HH, ':', nn, ':', ss])}", textAlign: TextAlign.left,) | ||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
|     return Expanded( |     return Column( | ||||||
|       child: Column( |  | ||||||
|       crossAxisAlignment: CrossAxisAlignment.start, |       crossAxisAlignment: CrossAxisAlignment.start, | ||||||
|       children: children, |       children: children, | ||||||
|       ), |  | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -14,6 +14,7 @@ import 'package:date_format/date_format.dart'; | |||||||
| import 'package:http/http.dart' as http; | import 'package:http/http.dart' as http; | ||||||
| import 'package:flutter_colorpicker/material_picker.dart'; | import 'package:flutter_colorpicker/material_picker.dart'; | ||||||
| import 'package:charts_flutter/flutter.dart' as charts; | import 'package:charts_flutter/flutter.dart' as charts; | ||||||
|  | import 'dart:math' as math; | ||||||
|  |  | ||||||
| part 'entity_class/entity.class.dart'; | part 'entity_class/entity.class.dart'; | ||||||
| part 'entity_class/switch_entity.class.dart'; | part 'entity_class/switch_entity.class.dart'; | ||||||
| @@ -35,6 +36,7 @@ part 'entity_widgets/entity_name.dart'; | |||||||
| part 'entity_widgets/last_updated.dart'; | part 'entity_widgets/last_updated.dart'; | ||||||
| part 'entity_widgets/mode_swicth.dart'; | part 'entity_widgets/mode_swicth.dart'; | ||||||
| part 'entity_widgets/mode_selector.dart'; | part 'entity_widgets/mode_selector.dart'; | ||||||
|  | part 'entity_widgets/entity_colors.class.dart'; | ||||||
| part 'entity_widgets/entity_page_container.dart'; | part 'entity_widgets/entity_page_container.dart'; | ||||||
| part 'entity_widgets/history_chart/entity_history.dart'; | part 'entity_widgets/history_chart/entity_history.dart'; | ||||||
| part 'entity_widgets/history_chart/simple_state_history_chart.dart'; | part 'entity_widgets/history_chart/simple_state_history_chart.dart'; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user