Compare commits
3 Commits
58fec4bf48
...
61c6e637ec
Author | SHA1 | Date | |
---|---|---|---|
![]() |
61c6e637ec | ||
![]() |
efda077f4b | ||
![]() |
e00f4f55b7 |
@ -116,6 +116,19 @@ Widget onBuild(BuildContext context, CounterState state) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If needed, you can wrap what depends on the state with the function `onWrap`.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
@override
|
||||||
|
Widget onWrap(BuildContext context, Widget child) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Title'),
|
||||||
|
),
|
||||||
|
body: child,
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
> Note: you can override `onBuild`, but also `onListen`, `shouldBuildWhen` and `shouldListenWhen` methods.
|
> Note: you can override `onBuild`, but also `onListen`, `shouldBuildWhen` and `shouldListenWhen` methods.
|
||||||
|
|
||||||
### Both BlocProvider and BlocConsumer
|
### Both BlocProvider and BlocConsumer
|
||||||
|
@ -36,17 +36,20 @@ abstract class BlocBaseConsumerScreen<B extends BlocBase<S>, S extends Object>
|
|||||||
/// must return a widget.
|
/// must return a widget.
|
||||||
Widget onBuild(BuildContext context, S state);
|
Widget onBuild(BuildContext context, S state);
|
||||||
|
|
||||||
|
/// The [onWrap] function which will be invoked on each widget build.
|
||||||
|
/// The [onWrap] takes the `BuildContext`
|
||||||
|
/// Used to wrap which depends on the state.
|
||||||
|
Widget onWrap(BuildContext context, Widget child) => child;
|
||||||
|
|
||||||
/// Takes the `BuildContext` along with the `state`
|
/// Takes the `BuildContext` along with the `state`
|
||||||
/// and is responsible for executing in response to `state` changes.
|
/// and is responsible for executing in response to `state` changes.
|
||||||
void onListen(BuildContext context, S state) {}
|
void onListen(BuildContext context, S state) {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) => BlocConsumer<B, S>(
|
||||||
return BlocConsumer<B, S>(
|
|
||||||
listenWhen: shouldListenWhen,
|
listenWhen: shouldListenWhen,
|
||||||
listener: onListen,
|
listener: onListen,
|
||||||
buildWhen: shouldBuildWhen,
|
buildWhen: shouldBuildWhen,
|
||||||
builder: onBuild,
|
builder: (context, state) => onWrap(context, onBuild(context, state)),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,8 @@ abstract class BlocBaseProviderScreen<B extends BlocBase<S>, S extends Object>
|
|||||||
Widget buildChild(BuildContext context);
|
Widget buildChild(BuildContext context);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) => BlocProvider<B>(
|
||||||
return BlocProvider<B>(
|
|
||||||
create: (_) => create(context),
|
create: (_) => create(context),
|
||||||
child: Builder(builder: buildChild),
|
child: Builder(builder: buildChild),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,8 @@ abstract class BlocBaseScreen<B extends BlocBase<S>, S extends Object>
|
|||||||
B create(BuildContext context);
|
B create(BuildContext context);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) => BlocProvider<B>(
|
||||||
return BlocProvider<B>(
|
|
||||||
create: (_) => create(context),
|
create: (_) => create(context),
|
||||||
child: super.build(context),
|
child: super.build(context),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
/// [Bloc] and [Cubit] widgets.
|
/// [Bloc] and [Cubit] widgets.
|
||||||
mixin BlocBaseProviderMixin<B extends BlocBase<Object>> {
|
mixin BlocBaseProviderMixin<B extends BlocBase<Object>> {
|
||||||
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
|
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
|
||||||
B bloc(BuildContext context) => BlocProvider.of<B>(context);
|
B bloc(BuildContext context) => context.read<B>();
|
||||||
|
|
||||||
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
|
/// Returns the [BlocBase] used by this [BlocBaseProviderMixin].
|
||||||
R repo<R>(BuildContext context) => RepositoryProvider.of<R>(context);
|
R repo<R>(BuildContext context) => context.read<R>();
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,5 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||||||
/// [Bloc] specific mixin that provides implementation
|
/// [Bloc] specific mixin that provides implementation
|
||||||
/// of helper methods for events.
|
/// of helper methods for events.
|
||||||
mixin BlocProviderMixin<B extends Bloc<E, Object>, E> {
|
mixin BlocProviderMixin<B extends Bloc<E, Object>, E> {
|
||||||
void add(BuildContext context, E event) {
|
void add(BuildContext context, E event) => context.read<B>().add(event);
|
||||||
BlocProvider.of<B>(context).add(event);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user