Skip to content

Commit

Permalink
Added number bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Sep 11, 2023
1 parent 022f805 commit 292d880
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/combo_box_choo
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/split_button_chooser.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/subsystem_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/match_time.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/number_bar.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/text_display.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/toggle_button.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/toggle_switch.dart';
Expand Down Expand Up @@ -82,6 +83,10 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
newWidget = GraphWidget(
key: UniqueKey(), topic: child!.topic, period: child!.period);
break;
case 'Number Bar':
newWidget = NumberBar(
key: UniqueKey(), topic: child!.topic, period: child!.period);
break;
case 'Number Slider':
newWidget = NumberSlider(
key: UniqueKey(), topic: child!.topic, period: child!.period);
Expand Down Expand Up @@ -278,6 +283,11 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
key: UniqueKey(),
jsonData: jsonData['properties'],
);
case 'Number Bar':
return NumberBar.fromJson(
key: UniqueKey(),
jsonData: jsonData['properties'],
);
case 'Number Slider':
return NumberSlider.fromJson(
key: UniqueKey(),
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/nt4_widgets/nt4_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mixin NT4Widget on StatelessWidget {
case NT4TypeStr.kInt:
return [
'Text Display',
'Number Bar',
'Number Slider',
'Graph',
'Match Time',
Expand Down
154 changes: 154 additions & 0 deletions lib/widgets/nt4_widgets/single_topic/number_bar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_text_input.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';

class NumberBar extends StatelessWidget with NT4Widget {
@override
String type = 'Number Bar';

double minValue;
double maxValue;
int? divisions;

NumberBar(
{super.key,
required topic,
this.minValue = -1.0,
this.maxValue = 1.0,
this.divisions = 5,
period = Globals.defaultPeriod}) {
super.topic = topic;
super.period = period;

init();
}

NumberBar.fromJson({super.key, required Map<String, dynamic> jsonData})
: minValue = jsonData['min_value'] ?? -1.0,
maxValue = jsonData['max_value'] ?? 1.0,
divisions = jsonData['divisions'] {
topic = jsonData['topic'] ?? '';
period = jsonData['period'] ?? Globals.defaultPeriod;

init();
}

@override
Map<String, dynamic> toJson() {
return {
'topic': topic,
'period': period,
'min_value': minValue,
'max_value': maxValue,
'divisions': divisions,
};
}

@override
List<Widget> getEditProperties(BuildContext context) {
return [
// Min and max values
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
children: [
Flexible(
child: DialogTextInput(
onSubmit: (value) {
double? newMin = double.tryParse(value);
if (newMin == null) {
return;
}
minValue = newMin;
refresh();
},
formatter: FilteringTextInputFormatter.allow(RegExp(r"[0-9.-]")),
label: 'Min Value',
initialText: minValue.toString(),
),
),
const SizedBox(width: 5),
Flexible(
child: DialogTextInput(
onSubmit: (value) {
double? newMax = double.tryParse(value);
if (newMax == null) {
return;
}
maxValue = newMax;
refresh();
},
formatter: FilteringTextInputFormatter.allow(RegExp(r"[0-9.-]")),
label: 'Max Value',
initialText: maxValue.toString(),
),
),
],
),
const SizedBox(height: 5),
// Number of divisions
DialogTextInput(
onSubmit: (value) {
int? newDivisions = int.tryParse(value);
if (newDivisions != null && newDivisions < 2) {
return;
}
divisions = newDivisions;
refresh();
},
formatter: FilteringTextInputFormatter.digitsOnly,
label: 'Divisions',
initialText: (divisions != null) ? divisions.toString() : '',
allowEmptySubmission: true,
),
];
}

@override
Widget build(BuildContext context) {
notifier = context.watch<NT4WidgetNotifier?>();

return StreamBuilder(
stream: subscription?.periodicStream(),
initialData: nt4Connection.getLastAnnouncedValue(topic),
builder: (context, snapshot) {
double value = snapshot.data as double? ?? 0.0;

double clampedValue = value.clamp(minValue, maxValue);

double? divisionInterval = (divisions != null)
? (maxValue - minValue) / (divisions! - 1)
: null;

return Column(
children: [
Text(
value.toStringAsFixed(4),
style: Theme.of(context).textTheme.bodyLarge,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 5),
SfLinearGauge(
key: UniqueKey(),
maximum: maxValue,
minimum: minValue,
barPointers: [
LinearBarPointer(
value: clampedValue,
animationDuration: 0,
),
],
minorTicksPerInterval: 0,
interval: divisionInterval,
),
],
);
},
);
}
}

0 comments on commit 292d880

Please sign in to comment.