This commit is contained in:
2025-05-28 10:23:20 +03:00
parent 55996c06c8
commit a4741c3137
4 changed files with 23 additions and 18 deletions

View File

@@ -17,6 +17,8 @@ import 'models/form_group_structure.model.dart';
import 'models/form_group_value.model.dart'; import 'models/form_group_value.model.dart';
//s1 Exports //s1 Exports
part './helpers/controller_states.helper.dart';
/// A specialized form controller to handle form states, /// A specialized form controller to handle form states,
class AstromicFormController extends FormController { class AstromicFormController extends FormController {
AstromicFormController({ AstromicFormController({
@@ -49,9 +51,13 @@ class AstromicFormController extends FormController {
@override @override
void removeController(String id) { void removeController(String id) {
super.removeController(id); super.removeController(id);
// Remove the field state
if (fieldStates.containsKey(id)) { if (fieldStates.containsKey(id)) {
fieldStates.remove(id); fieldStates.remove(id);
} }
// Remove the field message
if (fieldMessages.containsKey(id)) { if (fieldMessages.containsKey(id)) {
fieldMessages.remove(id); fieldMessages.remove(id);
} }
@@ -68,24 +74,7 @@ class AstromicFormController extends FormController {
final Stream<List<(String internalCode, String? message)>>? errorStream; final Stream<List<(String internalCode, String? message)>>? errorStream;
//S1 - Methods //S1 - Methods
/// Does the controller has a field with this ID.
bool hasKey(String id) => controllers.containsKey(id);
/// 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;
/// Set the field state and message of a specific field using it's ID.
void setState(String fieldId, AstromicFieldState state, {String? message}) {
if (!fieldStates.containsKey(fieldId)) {
throw Exception('The state of the field ID $fieldId does not exist.');
}
fieldStates[fieldId] = state;
fieldMessages[fieldId] = message;
_stateStreamController.add((fieldId, state));
}
/// Reset the state of a specific field using it's ID.
void resetState(String fieldId) => setState(fieldId, AstromicFieldState.idle);
//!SECTION //!SECTION
//SECTION - Hosted Values //SECTION - Hosted Values

View File

@@ -0,0 +1,16 @@
part of '../controller.dart';
/// 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;
/// Set the field state and message of a specific field using it's ID.
void setState(String fieldId, AstromicFieldState state, {String? message}) {
if (!fieldStates.containsKey(fieldId)) {
throw Exception('The state of the field ID $fieldId does not exist.');
}
fieldStates[fieldId] = state;
fieldMessages[fieldId] = message;
_stateStreamController.add((fieldId, state));
}
/// Reset the state of a specific field using it's ID.
void resetState(String fieldId) => setState(fieldId, AstromicFieldState.idle);