Skip to content

Commit

Permalink
Create example with onChangeAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-jilek committed Jan 5, 2024
1 parent ebf9819 commit 031793e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
20 changes: 19 additions & 1 deletion glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -139,7 +140,7 @@ class GladeInput<T> extends ChangeNotifier {

notifyListeners();

_onChangeAsyncCall();
unawaited(_onChangeAsyncCall());
}

// ignore: avoid_setters_without_getters, ok for internal use
Expand All @@ -159,6 +160,7 @@ class GladeInput<T> extends ChangeNotifier {
required InputDependenciesFactory? dependenciesFactory,
required this.defaultTranslations,
required this.onChange,
required this.onChangeAsync,
required ValueTransform<T>? valueTransform,
T? initialValue,
TextEditingController? textEditingController,
Expand Down Expand Up @@ -198,6 +200,7 @@ class GladeInput<T> extends ChangeNotifier {
StringToTypeConverter<T>? valueConverter,
InputDependenciesFactory? dependencies,
OnChange<T>? onChange,
OnChangeAsync<T>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<T>? valueTransform,
Expand All @@ -216,6 +219,7 @@ class GladeInput<T> extends ChangeNotifier {
stringTovalueConverter: valueConverter,
dependenciesFactory: dependencies,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
Expand All @@ -238,6 +242,7 @@ class GladeInput<T> extends ChangeNotifier {
StringToTypeConverter<T>? valueConverter,
InputDependenciesFactory? dependencies,
OnChange<T>? onChange,
OnChangeAsync<T>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<T>? valueTransform,
Expand All @@ -253,6 +258,7 @@ class GladeInput<T> extends ChangeNotifier {
pure: pure,
dependencies: dependencies,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
Expand All @@ -271,6 +277,7 @@ class GladeInput<T> extends ChangeNotifier {
StringToTypeConverter<T>? valueConverter,
InputDependenciesFactory? dependencies,
OnChange<T>? onChange,
OnChangeAsync<T>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<T>? valueTransform,
Expand All @@ -286,6 +293,7 @@ class GladeInput<T> extends ChangeNotifier {
pure: pure,
dependencies: dependencies,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
Expand All @@ -305,6 +313,7 @@ class GladeInput<T> extends ChangeNotifier {
ValueComparator<int>? valueComparator,
InputDependenciesFactory? dependencies,
OnChange<int>? onChange,
OnChangeAsync<int>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<int>? valueTransform,
Expand All @@ -320,6 +329,7 @@ class GladeInput<T> extends ChangeNotifier {
dependencies: dependencies,
valueConverter: GladeTypeConverters.intConverter,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
Expand All @@ -335,6 +345,7 @@ class GladeInput<T> extends ChangeNotifier {
ValueComparator<bool>? valueComparator,
InputDependenciesFactory? dependencies,
OnChange<bool>? onChange,
OnChangeAsync<bool>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
ValueTransform<bool>? valueTransform,
Expand All @@ -350,6 +361,7 @@ class GladeInput<T> extends ChangeNotifier {
dependencies: dependencies,
valueConverter: GladeTypeConverters.boolConverter,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueTransform: valueTransform,
Expand All @@ -365,6 +377,7 @@ class GladeInput<T> extends ChangeNotifier {
DefaultTranslations? defaultTranslations,
InputDependenciesFactory? dependencies,
OnChange<String?>? onChange,
OnChangeAsync<String?>? onChangeAsync,
TextEditingController? textEditingController,
bool createTextController = true,
bool isRequired = true,
Expand All @@ -384,6 +397,7 @@ class GladeInput<T> extends ChangeNotifier {
inputKey: inputKey,
dependenciesFactory: dependencies,
onChange: onChange,
onChangeAsync: onChangeAsync,
textEditingController: textEditingController,
createTextController: createTextController,
valueComparator: valueComparator,
Expand Down Expand Up @@ -490,6 +504,7 @@ class GladeInput<T> extends ChangeNotifier {
bool? isPure,
DefaultTranslations? defaultTranslations,
OnChange<T>? onChange,
OnChangeAsync<T>? onChangeAsync,
TextEditingController? textEditingController,
// ignore: avoid-unused-parameters, it is here just to be linter happy ¯\_(ツ)_/¯
bool? createTextController,
Expand All @@ -507,6 +522,7 @@ class GladeInput<T> extends ChangeNotifier {
isPure: isPure ?? this.isPure,
defaultTranslations: defaultTranslations ?? this.defaultTranslations,
onChange: onChange ?? this.onChange,
onChangeAsync: onChangeAsync ?? this.onChangeAsync,
textEditingController: textEditingController ?? this._textEditingController,
valueTransform: valueTransform ?? this.valueTransform,
);
Expand All @@ -527,6 +543,8 @@ class GladeInput<T> extends ChangeNotifier {
);

_isChanging = false;

notifyListeners();
}

/// Translates input's errors (validation or conversion).
Expand Down
19 changes: 18 additions & 1 deletion storybook/lib/usecases/quickstart_async_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ class _Model extends GladeModelAsync {

name = GladeInput.stringInput(inputKey: 'name', value: nameValue);
age = GladeInput.intInput(value: 0, inputKey: 'age');
email = GladeInput.stringInput(validator: (validator) => (validator..isEmail()).build(), inputKey: 'email');
email = GladeInput.stringInput(
validator: (validator) => (validator..isEmail()).build(),
inputKey: 'email',
dependencies: () => [name],
onChangeAsync: (info, dependencies) async {
print(1);
final nameInput = dependencies.byKey<String?>('name');

final fetchedName = await _fetchName();

groupEdit(() {
nameInput.value = fetchedName;
});
},
);

await super.initializeAsync();
}
Expand All @@ -50,18 +64,21 @@ class QuickStartAsyncExample extends StatelessWidget {
validator: model.name.textFormFieldInputValidator,
onChanged: model.name.updateValueWithString,
decoration: const InputDecoration(labelText: 'Name'),
readOnly: model.isChanging,
),
TextFormField(
controller: model.age.controller,
validator: model.age.textFormFieldInputValidator,
onChanged: model.age.updateValueWithString,
decoration: const InputDecoration(labelText: 'Age'),
readOnly: model.isChanging,
),
TextFormField(
controller: model.email.controller,
validator: model.email.textFormFieldInputValidator,
onChanged: model.email.updateValueWithString,
decoration: const InputDecoration(labelText: 'Email'),
readOnly: model.isChanging,
),
const SizedBox(height: 10),
ElevatedButton(
Expand Down

0 comments on commit 031793e

Please sign in to comment.