This commit is contained in:
2025-04-06 16:06:04 +02:00
parent 01eb92d3fc
commit fade65d178
6 changed files with 201 additions and 118 deletions

View File

@@ -13,6 +13,7 @@ import 'package:form_controller/form_controller.dart';
//s3 Services
//s3 Models & Widgets
import 'enums/enums.exports.dart';
import 'models/form_group_structure.model.dart';
//s1 Exports
/// A specialized form controller to handle form states,
@@ -22,6 +23,7 @@ class AstromicFormController extends FormController {
final Map<String, String?> fieldMessages = <String, String?>{};
final Map<String, (dynamic, bool)> _hostedValues = <String, (dynamic, bool)>{};
final Stream<List<(String internalCode, String? message)>>? errorStream;
final List<FormGroupStructure> _formGroups = <FormGroupStructure>[];
// State Stream Variables
static final StreamController<(String, AstromicFieldState)> _stateStreamController = StreamController<(String id, AstromicFieldState)>.broadcast();
@@ -39,9 +41,135 @@ class AstromicFormController extends FormController {
_addInitialControllers(initialValues);
}
get i => null;
/// Does the controller has a field with this ID.
bool hasKey(String id) => controllers.containsKey(id);
/* some-group: {
some-group-0 : null,
some-group-1 : null,
some-group-2: null,
some-group-3: {
some-group-3-a: null,
some-group-3-b: null,
some-group-3-c: null,
some-group-3-d: null,
},
}
*/
// At the start of a screen?
void initializeFormGroup(FormGroupStructure group, {int initialCount = 1}) {
if (group.fields.isEmpty) {
throw Exception('Group Fields should NOT be empty.');
}
_formGroups.add(group);
// Add the controllers and values.
for (int i = 0; i < initialCount; i++) {
for (MapEntry<String, bool> fieldEntry in group.fields.entries) {
String fieldID = '${group.id}-$i-${fieldEntry.key}';
controller(fieldID);
if (fieldEntry.value) {
// Is a value field
} else {
// Is a text field
}
}
}
}
/*{
{
0: {
field1: something,
field2: something else
field3: somethin another,
sub-group: {
0:
{
sub-field1: someValue,
sub-field2: someOtherValue,
}
1:
2:
}
}
1:
2:
}
}
*/
Map<int, Map<String,dynamic>> getFormGroup(String formGroupID) {
// Get the group structure with the same ID
FormGroupStructure? groupWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList().firstOrNull;
if (groupWithSameID == null) {
throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?');
}
// Get the current fields with this ID
Map<String, String> firstSetOfFieldsWithID = Map<String, String>.fromEntries(
controllers.entries.where((MapEntry<String, String> c) => RegExp(formGroupID + r'-#[\d+]-' + groupWithSameID.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(),
);
for (MapEntry<String, bool> fieldEntry in groupWithSameID.fields.entries) {
String fieldID = '$formGroupID-${firstSetOfFieldsWithID.length}-${fieldEntry.key}';
controller(fieldID);
if (fieldEntry.value) {
// Is a value field
} else {
// Is a text field
}
}
}
void addToFormGroup(String formGroupID) {
// Get the group structure with the same ID
FormGroupStructure? groupWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList().firstOrNull;
if (groupWithSameID == null) {
throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?');
}
// Get the current fields with this ID
Map<String, String> firstSetOfFieldsWithID = Map<String, String>.fromEntries(
controllers.entries.where((MapEntry<String, String> c) => RegExp(formGroupID + r'-#[\d+]-' + groupWithSameID.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(),
);
for (MapEntry<String, bool> fieldEntry in groupWithSameID.fields.entries) {
String fieldID = '$formGroupID-${firstSetOfFieldsWithID.length}-${fieldEntry.key}';
controller(fieldID);
if (fieldEntry.value) {
// Is a value field
} else {
// Is a text field
}
}
}
void removeFromFormGroup(String formGroupID, int index) {
// Get all the groups with the same ID
List<FormGroupStructure> groupsWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList();
if (groupsWithSameID.isEmpty) {
throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?');
}
_formGroups.removeAt(groupsWithSameID.first);
for (MapEntry<String, bool> fieldEntry in groupsWithSameID.first.fields.entries) {
String fieldID = '$formGroupID-${groupsWithSameID.length}-${fieldEntry.key}';
controller(fieldID);
if (fieldEntry.value) {
// Is a value field
} else {
// Is a text field
}
}
}
void getFormGroupValues(FormGroupStructure group) {}
// Map<String,Map<String,dynamic>> getFormGroup(String groupID){}
/// Get the field state and message of a specific field using it's ID.
(AstromicFieldState, String? message)? getState(String fieldId) => (fieldStates.containsKey(fieldId)) ? (fieldStates[fieldId]!, fieldMessages[fieldId]) : null;

View File

@@ -1,117 +0,0 @@
// //s1 Imports
// //s2 Packages
// //s3 Core Packages
// import 'package:flutter/material.dart';
// import '../../sheet/sheet_helper.astromic.dart';
// //s3 Internal Packages
// //s3 3rd-party Packages
// //s2 Utility
// //s3 Configs
// //s3 Misc
// //s2 Domain
// //s3 Entities
// //s3 Usecases
// //s2 Presentation
// //s3 Design
// //s3 Presenters
// //s3 Widgets
// //s1 Exports
// class FormGroupWrapper extends StatefulWidget {
// //SECTION - Widget Arguments
// final AstromicFormController formController;
// final String identifier;
// //!SECTION
// //
// const FormGroupWrapper({
// super.key,
// required this.formController,
// required this
// });
// @override
// State<FormGroupWrapper> createState() => _FormGroupWrapperState();
// }
// class _FormGroupWrapperState extends State<FormGroupWrapper> {
// //
// //SECTION - State Variables
// //s1 --State
// //s1 --State
// //
// //s1 --Controllers
// //late AstromicFormController _formController;
// //s1 --Controllers
// //
// //s1 --Constants
// //s1 --Constants
// //!SECTION
// @override
// void initState() {
// super.initState();
// //
// //SECTION - State Variables initializations & Listeners
// //s1 --State
// //s1 --State
// //
// //s1 --Controllers & Listeners
// // _formController = AstromicFormController();
// //s1 --Controllers & Listeners
// //
// //s1 --Late & Async Initializers
// //s1 --Late & Async Initializers
// //!SECTION
// }
// @override
// void didChangeDependencies() {
// super.didChangeDependencies();
// //
// //SECTION - State Variables initializations & Listeners
// //s1 --State
// //s1 --State
// //
// //s1 --Controllers & Listeners
// //s1 --Controllers & Listeners
// //
// //!SECTION
// }
// //SECTION - Dumb Widgets
// //!SECTION
// //SECTION - Stateless functions
// //!SECTION
// //SECTION - Action Callbacks
// //!SECTION
// @override
// Widget build(BuildContext context) {
// //SECTION - Build Setup
// //s1 --Values
// //double w = MediaQuery.of(context).size.width;
// //double h = MediaQuery.of(context).size.height;
// //s1 --Values
// //
// //s1 --Contexted Widgets
// //s1 --Contexted Widgets
// //!SECTION
// //SECTION - Build Return
// return const Scaffold(
// body: Center(child:Text(FormGroupWrapper)),
// );
// //!SECTION
// }
// @override
// void dispose() {
// //SECTION - Disposable variables
// //!SECTION
// super.dispose();
// }
// }

View File

@@ -0,0 +1,69 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
class FormGroupStructure {
final String id;
final Map<String, bool> fields;
final List<FormGroupStructure>? subGroups;
FormGroupStructure({
required this.id,
required this.fields,
this.subGroups,
});
FormGroupStructure copyWith({
String? id,
Map<String, bool>? fields,
List<FormGroupStructure>? subGroups,
}) {
return FormGroupStructure(
id: id ?? this.id,
fields: fields ?? this.fields,
subGroups: subGroups ?? this.subGroups,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'fields': fields,
'subGroups': subGroups?.map((FormGroupStructure x) => x.toMap()).toList(),
};
}
factory FormGroupStructure.fromMap(Map<String, dynamic> map) {
return FormGroupStructure(
id: map['id'] as String,
fields: Map<String, bool>.from(map['fields'] as Map<String, bool>),
subGroups: map['subGroups'] != null
? List<FormGroupStructure>.from(
(map['subGroups'] as List<int>).map<FormGroupStructure?>(
(int x) => FormGroupStructure.fromMap(x as Map<String, dynamic>),
),
)
: null,
);
}
String toJson() => json.encode(toMap());
factory FormGroupStructure.fromJson(String source) => FormGroupStructure.fromMap(json.decode(source) as Map<String, dynamic>);
@override
String toString() {
return 'FormGroup(id: $id, fields: $fields, subGroups: $subGroups)';
}
@override
bool operator ==(covariant FormGroupStructure other) {
if (identical(this, other)) return true;
return other.id == id && mapEquals(other.fields, fields) && listEquals(other.subGroups, subGroups);
}
@override
int get hashCode {
return id.hashCode ^ fields.hashCode ^ subGroups.hashCode;
}
}

View File

@@ -0,0 +1,3 @@
class FromGroupValue{
final
}