-
Notifications
You must be signed in to change notification settings - Fork 1
/
di_scope.dart
43 lines (35 loc) · 1007 Bytes
/
di_scope.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import 'package:flutter/material.dart';
import 'package:places_elementary/features/app/di/app_scope.dart';
import 'package:provider/provider.dart';
/// Factory that returns the dependency scope.
typedef ScopeFactory<T> = T Function();
/// Di container. T - return type(for example [AppScope]).
class DiScope<T> extends StatefulWidget {
/// Factory that returns the dependency scope.
final ScopeFactory<T> factory;
/// The widget below this widget in the tree.
final Widget child;
/// Create an instance [DiScope].
const DiScope({
required this.factory,
required this.child,
Key? key,
}) : super(key: key);
@override
_DiScopeState createState() => _DiScopeState<T>();
}
class _DiScopeState<T> extends State<DiScope<T>> {
late T scope;
@override
void initState() {
super.initState();
scope = widget.factory();
}
@override
Widget build(BuildContext context) {
return Provider<T>(
create: (_) => scope,
child: widget.child,
);
}
}