-
Notifications
You must be signed in to change notification settings - Fork 1
/
date_time_box.dart
220 lines (179 loc) · 5.91 KB
/
date_time_box.dart
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import 'package:flutter/material.dart';
import 'package:boatinstrument/boatinstrument_controller.dart';
import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart';
part 'date_time_box.g.dart';
@JsonSerializable()
class _DateTimeSettings {
String dateFormat;
_DateTimeSettings({
this.dateFormat = 'yyyy-MM-dd'
});
}
@JsonSerializable()
class _DateTimePerBoxSettings {
bool showDate;
bool showTime;
bool utc;
String timeFormat;
_DateTimePerBoxSettings({
this.showDate = true,
this.showTime = true,
this.utc = false,
this.timeFormat = 'HH:mm:ss'
});
}
class DateTimeBox extends BoxWidget {
late final _DateTimePerBoxSettings _perBoxSettings;
DateTimeBox(super.config, {super.key}) {
_perBoxSettings = _$DateTimePerBoxSettingsFromJson(config.settings);
}
@override
State<DateTimeBox> createState() => _DateTimeBoxState();
static String sid = 'date-time';
@override
String get id => sid;
@override
bool get hasSettings => true;
@override
BoxSettingsWidget getSettingsWidget(Map<String, dynamic> json) {
return _SettingsWidget(_$DateTimeSettingsFromJson(json));
}
@override
Widget? getSettingsHelp() => const HelpTextWidget('For a full list of formats see https://api.flutter.dev/flutter/intl/DateFormat-class.html');
@override
bool get hasPerBoxSettings => true;
@override
BoxSettingsWidget getPerBoxSettingsWidget() {
return _PerBoxSettingsWidget(_perBoxSettings);
}
@override
Widget? getPerBoxSettingsHelp() => const HelpTextWidget('For a full list of formats see https://api.flutter.dev/flutter/intl/DateFormat-class.html');
}
class _DateTimeBoxState extends State<DateTimeBox> {
_DateTimeSettings _settings = _DateTimeSettings();
DateTime? _dateTime;
@override
void initState() {
super.initState();
_settings = _$DateTimeSettingsFromJson(widget.config.controller.getBoxSettingsJson(widget.id));
widget.config.controller.configure(onUpdate: _processData, paths: {'navigation.datetime'});
}
@override
Widget build(BuildContext context) {
_DateTimePerBoxSettings perBoxSettings = widget._perBoxSettings;
TextStyle style = Theme.of(context).textTheme.titleMedium!.copyWith(height: 1.0);
const double pad = 5.0;
if(widget.config.editMode) {
_dateTime = DateTime.now();
}
String dateTimeString = '';
int lines = 1;
if(_dateTime == null) {
dateTimeString = '-';
} else {
DateTime dt = perBoxSettings.utc ? _dateTime!.toUtc() : _dateTime!.toLocal();
if(perBoxSettings.showDate) {
dateTimeString += DateFormat(_settings.dateFormat).format(dt);
}
if(perBoxSettings.showTime) {
if(perBoxSettings.showDate) {
++lines;
dateTimeString += '\n';
}
dateTimeString += DateFormat(perBoxSettings.timeFormat).format(dt);
}
}
double fontSize = maxFontSize(dateTimeString, style,
((widget.config.constraints.maxHeight - style.fontSize! - (3 * pad)) / lines),
widget.config.constraints.maxWidth - (2 * pad));
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Row(children: [Padding(padding: const EdgeInsets.only(top: pad, left: pad), child: Text('Date/Time${perBoxSettings.utc ? ' UTC':''}', style: style))]),
// We need to disable the device text scaling as this interferes with our text scaling.
Expanded(child: Center(child: Padding(padding: const EdgeInsets.all(pad), child: Text(dateTimeString, textScaler: TextScaler.noScaling, style: style.copyWith(fontSize: fontSize)))))
]);
}
_processData(List<Update>? updates) {
if(updates == null) {
_dateTime = null;
} else {
try {
_dateTime = DateTime.parse(updates[0].value);
} catch (e) {
widget.config.controller.l.e("Error parsing date/time $updates", error: e);
}
}
if(mounted) {
setState(() {});
}
}
}
class _SettingsWidget extends BoxSettingsWidget {
final _DateTimeSettings _settings;
const _SettingsWidget(this._settings);
@override
Map<String, dynamic> getSettingsJson() {
return _$DateTimeSettingsToJson(_settings);
}
@override
createState() => _SettingsState();
}
class _SettingsState extends State<_SettingsWidget> {
@override
Widget build(BuildContext context) {
_DateTimeSettings s = widget._settings;
return ListView(children: [
ListTile(
leading: const Text('Date Format:'),
title: TextFormField(
initialValue: s.dateFormat,
onChanged: (value) => s.dateFormat = value)
),
]);
}
}
class _PerBoxSettingsWidget extends BoxSettingsWidget {
final _DateTimePerBoxSettings _perBoxSettings;
const _PerBoxSettingsWidget(this._perBoxSettings);
@override
Map<String, dynamic> getSettingsJson() {
return _$DateTimePerBoxSettingsToJson(_perBoxSettings);
}
@override
createState() => _PerBoxSettingsState();
}
class _PerBoxSettingsState extends State<_PerBoxSettingsWidget> {
@override
Widget build(BuildContext context) {
_DateTimePerBoxSettings s = widget._perBoxSettings;
return ListView(children: [
SwitchListTile(title: const Text('Show Date:'),
value: s.showDate,
onChanged: (bool value) {
setState(() {
s.showDate = value;
});
}),
SwitchListTile(title: const Text('Show Time:'),
value: s.showTime,
onChanged: (bool value) {
setState(() {
s.showTime = value;
});
}),
SwitchListTile(title: const Text('UTC:'),
value: s.utc,
onChanged: (bool value) {
setState(() {
s.utc = value;
});
}),
ListTile(
leading: const Text('Time Format:'),
title: TextFormField(
initialValue: s.timeFormat,
onChanged: (value) => s.timeFormat = value)
),
]);
}
}