-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrefreshable_data_table.dart
More file actions
157 lines (140 loc) · 4.52 KB
/
refreshable_data_table.dart
File metadata and controls
157 lines (140 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import 'dart:async';
import 'package:flutter/material.dart';
import 'button.dart';
/// A widget to display data from a [Map]
///
/// Displays the data in a simple data table, with options for
/// displaying the time the data were retrieved,
/// automatic refreshing, and for controlling the refresh.
class ViamRefreshableDataTable extends StatefulWidget {
/// The function to get and refresh the data
final FutureOr<Map<String, dynamic>> Function({Map<String, dynamic>? extra}) getData;
/// How often to automatically refresh the data
final Duration? refreshInterval;
/// Whether to display the time the data were retrieved
final bool showLastRefreshed;
/// Whether to display controls for refreshing data
final bool showRefreshControls;
const ViamRefreshableDataTable({
super.key,
required this.getData,
this.refreshInterval = const Duration(seconds: 1),
this.showLastRefreshed = true,
this.showRefreshControls = true,
});
@override
State<ViamRefreshableDataTable> createState() {
return _ViamRefreshableDataTableState();
}
}
class _ViamRefreshableDataTableState extends State<ViamRefreshableDataTable> {
Map<String, dynamic> readings = {};
Timer? timer;
DateTime? lastRefreshed;
bool isPaused = false;
Future<void> getReadings() async {
try {
final response = await widget.getData();
setState(
() {
readings = Map.fromEntries(response.entries.toList()..sort((a, b) => a.key.compareTo(b.key)));
lastRefreshed = DateTime.now();
},
);
} catch (e) {
Text('Error: $e');
}
}
void refresh() {
getReadings();
}
void playPause() {
setState(() => isPaused = !isPaused);
if (isPaused) {
timer?.cancel();
} else {
getReadings();
_createTimer();
}
}
void _createTimer() {
if (widget.refreshInterval != null) {
timer = Timer.periodic(widget.refreshInterval!, (_) {
refresh();
});
}
}
String formattedDate(DateTime date) {
return '${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}:${date.millisecond}';
}
@override
void initState() {
super.initState();
getReadings();
_createTimer();
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
if (readings.isEmpty)
const Text('No sensor readings available')
else
DataTable(
columns: const <DataColumn>[DataColumn(label: Text('Reading')), DataColumn(label: Text('Value'))],
rows: readings.keys
.map((e) => DataRow(cells: [
DataCell(Text(e)),
DataCell(
SizedBox(
width: MediaQuery.of(context).size.width * 0.5,
height: 120,
child: Align(
alignment: Alignment.centerLeft,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(readings[e].toString()),
),
),
),
),
)
]))
.toList()),
if (widget.showLastRefreshed && lastRefreshed != null)
Column(children: [
const SizedBox(height: 8),
Text('Updated at: ${formattedDate(lastRefreshed!)}'),
]),
if (widget.showRefreshControls)
Column(children: [
const SizedBox(height: 8),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
if (widget.refreshInterval != null)
ViamButton(
onPressed: playPause,
text: isPaused ? 'Play' : 'Pause',
icon: isPaused ? Icons.play_arrow : Icons.pause,
variant: ViamButtonVariant.iconOnly,
style: ViamButtonFillStyle.ghost,
),
const SizedBox(width: 8),
ViamButton(
onPressed: refresh,
text: 'Refresh',
icon: Icons.refresh,
variant: ViamButtonVariant.iconOnly,
style: ViamButtonFillStyle.ghost,
),
]),
]),
],
);
}
}