1
1
// Copyright (c) The ThingSet Project Contributors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
+ import 'dart:convert' ;
5
+
4
6
import 'package:flutter/material.dart' ;
5
7
import 'package:flutter/services.dart' ;
6
8
@@ -62,13 +64,19 @@ class StatefulTextField extends StatefulWidget {
62
64
class StatefulTextFieldState extends State <StatefulTextField > {
63
65
final controller = TextEditingController ();
64
66
late bool isNum = false ;
67
+ late bool isList = false ;
65
68
66
69
@override
67
70
void initState () {
68
71
super .initState ();
69
- controller.text = widget.value.toString ();
70
72
if (widget.value is num ) {
71
73
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 ();
72
80
}
73
81
}
74
82
@@ -85,6 +93,12 @@ class StatefulTextFieldState extends State<StatefulTextField> {
85
93
// maintain type of original data (default would be String)
86
94
if (isNum) {
87
95
widget.onChanged (num .tryParse (value));
96
+ } else if (isList) {
97
+ try {
98
+ widget.onChanged (jsonDecode ('[$value ]' ));
99
+ } catch (e) {
100
+ // ignore invalid data
101
+ }
88
102
} else {
89
103
widget.onChanged (value);
90
104
}
@@ -95,11 +109,9 @@ class StatefulTextFieldState extends State<StatefulTextField> {
95
109
suffixText: widget.unit,
96
110
),
97
111
controller: controller,
98
- keyboardType:
99
- isNum ? TextInputType .number : TextInputType .text,
112
+ keyboardType: isNum ? TextInputType .number : TextInputType .text,
100
113
inputFormatters: [
101
- if (isNum)
102
- FilteringTextInputFormatter .allow (RegExp ('[0-9.-]' )),
114
+ if (isNum) FilteringTextInputFormatter .allow (RegExp ('[0-9.-]' )),
103
115
if (isNum)
104
116
TextInputFormatter .withFunction (
105
117
(TextEditingValue oldValue, TextEditingValue newValue) {
0 commit comments