Skip to content

Instantly share code, notes, and snippets.

@SergioEric
Created October 5, 2020 20:28
Show Gist options
  • Save SergioEric/e28a9e3f3f930af91a9bb05ef622261c to your computer and use it in GitHub Desktop.
Save SergioEric/e28a9e3f3f930af91a9bb05ef622261c to your computer and use it in GitHub Desktop.
// STATE
class ProductNotifier extends ChangeNotifier{
FormzStatus _status;
NameInput _name;
PriceInput _price;
DescriptionInput _description;
FormzStatus get status => this._status;
NameInput get name => this._name;
PriceInput get price => this._price;
DescriptionInput get description => this._description;
bool get completed => name.valid && price.valid && description.valid;
void onChangeName(String val){
this._name = NameInput.dirty(value: val);
notifyListeners();
}
...
}
//PROVIDERS
final productState = ChangeNotifierProvider.autoDispose<ProductNotifier>((ref){
return ProductNotifier();
});
final nameInputState = Provider.autoDispose((ref) {
return ref.watch(productState).name;
});
final priceInputState = Provider.autoDispose((ref) {
return ref.watch(productState).price;
});
// WIDGETS BUILDERS
Consumer(
builder: (_, watch, __) {
print("nameInputState");
final field = watch(nameInputState);
return Container(
child: TextFormField(
decoration: InputDecoration(
errorText: field?.error?.asString,
),
onChanged: (value) {
context.read(productState).onChangePrice(value);
},
),
);
},),
Consumer(
builder: (_, watch, __) {
print("PriceInputState");
final field = watch(priceInputState);
return Container(
child: TextFormField(
decoration: InputDecoration(
errorText: field?.error?.asString,
),
onChanged: (value) {
context.read(productState).onChangeName(value);
},
),
);
},),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment