Created
October 5, 2020 20:28
-
-
Save SergioEric/e28a9e3f3f930af91a9bb05ef622261c to your computer and use it in GitHub Desktop.
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
// 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