Skip to content

Commit

Permalink
Add support for view inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
tenhobi committed Mar 13, 2024
1 parent dd66f48 commit 8bb6b92
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 41 deletions.
3 changes: 1 addition & 2 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"flutterSdkVersion": "3.16.5",
"flavors": {}
"flutterSdkVersion": "3.16.5"
}
3 changes: 3 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flutter": "3.16.5"
}
2 changes: 0 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
- name: Configure FVM
uses: kuhnroyal/flutter-fvm-config-action@v2
id: fvm-config-action
with:
path: ".fvm/fvm_config.json"

- name: Install Flutter
uses: subosito/flutter-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ build/
.last_build_id
*.iml

.fvm/flutter_sdk
.fvm/
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dart.flutterSdkPath": ".fvm/flutter_sdk",
"dart.lineLength": 120,
"dart.flutterSdkPath": ".fvm/versions/3.16.5",
"dart.lineLength": 120
}
4 changes: 4 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.0
- **[Feat]**: Support non-data-holding inputs to enable "view" inputs.
- **[Fix]**: Export `ChangesInfo`.

## 1.3.2
- **[Fix]**: `GladeInput` now preserves selection. (Before, a cursor jumped at the end.)

Expand Down
1 change: 1 addition & 0 deletions glade_forms/lib/src/core/core.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'changes_info.dart';
export 'convert_error.dart';
export 'error_translator.dart';
export 'glade_error_keys.dart';
Expand Down
90 changes: 57 additions & 33 deletions glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class GladeInput<T> extends ChangeNotifier {
/// Called when input's value changed.
OnChange<T>? onChange;

/// Determines whether this input will be considered in isUnchanged on model.
///
/// That means, when the value is false, it will opt-out this input from the computation.
bool trackUnchanged;

/// Transforms passed value before assigning it into input.
// ignore: prefer-correct-callback-field-name, ok name
ValueTransform<T> valueTransform;
Expand Down Expand Up @@ -105,36 +110,7 @@ class GladeInput<T> extends ChangeNotifier {
/// String representattion of [value].
String get stringValue => stringTovalueConverter?.convertBack(value) ?? value.toString();

set value(T value) {
_previousValue = _value;

_value = valueTransform(value);

final strValue = stringValue;
// synchronize text controller with value
_textEditingController?.value = TextEditingValue(
text: strValue,
selection: _textEditingController?.selection ?? const TextSelection.collapsed(offset: -1),
);

_isPure = false;
__conversionError = null;

// propagate input's changes
onChange?.call(
ChangesInfo(
previousValue: _previousValue,
value: value,
initialValue: initialValue,
validatorResult: validate(),
),
dependenciesFactory(),
);

_bindedModel?.notifyInputUpdated(this);

notifyListeners();
}
set value(T value) => _setValue(value, shouldTriggerOnChange: true);

// ignore: avoid_setters_without_getters, ok for internal use
set _conversionError(ConvertError<T> value) {
Expand All @@ -157,6 +133,7 @@ class GladeInput<T> extends ChangeNotifier {
T? initialValue,
TextEditingController? textEditingController,
bool createTextController = true,
this.trackUnchanged = true,
}) : _isPure = isPure,
_value = value,
_initialValue = initialValue,
Expand Down Expand Up @@ -200,6 +177,7 @@ class GladeInput<T> extends ChangeNotifier {
bool createTextController = true,
ValueTransform<T>? valueTransform,
DefaultTranslations? defaultTranslations,
bool trackUnchanged = true,
}) {
assert(
value != null || initialValue != null || TypeHelper.typeIsNullable<T>(),
Expand All @@ -223,10 +201,10 @@ class GladeInput<T> extends ChangeNotifier {
createTextController: createTextController,
valueTransform: valueTransform,
defaultTranslations: defaultTranslations,
trackUnchanged: trackUnchanged,
);
}

// Predefined GenericInput without any validations.
///
/// Useful for input which allows null value without additional validations.
///
Expand All @@ -244,6 +222,7 @@ class GladeInput<T> extends ChangeNotifier {
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<T>? valueTransform,
bool trackUnchanged = true,
}) =>
GladeInput.create(
validator: (v) => v.build(),
Expand All @@ -259,6 +238,7 @@ class GladeInput<T> extends ChangeNotifier {
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
trackUnchanged: trackUnchanged,
);

/// Predefined GenericInput with predefined `notNull` validation.
Expand All @@ -277,6 +257,7 @@ class GladeInput<T> extends ChangeNotifier {
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<T>? valueTransform,
bool trackUnchanged = true,
}) =>
GladeInput.create(
validator: (v) => (v..notNull()).build(),
Expand All @@ -292,6 +273,7 @@ class GladeInput<T> extends ChangeNotifier {
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
trackUnchanged: trackUnchanged,
);

@internal
Expand All @@ -311,6 +293,7 @@ class GladeInput<T> extends ChangeNotifier {
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<int>? valueTransform,
bool trackUnchanged = true,
}) =>
GladeInput.create(
value: value,
Expand All @@ -326,6 +309,7 @@ class GladeInput<T> extends ChangeNotifier {
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
trackUnchanged: trackUnchanged,
);

static GladeInput<bool> boolInput({
Expand All @@ -341,6 +325,7 @@ class GladeInput<T> extends ChangeNotifier {
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<bool>? valueTransform,
bool trackUnchanged = true,
}) =>
GladeInput.create(
value: value,
Expand All @@ -356,6 +341,7 @@ class GladeInput<T> extends ChangeNotifier {
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
trackUnchanged: trackUnchanged,
);

static GladeInput<String> stringInput({
Expand Down Expand Up @@ -458,8 +444,11 @@ class GladeInput<T> extends ChangeNotifier {
}
}

// ignore: use_setters_to_change_properties, used as shorthand for field setter.
void updateValue(T value) => this.value = value;
/// Used as shorthand for field setter.
///
/// When [shouldTriggerOnChange] is set to false, the `onChange` callback will not be called.
void updateValue(T value, {bool shouldTriggerOnChange = true}) =>
_setValue(value, shouldTriggerOnChange: shouldTriggerOnChange);

/// Resets input into pure state.
///
Expand Down Expand Up @@ -497,6 +486,7 @@ class GladeInput<T> extends ChangeNotifier {
// ignore: avoid-unused-parameters, it is here just to be linter happy ¯\_(ツ)_/¯
bool? createTextController,
ValueTransform<T>? valueTransform,
bool? trackUnchanged,
}) {
return GladeInput(
value: value ?? this.value,
Expand All @@ -512,7 +502,41 @@ class GladeInput<T> extends ChangeNotifier {
onChange: onChange ?? this.onChange,
textEditingController: textEditingController ?? this._textEditingController,
valueTransform: valueTransform ?? this.valueTransform,
trackUnchanged: trackUnchanged ?? this.trackUnchanged,
);
}

void _setValue(T value, {required bool shouldTriggerOnChange}) {
_previousValue = _value;

_value = valueTransform(value);

final strValue = stringValue;
// synchronize text controller with value
_textEditingController?.value = TextEditingValue(
text: strValue,
selection: _textEditingController?.selection ?? const TextSelection.collapsed(offset: -1),
);

_isPure = false;
__conversionError = null;

// propagate input's changes
if (shouldTriggerOnChange) {
onChange?.call(
ChangesInfo(
previousValue: _previousValue,
value: value,
initialValue: initialValue,
validatorResult: validate(),
),
dependenciesFactory(),
);
}

_bindedModel?.notifyInputUpdated(this);

notifyListeners();
}

/// Translates input's errors (validation or conversion).
Expand Down
2 changes: 1 addition & 1 deletion glade_forms/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: glade_forms
description: A universal way to define form validators with support of translations.
version: 1.3.2
version: 1.4.0
repository: https://github.com/netglade/glade_forms
issue_tracker: https://github.com/netglade/glade_forms/issues
screenshots:
Expand Down

0 comments on commit 8bb6b92

Please sign in to comment.