Skip to content

Commit

Permalink
Implemented basic formly fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ansaries committed Apr 27, 2019
0 parents commit 10b119f
Show file tree
Hide file tree
Showing 33 changed files with 1,397 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
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.*
19 changes: 19 additions & 0 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .metadata
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
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.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
14 changes: 14 additions & 0 deletions README.md
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.
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;
}
}
19 changes: 19 additions & 0 deletions flutter_formly.iml
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>
1 change: 1 addition & 0 deletions lib/flutter_formly.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export './formly/formly.dart';
5 changes: 5 additions & 0 deletions lib/formly/bloc/field_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of formly;

// class FormlyFieldBloc extends Object with Validators {

// }
81 changes: 81 additions & 0 deletions lib/formly/bloc/formly_bloc.dart
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();
}


}
31 changes: 31 additions & 0 deletions lib/formly/bloc/formly_events.dart
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});
}
17 changes: 17 additions & 0 deletions lib/formly/bloc/formly_provider.dart
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;
}
}
73 changes: 73 additions & 0 deletions lib/formly/bloc/formly_validators.dart
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);
}
}
Loading

0 comments on commit 10b119f

Please sign in to comment.