Skip to content

Commit

Permalink
Added WithInjector and WithInstance widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
marcguilera committed Nov 13, 2018
1 parent 9b13ee8 commit 05b2a1d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MyModule implements Module {
class SomeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final injector = FlutterInjector.of(context);
final injector = InjectorWidget.of(context);
final apiKey = injector.get(name: "api_key");
print(apiKey);
return Container();
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import 'package:quiver/check.dart';
part 'src/injector_widget.dart';
part 'src/injector_widget_mixin.dart';
part 'src/module_widget.dart';
part 'src/flutter_injector.dart';
part 'src/error.dart';
part 'src/instance_widget.dart';
17 changes: 0 additions & 17 deletions lib/src/flutter_injector.dart

This file was deleted.

11 changes: 11 additions & 0 deletions lib/src/injector_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ class InjectorWidget extends StatefulWidget {

@override
_InjectorWidgetState createState() => _InjectorWidgetState();

/// Gets an [Injector] from a ]BuildContext]
static Injector of(BuildContext context) {
final w = context.inheritFromWidgetOfExactType(_InjectorInheritedWidget);
if (w == null) {
throw InjectorWidgetError._internal(
"No `InjectorWidget` was found in the context.");
}
final injectorWidget = w as _InjectorInheritedWidget;
return injectorWidget.injector;
}
}

class _InjectorWidgetState extends State<InjectorWidget> {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/injector_widget_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'package:dependencies_flutter/dependencies_flutter.dart';
/// This can be applied to [State] or to [StatelessWidget] classes.
abstract class InjectorWidgetMixin {
Widget build(BuildContext context) {
return buildWithInjector(context, FlutterInjector.of(context));
return buildWithInjector(context, InjectorWidget.of(context));
}

/// Build the [Widget] with the existing [Injector].
Expand Down
62 changes: 62 additions & 0 deletions lib/src/instance_widget.dart
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);
});
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
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]>

Expand Down

0 comments on commit 05b2a1d

Please sign in to comment.