WidgetModelFactory<T extends WidgetModel<ElementaryWidget<IWidgetModel> , ElementaryModel> > typedef
Type for a factory function that is used to create an instance of WidgetModel.
The part of Elementary Lifecycle
The WidgetModel instance is created for the ElementaryWidget when it
instantiate into the tree. Only in this moment the factory is called once(*)
to get the instance of the WidgetModel and associate it with
the Elementary
. After this the WidgetModel will alive while
the Elementary
is alive. If Elementary
updates the widget associated
with it, the WidgetModel will not be recreated and continue work with
the same state that has before the update. But the WidgetModel will be
notified about the update by calling the method
WidgetModel.didUpdateWidget. All needed state adjustments can be
handle inside this method.
(*) Once per insert into the tree. As all other widgets, an instance of ElementaryWidget can be inserted many times. Every time the element and separate WidgetModel will be created for manage different state for every concrete inserts.
Examples
{@tool snippet}
The following is a an example for the top-level factory function that creates dependencies right on the spot.
ExampleWidgetModel exampleWidgetModelFactory(BuildContext context) {
final modelDependency = ModelDependency();
final exampleModel = ExampleModel(modelDependency);
return ExampleWidgetModel(exampleModel);
}
{@end-tool}
{@tool snippet}
The following is a an example for the top-level factory function that creates dependencies using one of the DI containers.
ExampleWidgetModel exampleWidgetModelFactory(BuildContext context) {
final exampleModel = ContainerInstance.createExampleModel();
return ExampleWidgetModel(exampleModel);
}
{@end-tool}
{@tool snippet}
The following is a an example for the top-level factory function that get dependencies using passed BuildContext.
ExampleWidgetModel exampleWidgetModelFactory(BuildContext context) {
final modelDependency = ModelDependency();
final exampleModel = ExampleModel(modelDependency);
final widgetModelAnotherDependency = SomeWidget.of(context).getSomething;
return ExampleWidgetModel(exampleModel, widgetModelAnotherDependency);
}
{@end-tool}
Implementation
typedef WidgetModelFactory<T extends WidgetModel> = T Function(
BuildContext context,
);