-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 10b119f
Showing
33 changed files
with
1,397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ | ||
ios/.generated/ | ||
ios/Flutter/Generated.xcconfig | ||
ios/Runner/GeneratedPluginRegistrant.* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 8661d8aecd626f7f57ccbcb735553edc05a2e713 | ||
channel: stable | ||
|
||
project_type: package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## [0.0.1] - TODO: Add release date. | ||
|
||
* TODO: Describe initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: Add your license here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# flutter_formly | ||
|
||
A new Flutter package project. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Dart | ||
[package](https://flutter.io/developing-packages/), | ||
a library module containing code that can be shared easily across | ||
multiple Flutter or Dart projects. | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.io/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
23 changes: 23 additions & 0 deletions
23
android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.flutter.plugins; | ||
|
||
import io.flutter.plugin.common.PluginRegistry; | ||
|
||
/** | ||
* Generated file. Do not edit. | ||
*/ | ||
public final class GeneratedPluginRegistrant { | ||
public static void registerWith(PluginRegistry registry) { | ||
if (alreadyRegisteredWith(registry)) { | ||
return; | ||
} | ||
} | ||
|
||
private static boolean alreadyRegisteredWith(PluginRegistry registry) { | ||
final String key = GeneratedPluginRegistrant.class.getCanonicalName(); | ||
if (registry.hasPlugin(key)) { | ||
return true; | ||
} | ||
registry.registrarFor(key); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.idea" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.pub" /> | ||
<excludeFolder url="file://$MODULE_DIR$/build" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Dart Packages" level="project" /> | ||
<orderEntry type="library" name="Dart SDK" level="project" /> | ||
<orderEntry type="library" name="Flutter Plugins" level="project" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export './formly/formly.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
part of formly; | ||
|
||
// class FormlyFieldBloc extends Object with Validators { | ||
|
||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
part of formly; | ||
|
||
abstract class IFormlyBloc{ | ||
List<FieldConfig> get fields; | ||
Function(ModelEvents) get changeEvent; | ||
dynamic get modelValue; | ||
dynamic get valid; | ||
Stream<dynamic> get onSubmit; | ||
Stream<dynamic> get onModelChanges; | ||
} | ||
|
||
class FormlyBloc extends Object with | ||
Validators implements IFormlyBloc | ||
{ | ||
bool _valid; | ||
dynamic _model; | ||
List<FieldConfig> _fields; | ||
|
||
@override | ||
List<FieldConfig> get fields => _fields; | ||
final _modelStateController = BehaviorSubject<dynamic>(); | ||
final _formSubmitController = BehaviorSubject<dynamic>(); | ||
final _modelEventController = BehaviorSubject<ModelEvents>(); | ||
|
||
// Return data as stream | ||
@override | ||
Stream<dynamic> get onModelChanges => _modelStateController.stream; | ||
@override | ||
Stream<dynamic> get onSubmit => _formSubmitController.stream; | ||
|
||
@override | ||
dynamic get modelValue => _model; | ||
@override | ||
dynamic get valid => _valid; | ||
|
||
// change data | ||
@override | ||
Function(ModelEvents) get changeEvent => _modelEventController.sink.add; | ||
|
||
|
||
FormlyBloc(dynamic model, List<FieldConfig>fields) { | ||
_model = model; | ||
this._fields = fields; | ||
|
||
_modelEventController | ||
.listen(_mapEventToState); | ||
} | ||
|
||
void _mapEventToState(ModelEvents event) { | ||
if (event is PropertyChangeEvent) { | ||
_model[event.property] = event.value; | ||
_modelStateController.add(_model); | ||
} | ||
|
||
if(event is ModelChangeEvent) { | ||
_model = event.model; | ||
_modelStateController.add(_model); | ||
} | ||
|
||
|
||
if(event is AddFormErrorEvent) { | ||
_errors.add(event.error); | ||
} | ||
|
||
if(event is RemoveFormErrorEvent) { | ||
_removeError(event.error); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
dispose() { | ||
_modelStateController.close(); | ||
_modelEventController.close(); | ||
_formSubmitController.close(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
part of formly; | ||
abstract class ModelEvents {} | ||
|
||
class ModelChangeEvent extends ModelEvents { | ||
final Map<dynamic, dynamic> model; | ||
ModelChangeEvent({ | ||
@required this.model, | ||
}); | ||
} | ||
|
||
class PropertyChangeEvent extends ModelEvents { | ||
final String property; | ||
final value; | ||
|
||
PropertyChangeEvent({ | ||
@required this.property, | ||
@required this.value, | ||
}); | ||
} | ||
class FormSubmitEvent extends ModelEvents { | ||
final Map<dynamic, dynamic> model; | ||
FormSubmitEvent({this.model}); | ||
} | ||
class AddFormErrorEvent extends ModelEvents { | ||
String error; | ||
AddFormErrorEvent({this.error}); | ||
} | ||
class RemoveFormErrorEvent extends ModelEvents { | ||
String error; | ||
RemoveFormErrorEvent({this.error}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
part of formly; | ||
|
||
class Provider extends InheritedWidget { | ||
// final bloc = FormlyBloc(); | ||
|
||
|
||
Provider({Key key, Widget child}) : super(key: key, child: child); | ||
|
||
bool updateShouldNotify(_) => true; | ||
|
||
static FormlyBloc of(BuildContext context) { | ||
//* What it does is through the "of" function, it looks through the context of a widget from the deepest in the widget tree | ||
//* and it keeps travelling up to each widget's parent's context until it finds a "Provider" widget | ||
//* and performs the type conversion to Provider through "as Provider" and then access the Provider's bloc instance variable | ||
// return (context.inheritFromWidgetOfExactType(Provider) as Provider).bloc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
part of formly; | ||
abstract class Validators implements IFormlyBloc { | ||
List<String> _errors = []; | ||
|
||
void onChange(v, FieldConfig field) { | ||
field.value = v; | ||
changeEvent( | ||
PropertyChangeEvent( | ||
property: field.key, | ||
value: v, | ||
), | ||
); | ||
|
||
if(field.onChange != null) { | ||
field.onChange(v); | ||
} | ||
} | ||
bool _updateFieldValidStatus(String r, FieldConfig f, dynamic _val, FormlyBaseValidator v) { | ||
f.errorText = r; | ||
f.isValid = r == null; | ||
if(!f.isValid) _errors.add('${f.key}-${v.name}'); | ||
else _removeError('${f.key}-${v.name}'); | ||
onChange(_val, f); | ||
return !f.isValid; | ||
} | ||
validateForm({List<FieldConfig> flds, bool onSubmit = true}) async{ | ||
flds = flds ?? this.fields; | ||
for(int i=0; i< flds.length; i++) { | ||
FieldConfig f = flds[i]; | ||
await validateField(f, onSubmit); | ||
} | ||
|
||
if(_errors.length > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
validateField(FieldConfig f, bool onSubmit) async { | ||
if(f.validators !=null && f.validators.length >0) { | ||
f.validators.forEach((v) { | ||
if(v.onChange != null && v.onChange || onSubmit) { | ||
String r = v.validator(modelValue, f); | ||
_updateFieldValidStatus(r, f, modelValue[f.key], v); | ||
} | ||
}); | ||
} | ||
if(f.asyncValidators!=null && f.asyncValidators.length >0) { | ||
for(int j=0; j< f.asyncValidators.length; j++) { | ||
AsyncFormlyValidator v = f.asyncValidators[j]; | ||
if(v.onChange != null && v.onChange || onSubmit) { | ||
dynamic _v = await v.validator(modelValue, f); | ||
if(_v is Call && _v.reply == true) { | ||
_updateFieldValidStatus(null, f, modelValue[f.key], v); | ||
} else if(_v is Call && _v.reply!=null && _v.reply['error']!=null) { | ||
_updateFieldValidStatus(v.message ?? _v.reply['reason'], f, modelValue[f.key], v); | ||
} else { | ||
_updateFieldValidStatus(null, f, modelValue[f.key], v); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if(f.fields != null && f.fields.length > 0){ | ||
validateForm(flds: fields, onSubmit: onSubmit); | ||
} | ||
} | ||
|
||
_removeError(name) { | ||
if(_errors.remove(name)) _removeError(name); | ||
} | ||
} |
Oops, something went wrong.