This commit is contained in:
2025-04-08 09:44:43 +02:00
parent 1b6eef010a
commit 0c9067d142
2 changed files with 34 additions and 22 deletions

View File

@@ -70,9 +70,15 @@ class AstromicFormController extends FormController {
// Add the controllers and values. // Add the controllers and values.
for (int i = 0; i < initialCount; i++) { for (int i = 0; i < initialCount; i++) {
for (MapEntry<String, bool> fieldEntry in groupStructure.fields.entries) { for (String fieldID in groupStructure.fields) {
String fieldID = '${groupStructure.id}-#$i-${fieldEntry.key}'; String finalFieldID = '${groupStructure.id}-#$i-$fieldID';
controller(fieldID); controller(finalFieldID);
}
if (groupStructure.values != null && groupStructure.values!.isNotEmpty) {
for (String valueID in groupStructure.values!.map(((String, Type) a) => a.$1).toList()) {
String finalFieldID = '${groupStructure.id}-#$i-$valueID';
controller(finalFieldID);
}
} }
} }
} }
@@ -120,15 +126,15 @@ class AstromicFormController extends FormController {
// Get the current fields with this ID // Get the current fields with this ID
Map<String, String> firstSetOfFieldsWithID = Map<String, String>.fromEntries( Map<String, String> firstSetOfFieldsWithID = Map<String, String>.fromEntries(
controllers.entries.where((MapEntry<String, String> c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(), controllers.entries.where((MapEntry<String, String> c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(),
); );
print('First set of fields: $firstSetOfFieldsWithID'); print('First set of fields: $firstSetOfFieldsWithID');
// get the fields IDs // get the fields IDs
List<String> fieldsIDs = groupStructure!.fields.entries.where((MapEntry<String, bool> e) => !e.value).nonNulls.map((MapEntry<String, bool> ee) => ee.key).toList(); List<String> fieldsIDs = groupStructure!.fields.nonNulls.toList();
print('fieldIDs: $fieldsIDs'); print('fieldIDs: $fieldsIDs');
// get the values IDs // get the values IDs
List<String> valuesIDs = groupStructure.fields.entries.where((MapEntry<String, bool> e) => e.value).nonNulls.map((MapEntry<String, bool> ee) => ee.key).toList(); List<(String, Type)> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <(String, Type)>[];
print('valueIDs: $valuesIDs'); print('valueIDs: $valuesIDs');
// get the subGroups // get the subGroups
@@ -143,7 +149,7 @@ class AstromicFormController extends FormController {
instances.add( instances.add(
FormGroupInstance( FormGroupInstance(
fields: Map<String, String>.fromEntries(fieldsIDs.map((String id) => MapEntry<String, String>('$formGroupID-#$i-$id', value('$formGroupID-#$i-$id'))).toList()), fields: Map<String, String>.fromEntries(fieldsIDs.map((String id) => MapEntry<String, String>('$formGroupID-#$i-$id', value('$formGroupID-#$i-$id'))).toList()),
values: Map<String, dynamic>.fromEntries(valuesIDs.map((String id) => MapEntry<String, String>('$formGroupID-#$i-$id', getValue('$formGroupID-#$i-$id'))).toList()), values: Map<String, dynamic>.fromEntries(valuesIDs.map(((String, Type) a) => MapEntry<String, String>('$formGroupID-#$i-${a.$1}', getValue<dynamic>('$formGroupID-#$i-${a.$1}'))).toList()),
subGroups: subValues, subGroups: subValues,
), ),
); );
@@ -194,7 +200,6 @@ class AstromicFormController extends FormController {
// } // }
// } // }
// Map<String,Map<String,dynamic>> getFormGroup(String groupID){} // Map<String,Map<String,dynamic>> getFormGroup(String groupID){}
/// Get the field state and message of a specific field using it's ID. /// Get the field state and message of a specific field using it's ID.

View File

@@ -4,22 +4,26 @@ import 'package:flutter/foundation.dart';
class FormGroupStructure { class FormGroupStructure {
final String id; final String id;
final Map<String, bool> fields; final List<String> fields;
final List<(String, Type)>? values;
final List<FormGroupStructure>? subGroups; final List<FormGroupStructure>? subGroups;
FormGroupStructure({ FormGroupStructure({
required this.id, required this.id,
required this.fields, required this.fields,
this.values,
this.subGroups, this.subGroups,
}); });
FormGroupStructure copyWith({ FormGroupStructure copyWith({
String? id, String? id,
Map<String, bool>? fields, List<String>? fields,
List<(String, Type)>? values,
List<FormGroupStructure>? subGroups, List<FormGroupStructure>? subGroups,
}) { }) {
return FormGroupStructure( return FormGroupStructure(
id: id ?? this.id, id: id ?? this.id,
fields: fields ?? this.fields, fields: fields ?? this.fields,
values: values ?? this.values,
subGroups: subGroups ?? this.subGroups, subGroups: subGroups ?? this.subGroups,
); );
} }
@@ -28,6 +32,7 @@ class FormGroupStructure {
return <String, dynamic>{ return <String, dynamic>{
'id': id, 'id': id,
'fields': fields, 'fields': fields,
'values': values?.map(((String, Type) x) => <String, Object>{'id': x.$1, 'type': x.$2}).toList(),
'subGroups': subGroups?.map((FormGroupStructure x) => x.toMap()).toList(), 'subGroups': subGroups?.map((FormGroupStructure x) => x.toMap()).toList(),
}; };
} }
@@ -35,14 +40,9 @@ class FormGroupStructure {
factory FormGroupStructure.fromMap(Map<String, dynamic> map) { factory FormGroupStructure.fromMap(Map<String, dynamic> map) {
return FormGroupStructure( return FormGroupStructure(
id: map['id'] as String, id: map['id'] as String,
fields: Map<String, bool>.from(map['fields'] as Map<String, bool>), fields: List<String>.from(map['fields'] as List<String>),
subGroups: map['subGroups'] != null values: map['values'] != null ? (map['values'] as Map<String,dynamic>).entries.map((MapEntry<String, dynamic> entry) => (entry.key.toString(),entry.value.runtimeType)).toList() : null,
? List<FormGroupStructure>.from( subGroups: map['subGroups'] != null ? (map['subGroups'] as List<Map<String, dynamic>>).map((Map<String, dynamic> f) => FormGroupStructure.fromMap(f)).toList() : null,
(map['subGroups'] as List<int>).map<FormGroupStructure?>(
(int x) => FormGroupStructure.fromMap(x as Map<String, dynamic>),
),
)
: null,
); );
} }
@@ -52,18 +52,25 @@ class FormGroupStructure {
@override @override
String toString() { String toString() {
return 'FormGroup(id: $id, fields: $fields, subGroups: $subGroups)'; return 'FormGroupStructure(id: $id, fields: $fields, values: $values, subGroups: $subGroups)';
} }
@override @override
bool operator ==(covariant FormGroupStructure other) { bool operator ==(covariant FormGroupStructure other) {
if (identical(this, other)) return true; if (identical(this, other)) return true;
return other.id == id && mapEquals(other.fields, fields) && listEquals(other.subGroups, subGroups); return
other.id == id &&
listEquals(other.fields, fields) &&
listEquals(other.values, values) &&
listEquals(other.subGroups, subGroups);
} }
@override @override
int get hashCode { int get hashCode {
return id.hashCode ^ fields.hashCode ^ subGroups.hashCode; return id.hashCode ^
fields.hashCode ^
values.hashCode ^
subGroups.hashCode;
} }
} }