This commit is contained in:
2025-05-16 11:02:15 -04:00
parent b56cd35042
commit 67dd688db7
2 changed files with 42 additions and 6 deletions

View File

@@ -21,10 +21,12 @@ import 'models/form_group_value.model.dart';
class AstromicFormController extends FormController { class AstromicFormController extends FormController {
AstromicFormController({ AstromicFormController({
Map<String, (String initialText, bool initialObscurity)>? initialValues, Map<String, (String initialText, bool initialObscurity)>? initialValues,
Map<String, (dynamic, bool)>? initialHostedValues,
this.errorStream, this.errorStream,
}) : super(initialValues) { }) : super(initialValues) {
// Add states and messages based on initial controller. // Add states and messages based on initial controllers.
_addInitialControllers(initialValues); _addInitialControllers(initialValues);
_addInitialHostedValues(initialHostedValues);
} }
//SECTION - Overrides //SECTION - Overrides
@@ -254,12 +256,21 @@ class AstromicFormController extends FormController {
for (final String fieldID in structure.fields) { for (final String fieldID in structure.fields) {
final String fullID = standeredGroupFormat(baseID, index.toString(), fieldID); final String fullID = standeredGroupFormat(baseID, index.toString(), fieldID);
controller(fullID); String? preFilled;
if (structure.preFields?.containsKey(fieldID) ?? false) {
preFilled = structure.preFields![fieldID];
}
controller(fullID, initialText: preFilled);
} }
for (final String valueID in structure.values ?? <String>[]) { for (final String valueID in structure.values ?? <String>[]) {
final String fullID = standeredGroupFormat(baseID, index.toString(), valueID); final String fullID = standeredGroupFormat(baseID, index.toString(), valueID);
controller(fullID); (String, dynamic)? preFilled;
if (structure.preValues?.containsKey(valueID) ?? false) {
preFilled = structure.preValues![valueID];
}
controller(fullID, initialText: preFilled?.$1);
setValue(valueID, preFilled?.$2);
} }
} }
@@ -548,5 +559,13 @@ class AstromicFormController extends FormController {
))); )));
} }
} }
void _addInitialHostedValues(Map<String, (dynamic, bool)>? initialValues) {
if (initialValues != null) {
for (MapEntry<String, (dynamic, bool)> vEntry in initialValues.entries) {
setValue(vEntry.key, vEntry.value.$1, isRequired: vEntry.value.$2);
}
}
}
//!SECTION //!SECTION
} }

View File

@@ -5,25 +5,33 @@ import 'package:flutter/foundation.dart';
class FormGroupStructure { class FormGroupStructure {
final String id; final String id;
final List<String> fields; final List<String> fields;
final Map<String, String>? preFields;
final List<String>? values; final List<String>? values;
final Map<String, (String, dynamic)>? preValues;
final List<(int initialCount, FormGroupStructure structure)>? subGroups; final List<(int initialCount, FormGroupStructure structure)>? subGroups;
FormGroupStructure({ FormGroupStructure({
required this.id, required this.id,
required this.fields, required this.fields,
this.values, this.values,
this.preFields,
this.preValues,
this.subGroups, this.subGroups,
}); });
FormGroupStructure copyWith({ FormGroupStructure copyWith({
String? id, String? id,
List<String>? fields, List<String>? fields,
Map<String, String>? preFields,
List<String>? values, List<String>? values,
Map<String, (String, dynamic)>? preValues,
List<(int initialCount, FormGroupStructure structure)>? subGroups, List<(int initialCount, FormGroupStructure structure)>? 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, values: values ?? this.values,
preFields: preFields ?? this.preFields,
preValues: preValues ?? this.preValues,
subGroups: subGroups ?? this.subGroups, subGroups: subGroups ?? this.subGroups,
); );
} }
@@ -33,6 +41,8 @@ class FormGroupStructure {
'id': id, 'id': id,
'fields': fields, 'fields': fields,
'values': values, 'values': values,
'preFields': preFields,
'preValues': preValues,
'subGroups': subGroups?.map(((int initialCount, FormGroupStructure structure) x) => <String, dynamic>{'structure': x.$2.toMap(), 'initialCount': x.$1}).toList(), 'subGroups': subGroups?.map(((int initialCount, FormGroupStructure structure) x) => <String, dynamic>{'structure': x.$2.toMap(), 'initialCount': x.$1}).toList(),
}; };
} }
@@ -42,6 +52,8 @@ class FormGroupStructure {
id: map['id'] as String, id: map['id'] as String,
fields: List<String>.from(map['fields'] as List<String>), fields: List<String>.from(map['fields'] as List<String>),
values: map['values'] != null ? List<String>.from(map['values'] as List<String>) : null, values: map['values'] != null ? List<String>.from(map['values'] as List<String>) : null,
preFields: map['preFields'] != null ? map['preFields'] as Map<String,String> : null,
preValues: map['preValues'] != null ? map['preValues'] as Map<String,(String,dynamic)>: null,
subGroups: map['subGroups'] != null subGroups: map['subGroups'] != null
? (map['subGroups'] as List<Map<String, dynamic>>).map((Map<String, dynamic> map) => (int.tryParse(map['initialCount']) ?? 0, FormGroupStructure.fromMap(map['structure']))).toList() ? (map['subGroups'] as List<Map<String, dynamic>>).map((Map<String, dynamic> map) => (int.tryParse(map['initialCount']) ?? 0, FormGroupStructure.fromMap(map['structure']))).toList()
: null, : null,
@@ -54,18 +66,23 @@ class FormGroupStructure {
@override @override
String toString() { String toString() {
return 'FormGroupStructure(id: $id, fields: $fields, values: $values, subGroups: $subGroups)'; return 'FormGroupStructure(id: $id, fields: $fields, values: $values, preFields: $preFields, preValues: $preValues, 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 && listEquals(other.fields, fields) && listEquals(other.values, values) && listEquals(other.subGroups, subGroups); return other.id == id &&
listEquals(other.fields, fields) &&
listEquals(other.values, values) &&
mapEquals(other.preFields, preFields) &&
mapEquals(other.preValues, preValues) &&
listEquals(other.subGroups, subGroups);
} }
@override @override
int get hashCode { int get hashCode {
return id.hashCode ^ fields.hashCode ^ values.hashCode ^ subGroups.hashCode; return id.hashCode ^ fields.hashCode ^ values.hashCode ^ preFields.hashCode ^ preValues.hashCode ^ subGroups.hashCode;
} }
} }