feat(bloc): add init function and multi provider
This commit is contained in:
@@ -22,6 +22,7 @@ abstract class SmartProvider {
|
||||
bloc<Bloc extends BlocBase<State>, State extends Object>(
|
||||
BuildContext context, {
|
||||
required Bloc Function(BuildContext) create,
|
||||
required Bloc Function(BuildContext, Bloc) init,
|
||||
Widget? child,
|
||||
bool lazy = true,
|
||||
bool enable = true,
|
||||
@@ -29,8 +30,9 @@ abstract class SmartProvider {
|
||||
if (enable) {
|
||||
final bloc = context.read<Bloc?>();
|
||||
if (bloc != null) {
|
||||
final b = bloc;
|
||||
return BlocProvider<Bloc>.value(
|
||||
value: bloc,
|
||||
value: init(context, b),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
@@ -38,7 +40,7 @@ abstract class SmartProvider {
|
||||
|
||||
return BlocProvider<Bloc>(
|
||||
lazy: lazy,
|
||||
create: (_) => create(context),
|
||||
create: (_) => init(context, create(context)),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
@@ -46,6 +48,7 @@ abstract class SmartProvider {
|
||||
static RepositoryProvider<Repository> repo<Repository>(
|
||||
BuildContext context, {
|
||||
required Repository Function(BuildContext) create,
|
||||
required Repository Function(BuildContext, Repository) init,
|
||||
Widget? child,
|
||||
bool lazy = true,
|
||||
bool enable = true,
|
||||
@@ -53,8 +56,9 @@ abstract class SmartProvider {
|
||||
if (enable) {
|
||||
final repo = context.read<Repository?>();
|
||||
if (repo != null) {
|
||||
final r = repo;
|
||||
return RepositoryProvider<Repository>.value(
|
||||
value: repo,
|
||||
value: init(context, r),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
@@ -62,7 +66,7 @@ abstract class SmartProvider {
|
||||
|
||||
return RepositoryProvider<Repository>(
|
||||
lazy: lazy,
|
||||
create: (_) => create(context),
|
||||
create: (_) => init(context, create(context)),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user