@@ -17,7 +17,7 @@ class FormDataWidget extends ConsumerStatefulWidget {
17
17
class _FormDataBodyState extends ConsumerState <FormDataWidget > {
18
18
late int seed;
19
19
final random = Random .secure ();
20
- late List <FormDataModel > rows ;
20
+ late List <FormDataModel > formRows ;
21
21
22
22
@override
23
23
void initState () {
@@ -28,7 +28,7 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
28
28
void _onFieldChange (String selectedId) {
29
29
ref.read (collectionStateNotifierProvider.notifier).update (
30
30
selectedId,
31
- requestFormDataList: rows ,
31
+ requestFormDataList: formRows. sublist ( 0 , formRows.length - 1 ) ,
32
32
);
33
33
}
34
34
@@ -37,40 +37,46 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
37
37
final selectedId = ref.watch (selectedIdStateProvider);
38
38
ref.watch (selectedRequestModelProvider
39
39
.select ((value) => value? .requestFormDataList? .length));
40
- var formRows = ref.read (selectedRequestModelProvider)? .requestFormDataList;
41
- rows =
42
- formRows == null || formRows.isEmpty ? [kFormDataEmptyModel] : formRows;
40
+ var rF = ref.read (selectedRequestModelProvider)? .requestFormDataList;
41
+ bool isFormDataEmpty = rF == null || rF.isEmpty;
42
+ List <FormDataModel > rows = (isFormDataEmpty)
43
+ ? [
44
+ kFormDataEmptyModel,
45
+ ]
46
+ : rF;
47
+ formRows = isFormDataEmpty ? rows : rows + [kFormDataEmptyModel];
43
48
44
49
DaviModel <FormDataModel > daviModelRows = DaviModel <FormDataModel >(
45
- rows: rows ,
50
+ rows: formRows ,
46
51
columns: [
47
52
DaviColumn (
48
53
cellPadding: kpsV5,
49
54
name: 'Key' ,
50
55
grow: 4 ,
51
56
cellBuilder: (_, row) {
52
57
int idx = row.index;
58
+ bool isLast = idx + 1 == formRows.length;
53
59
return Theme (
54
60
data: Theme .of (context),
55
61
child: FormDataField (
56
62
keyId: "$selectedId -$idx -form-v-$seed " ,
57
- initialValue: rows [idx].name,
63
+ initialValue: formRows [idx].name,
58
64
hintText: " Add Key" ,
59
65
onChanged: (value) {
60
- rows [idx] = rows [idx].copyWith (name: value);
61
- if (idx == rows.length - 1 ) rows .add (kFormDataEmptyModel);
66
+ formRows [idx] = formRows [idx].copyWith (name: value);
67
+ if (isLast) formRows .add (kFormDataEmptyModel);
62
68
_onFieldChange (selectedId! );
63
69
},
64
70
colorScheme: Theme .of (context).colorScheme,
65
- formDataType: rows [idx].type,
71
+ formDataType: formRows [idx].type,
66
72
onFormDataTypeChanged: (value) {
67
- bool hasChanged = rows [idx].type != value;
68
- rows [idx] = rows [idx].copyWith (
73
+ bool hasChanged = formRows [idx].type != value;
74
+ formRows [idx] = formRows [idx].copyWith (
69
75
type: value ?? FormDataType .text,
70
76
);
71
- rows [idx] = rows [idx].copyWith (value: "" );
72
- if (idx == rows .length - 1 && hasChanged) {
73
- rows .add (kFormDataEmptyModel);
77
+ formRows [idx] = formRows [idx].copyWith (value: "" );
78
+ if (idx == formRows .length - 1 && hasChanged) {
79
+ formRows .add (kFormDataEmptyModel);
74
80
}
75
81
setState (() {});
76
82
_onFieldChange (selectedId! );
@@ -97,7 +103,8 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
97
103
cellPadding: kpsV5,
98
104
cellBuilder: (_, row) {
99
105
int idx = row.index;
100
- return rows[idx].type == FormDataType .file
106
+ bool isLast = idx + 1 == formRows.length;
107
+ return formRows[idx].type == FormDataType .file
101
108
? Align (
102
109
alignment: Alignment .centerLeft,
103
110
child: Row (
@@ -122,17 +129,17 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
122
129
if (pickedResult != null &&
123
130
pickedResult.files.isNotEmpty &&
124
131
pickedResult.files.first.path != null ) {
125
- rows [idx] = rows [idx].copyWith (
132
+ formRows [idx] = formRows [idx].copyWith (
126
133
value: pickedResult.files.first.path! ,
127
134
);
128
135
setState (() {});
129
136
_onFieldChange (selectedId! );
130
137
}
131
138
},
132
139
label: Text (
133
- (rows [idx].type == FormDataType .file &&
134
- rows [idx].value.isNotEmpty)
135
- ? rows [idx].value.toString ()
140
+ (formRows [idx].type == FormDataType .file &&
141
+ formRows [idx].value.isNotEmpty)
142
+ ? formRows [idx].value.toString ()
136
143
: "Select File" ,
137
144
textAlign: TextAlign .center,
138
145
overflow: TextOverflow .ellipsis,
@@ -146,11 +153,11 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
146
153
)
147
154
: CellField (
148
155
keyId: "$selectedId -$idx -form-v-$seed " ,
149
- initialValue: rows [idx].value,
156
+ initialValue: formRows [idx].value,
150
157
hintText: " Add Value" ,
151
158
onChanged: (value) {
152
- rows [idx] = rows [idx].copyWith (value: value);
153
- if (idx == rows.length - 1 ) rows .add (kFormDataEmptyModel);
159
+ formRows [idx] = formRows [idx].copyWith (value: value);
160
+ if (isLast) formRows .add (kFormDataEmptyModel);
154
161
_onFieldChange (selectedId! );
155
162
},
156
163
colorScheme: Theme .of (context).colorScheme,
@@ -162,21 +169,24 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
162
169
pinStatus: PinStatus .none,
163
170
width: 30 ,
164
171
cellBuilder: (_, row) {
172
+ bool isLast = row.index + 1 == formRows.length;
165
173
return InkWell (
174
+ onTap: isLast
175
+ ? null
176
+ : () {
177
+ seed = random.nextInt (kRandMax);
178
+ if (formRows.length == 2 ) {
179
+ setState (() {
180
+ formRows = [kFormDataEmptyModel];
181
+ });
182
+ } else {
183
+ formRows.removeAt (row.index);
184
+ }
185
+ _onFieldChange (selectedId! );
186
+ },
166
187
child: Theme .of (context).brightness == Brightness .dark
167
188
? kIconRemoveDark
168
189
: kIconRemoveLight,
169
- onTap: () {
170
- seed = random.nextInt (kRandMax);
171
- if (rows.length == 1 ) {
172
- setState (() {
173
- rows = [kFormDataEmptyModel];
174
- });
175
- } else {
176
- rows.removeAt (row.index);
177
- }
178
- _onFieldChange (selectedId! );
179
- },
180
190
);
181
191
},
182
192
),
@@ -207,7 +217,7 @@ class _FormDataBodyState extends ConsumerState<FormDataWidget> {
207
217
padding: const EdgeInsets .only (bottom: 30 ),
208
218
child: ElevatedButton .icon (
209
219
onPressed: () {
210
- rows .add (kFormDataEmptyModel);
220
+ formRows .add (kFormDataEmptyModel);
211
221
_onFieldChange (selectedId! );
212
222
},
213
223
icon: const Icon (Icons .add),
0 commit comments