Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async input #25

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.13.3",
"flavors": {}
"flutterSdkVersion": "3.13.3"
}
1 change: 1 addition & 0 deletions .fvm/release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13.3
1 change: 1 addition & 0 deletions .fvm/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13.3
1 change: 1 addition & 0 deletions .fvm/versions/3.13.3
3 changes: 3 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flutter": "3.13.3"
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4

- name: Configure FVM
uses: kuhnroyal/flutter-fvm-config-action@v1
uses: kuhnroyal/flutter-fvm-config-action@v2
id: fvm-config-action
with:
path: ".fvm/fvm_config.json"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/storybook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- uses: actions/checkout@v4

- name: Configure FVM
uses: kuhnroyal/flutter-fvm-config-action@v1
uses: kuhnroyal/flutter-fvm-config-action@v2
with:
path: ".fvm/fvm_config.json"

Expand All @@ -57,14 +57,14 @@ jobs:
run: flutter build web --base-href "/glade_forms/"

- name: Setup Pages
uses: actions/configure-pages@v3
uses: actions/configure-pages@v4

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
# Upload entire repository
path: "storybook/build/web/"

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
melos_my_project.iml
.vscode/*
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/settings.json

.packages
Expand Down
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "storybook",
"cwd": "storybook",
"request": "launch",
"type": "dart",
"program": "lib/main.dart"
},
{
"name": "storybook (profile)",
"cwd": "storybook",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
}
]
}
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"dart.flutterSdkPath": ".fvm/flutter_sdk",
"dart.lineLength": 120,
"dart.flutterSdkPath": ".fvm\\versions\\3.13.3",
"dart.lineLength": 120,
"yaml.schemaStore.enable": false
}
10 changes: 10 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GladeModel = sync verze
- Pouze SYNC inputy
- Podpora default TextFieldu
AsyncGladeModel
- ASYNC inicializace
- SYNC i ASYNC inputy
- Podpora pro GladeAsyncTextField
- Validace async
- IsValid async
- Translate async
100 changes: 99 additions & 1 deletion glade_forms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ A universal way to define form validators with support of translations.
- [Edit multiple inputs at once](#edit-multiple-inputs-at-once)
- [Dependencies](#dependencies)
- [Controlling other inputs](#controlling-other-inputs)
- [Asynchronous form](#asynchronous-form)
- [Translation](#translation)
- [Converters](#converters)
- [Debugging](#debugging)
Expand Down Expand Up @@ -73,7 +74,7 @@ Then use `GladeFormBuilder`
and connect the model to standard Flutter form and it's inputs like this:

```dart
GladeFormBuilder(
GladeFormBuilder.create(
create: (context) => _Model(),
builder: (context, model) => Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
Expand Down Expand Up @@ -312,6 +313,103 @@ onChange: (info, dependencies) {

![two-way-inputs-example](https://raw.githubusercontent.com/netglade/glade_forms/main/glade_forms/doc/two-way-dependencies.gif)

### Asynchronous form

When the default data for the form are for example fetched from external API you can use `GladeModelAsync` like this:

```dart
Future<String> _fetchName() {
return Future.delayed(const Duration(seconds: 2), () => 'John Doe');
}
class _Model extends GladeModelAsync {
late StringInput name;
late GladeInput<int> age;
late StringInput email;
late GladeInput<bool> vip;
@override
List<GladeInput<Object?>> get inputs => [name, age, email, vip];
_Model();
@override
Future<void> initializeAsync() async {
final nameValue = await _fetchName();
name = GladeInput.stringInput(inputKey: 'name', value: nameValue);
age = GladeInput.intInput(value: 0, inputKey: 'age');
email = GladeInput.stringInput(validator: (validator) => (validator..isEmail()).build(), inputKey: 'email');
vip = GladeInput.create(
validator: (v) => (v..notNull()).build(),
value: false,
inputKey: 'vip',
dependencies: () => [name],
onChangeAsync: (info, dependencies) async {
final nameInput = dependencies.byKey<String?>('name');
final fetchedName = await _fetchName();
groupEdit(() {
nameInput.value = fetchedName;
});
},
);
await super.initializeAsync();
}
}
```

Then use `GladeFormBuilder` and connect the model to standard Flutter form and it's inputs like this:

```dart
GladeFormBuilder.create(
create: (context) => _Model(),
builder: (context, model, _) => Padding(
padding: const EdgeInsets.all(32),
child: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
if (model.isChanging) const LinearProgressIndicator(color: Colors.red, backgroundColor: Colors.grey),
TextFormField(
controller: model.name.controller,
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,
),
CheckboxListTile(
value: model.vip.value,
title: Row(
children: [
const Text('VIP Content '),
if (model.isChanging) const Text('isChanging', style: TextStyle(color: Colors.red)),
],
),
onChanged: (v) => model.vip.value = v ?? false,
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: model.isValid
? () => ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Saved')))
: null,
child: const Text('Save'),
),
],
),
),
),
),
```

### Translation

Each validation error (and conversion error if any) can be translated. Provide `translateError` function which accepts:
Expand Down
2 changes: 2 additions & 0 deletions glade_forms/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ include: package:netglade_analysis/lints.yaml
dart_code_metrics:
extends:
- package:netglade_analysis/dcm.yaml
rules:
- prefer-correct-callback-field-name: false
pubspec-rules:
prefer-publish-to-none: false
Loading