-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WithInjector and WithInstance widgets
- Loading branch information
1 parent
9b13ee8
commit 05b2a1d
Showing
7 changed files
with
77 additions
and
21 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
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
This file was deleted.
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
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
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,62 @@ | ||
part of 'package:dependencies_flutter/dependencies_flutter.dart'; | ||
|
||
/// Builder to construct a [Widget] for the given [Injector]. | ||
typedef Widget InjectorBuilder(Injector injector); | ||
|
||
/// Widget able to build it's child from the [Injector] in the | ||
/// current [BuildContext]. | ||
class WithInjectorWidget extends StatelessWidget with InjectorWidgetMixin { | ||
final InjectorBuilder builder; | ||
|
||
const WithInjectorWidget._internal({Key key, this.builder}) : super(key: key); | ||
|
||
factory WithInjectorWidget({Key key, InjectorBuilder builder}) { | ||
checkNotNull(builder, message: () => "builder can't be null"); | ||
return WithInjectorWidget._internal( | ||
key: key, | ||
builder: builder, | ||
); | ||
} | ||
|
||
@override | ||
Widget buildWithInjector(BuildContext context, Injector injector) { | ||
return builder(injector); | ||
} | ||
} | ||
|
||
/// Builder to construct a [Widget] for the given instance of type [T] | ||
typedef Widget InstanceBuilder<T>(T instance); | ||
|
||
/// Widget able to build it's child from the instance found in the [Injector] | ||
/// in the current [BuildContext]. | ||
class WithInstanceWidget<T> extends StatelessWidget { | ||
final InstanceBuilder builder; | ||
final String name; | ||
final Params params; | ||
|
||
const WithInstanceWidget._internal({ | ||
Key key, | ||
this.builder, | ||
this.name, | ||
this.params, | ||
}) : super(key: key); | ||
|
||
factory WithInstanceWidget( | ||
{Key key, InstanceBuilder builder, String name, Params params}) { | ||
checkNotNull(builder, message: () => "builder can't be null"); | ||
return WithInstanceWidget._internal( | ||
key: key, | ||
builder: builder, | ||
name: name, | ||
params: params, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return WithInjectorWidget(builder: (injector) { | ||
final instance = injector.get<T>(name: name, params: params); | ||
return builder(instance); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: dependencies_flutter | ||
description: A simple and modular dependency injection system for Flutter. | ||
version: 0.1.3 | ||
version: 0.1.4 | ||
homepage: https://github.com/marcguilera/dart_dependencies_flutter | ||
author: marcguilera <[email protected]> | ||
|
||
|