Skip to content

Commit 1847569

Browse files
committed
Add support for array values
1 parent 8709ae2 commit 1847569

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

lib/screens/node.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,15 @@ class DataItem extends StatelessWidget {
423423
} else {
424424
valueWidget = StatefulTextField(
425425
value: data,
426-
unit: unit,
426+
unit: data is List ? '' : unit,
427427
onChanged: (value) {
428428
node.setDesired(path, value);
429429
},
430430
);
431431
}
432432
} else {
433433
valueWidget = Text(
434-
'$data $unit',
434+
data is List ? data.join(', ') : '$data $unit',
435435
softWrap: true,
436436
style: const TextStyle(fontSize: 16),
437437
);
@@ -441,10 +441,13 @@ class DataItem extends StatelessWidget {
441441
child: ListTile(
442442
tileColor: Theme.of(context).cardColor,
443443
title: Text(descr),
444-
trailing: ConstrainedBox(
445-
constraints: const BoxConstraints(maxWidth: 100),
446-
child: valueWidget,
447-
),
444+
trailing: data is List
445+
? Text(unit, style: const TextStyle(fontSize: 16))
446+
: ConstrainedBox(
447+
constraints: const BoxConstraints(maxWidth: 100),
448+
child: valueWidget,
449+
),
450+
subtitle: data is List ? valueWidget : null,
448451
),
449452
);
450453
}

lib/widgets/stateful_input.dart

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) The ThingSet Project Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'dart:convert';
5+
46
import 'package:flutter/material.dart';
57
import 'package:flutter/services.dart';
68

@@ -62,13 +64,19 @@ class StatefulTextField extends StatefulWidget {
6264
class StatefulTextFieldState extends State<StatefulTextField> {
6365
final controller = TextEditingController();
6466
late bool isNum = false;
67+
late bool isList = false;
6568

6669
@override
6770
void initState() {
6871
super.initState();
69-
controller.text = widget.value.toString();
7072
if (widget.value is num) {
7173
isNum = true;
74+
controller.text = widget.value.toString();
75+
} else if (widget.value is List) {
76+
isList = true;
77+
controller.text = widget.value.join(', ');
78+
} else {
79+
controller.text = widget.value.toString();
7280
}
7381
}
7482

@@ -85,6 +93,12 @@ class StatefulTextFieldState extends State<StatefulTextField> {
8593
// maintain type of original data (default would be String)
8694
if (isNum) {
8795
widget.onChanged(num.tryParse(value));
96+
} else if (isList) {
97+
try {
98+
widget.onChanged(jsonDecode('[$value]'));
99+
} catch (e) {
100+
// ignore invalid data
101+
}
88102
} else {
89103
widget.onChanged(value);
90104
}
@@ -95,11 +109,9 @@ class StatefulTextFieldState extends State<StatefulTextField> {
95109
suffixText: widget.unit,
96110
),
97111
controller: controller,
98-
keyboardType:
99-
isNum ? TextInputType.number : TextInputType.text,
112+
keyboardType: isNum ? TextInputType.number : TextInputType.text,
100113
inputFormatters: [
101-
if (isNum)
102-
FilteringTextInputFormatter.allow(RegExp('[0-9.-]')),
114+
if (isNum) FilteringTextInputFormatter.allow(RegExp('[0-9.-]')),
103115
if (isNum)
104116
TextInputFormatter.withFunction(
105117
(TextEditingValue oldValue, TextEditingValue newValue) {

0 commit comments

Comments
 (0)