Merge branch 'master' of https://git.micazi.dev/micazi/astromic_helpers
This commit is contained in:
@@ -96,15 +96,8 @@ class AstromicFormController extends FormController {
|
|||||||
|
|
||||||
//S1 - Methods
|
//S1 - Methods
|
||||||
|
|
||||||
/// Prepare a hosted value.
|
|
||||||
void prepareValue<T>(String id, bool isRequired) {
|
|
||||||
if (!_hostedValues.keys.toList().contains(id)) {
|
|
||||||
return _hostedValues.addEntries(<MapEntry<String, (T?, bool)>>[MapEntry<String, (T?, bool)>(id, (null, isRequired))]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get all the hosted values IDs.
|
/// Get all the hosted values IDs.
|
||||||
Map<String, (dynamic,bool)> get allValues => _hostedValues;
|
Map<String, (dynamic, bool)> get allValues => _hostedValues;
|
||||||
|
|
||||||
/// Get the value of a hosted state variable using it's ID.
|
/// Get the value of a hosted state variable using it's ID.
|
||||||
T? getValue<T>(String id) {
|
T? getValue<T>(String id) {
|
||||||
@@ -120,11 +113,16 @@ class AstromicFormController extends FormController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set the value of a hosted state variable using it's ID.
|
/// Set the value of a hosted state variable using it's ID.
|
||||||
void setValue<T>(String id, T data, {bool isRequired = false}) {
|
void setValue<T>(String id, T? data, {bool isRequired = false}) {
|
||||||
prepareValue(id, isRequired);
|
if (!_hostedValues.keys.toList().contains(id)) {
|
||||||
|
return _hostedValues.addEntries(<MapEntry<String, (T?, bool)>>[MapEntry<String, (T?, bool)>(id, (null, isRequired))]);
|
||||||
|
}
|
||||||
//
|
//
|
||||||
_hostedValues[id] = (data, _hostedValues[id]!.$2);
|
else {
|
||||||
_hostedValueValidationStreamController.add((id, false));
|
bool isReq = _hostedValues[id]!.$2;
|
||||||
|
_hostedValues[id] = (data, isReq);
|
||||||
|
_hostedValueValidationStreamController.add((id, false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove the value of a hosted state variable using it's ID.
|
/// Remove the value of a hosted state variable using it's ID.
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class FormValueWrapper<T extends Object?> extends StatefulWidget {
|
|||||||
final AstromicFormController controller;
|
final AstromicFormController controller;
|
||||||
final String id;
|
final String id;
|
||||||
final bool isRequired;
|
final bool isRequired;
|
||||||
final Widget Function(T? value, bool isErroredForValidation, void Function(T value) valueSetter, VoidCallback valueClear) builder;
|
final Widget Function(T? value, bool isErroredForValidation, void Function(T? value) valueSetter, VoidCallback valueClear) builder;
|
||||||
//!SECTION
|
//!SECTION
|
||||||
//
|
//
|
||||||
const FormValueWrapper({
|
const FormValueWrapper({
|
||||||
@@ -61,7 +61,9 @@ class _FormValueWrapperState<T> extends State<FormValueWrapper<T>> {
|
|||||||
//s1 --State
|
//s1 --State
|
||||||
//
|
//
|
||||||
//s1 --Controllers & Listeners
|
//s1 --Controllers & Listeners
|
||||||
widget.controller.prepareValue<T>(widget.id, widget.isRequired);
|
if (!widget.controller.allValues.keys.contains(widget.id)) {
|
||||||
|
widget.controller.setValue<T>(widget.id, null, isRequired: widget.isRequired);
|
||||||
|
}
|
||||||
//s1 --Controllers & Listeners
|
//s1 --Controllers & Listeners
|
||||||
//
|
//
|
||||||
//s1 --Late & Async Initializers
|
//s1 --Late & Async Initializers
|
||||||
@@ -109,8 +111,8 @@ class _FormValueWrapperState<T> extends State<FormValueWrapper<T>> {
|
|||||||
stream: widget.controller.hostedValueValidationStream,
|
stream: widget.controller.hostedValueValidationStream,
|
||||||
builder: (BuildContext context, AsyncSnapshot<(String, bool)> validationSnapshot) {
|
builder: (BuildContext context, AsyncSnapshot<(String, bool)> validationSnapshot) {
|
||||||
return widget.builder(widget.controller.getValue<T>(widget.id),
|
return widget.builder(widget.controller.getValue<T>(widget.id),
|
||||||
validationSnapshot.hasData && validationSnapshot.data != null && validationSnapshot.data!.$1 == widget.id && validationSnapshot.data!.$2 ? true : false, (T newValue) {
|
validationSnapshot.hasData && validationSnapshot.data != null && validationSnapshot.data!.$1 == widget.id && validationSnapshot.data!.$2 ? true : false, (T? newValue) {
|
||||||
return widget.controller.setValue(widget.id, newValue, isRequired: widget.isRequired);
|
return widget.controller.setValue(widget.id, newValue);
|
||||||
}, () => widget.controller.removeValue(widget.id));
|
}, () => widget.controller.removeValue(widget.id));
|
||||||
});
|
});
|
||||||
//!SECTION
|
//!SECTION
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ class _AstromicFuturePresenterState<T> extends State<AstromicFuturePresenter<T>>
|
|||||||
|
|
||||||
//S1 -- Method to reinitialize or update `_future` with a new instance
|
//S1 -- Method to reinitialize or update `_future` with a new instance
|
||||||
void _initializeFuture() {
|
void _initializeFuture() {
|
||||||
_future = widget.controller.getFuture<T?>(widget.id)?.then((result) => result);
|
_future = widget.controller.getFuture(widget.id)?.then((dynamic result) => result as T?);
|
||||||
}
|
}
|
||||||
//!SECTION
|
//!SECTION
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user