refactor(bloc_helper): docs + nullable multiprovider attributes
This commit is contained in:
parent
72b27b27ee
commit
468aa72635
@ -7,7 +7,7 @@
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
@ -16,12 +16,10 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
# Flutter - BloC Helper
|
||||
# BloC Helper
|
||||
|
||||
<p align="left">
|
||||
<a href="https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/src/branch/master/packages/wyatt_analysis">
|
||||
<img src="https://img.shields.io/badge/Style-Wyatt%20Analysis-blue.svg?style=flat-square" alt="Style: Wyatt Analysis" />
|
||||
</a>
|
||||
<a href="https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/src/branch/master/packages/wyatt_analysis"><img src="https://img.shields.io/badge/Style-Wyatt%20Analysis-blue.svg?style=flat-square" alt="Style: Wyatt Analysis" /></a>
|
||||
<img src="https://img.shields.io/badge/SDK-Flutter-blue?style=flat-square" alt="SDK: Flutter" />
|
||||
</p>
|
||||
|
||||
@ -86,7 +84,7 @@ Widget buildChild(BuildContext context) {
|
||||
}
|
||||
```
|
||||
|
||||
> Note: here, you can use BlocBuilder, BlocListener,... and access Bloc and Repository instance with the mixin helper.
|
||||
> Note: here, you can use BlocBuilder, BlocListener, ... and access Bloc and Repository instance with the mixin helper.
|
||||
|
||||
### Only BlocConsumer
|
||||
|
||||
@ -116,7 +114,7 @@ Widget onBuild(BuildContext context, CounterState state) {
|
||||
}
|
||||
```
|
||||
|
||||
If needed, you can wrap what depends on the state with the function `parent`.
|
||||
If needed, you can wrap what depends on the state with the function `parent` .
|
||||
|
||||
```dart
|
||||
@override
|
||||
@ -129,7 +127,7 @@ Widget parent(BuildContext context, Widget 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
|
||||
|
||||
|
@ -17,7 +17,12 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
/// A utility class that provides a way to create a [BlocProvider] or
|
||||
/// [RepositoryProvider] with a [Bloc] or Repository that is already
|
||||
/// available in the widget tree.
|
||||
abstract class SmartProvider {
|
||||
/// Creates a [BlocProvider] with a [Bloc] that is possibly already
|
||||
/// available in the widget tree.
|
||||
static BlocProvider<Bloc>
|
||||
bloc<Bloc extends BlocBase<State>, State extends Object>(
|
||||
BuildContext context, {
|
||||
@ -45,6 +50,8 @@ abstract class SmartProvider {
|
||||
);
|
||||
}
|
||||
|
||||
/// Creates a [RepositoryProvider] with a Repository that is possibly
|
||||
/// already available in the widget tree.
|
||||
static RepositoryProvider<Repository> repo<Repository>(
|
||||
BuildContext context, {
|
||||
required Repository Function(BuildContext) create,
|
||||
|
@ -77,9 +77,9 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
class MultiProvider extends StatelessWidget {
|
||||
/// {@macro multi_provider}
|
||||
const MultiProvider({
|
||||
required this.repositoryProviders,
|
||||
required this.blocProviders,
|
||||
required this.child,
|
||||
this.repositoryProviders = const <RepositoryProvider<dynamic>>[],
|
||||
this.blocProviders = const <BlocProvider>[],
|
||||
super.key,
|
||||
});
|
||||
|
||||
@ -88,11 +88,31 @@ class MultiProvider extends StatelessWidget {
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => MultiRepositoryProvider(
|
||||
providers: repositoryProviders,
|
||||
child: MultiBlocProvider(
|
||||
providers: blocProviders,
|
||||
child: child,
|
||||
),
|
||||
Widget build(BuildContext context) {
|
||||
if (repositoryProviders.isEmpty && blocProviders.isEmpty) {
|
||||
return child;
|
||||
}
|
||||
|
||||
if (repositoryProviders.isEmpty) {
|
||||
return MultiBlocProvider(
|
||||
providers: blocProviders,
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
if (blocProviders.isEmpty) {
|
||||
return MultiRepositoryProvider(
|
||||
providers: repositoryProviders,
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
return MultiRepositoryProvider(
|
||||
providers: repositoryProviders,
|
||||
child: MultiBlocProvider(
|
||||
providers: blocProviders,
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -6,20 +6,18 @@ version: 2.0.0
|
||||
publish_to: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub
|
||||
|
||||
environment:
|
||||
sdk: '>=2.17.0 <3.0.0'
|
||||
sdk: ">=2.17.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
flutter: { sdk: flutter }
|
||||
|
||||
flutter_bloc: ^8.1.1
|
||||
equatable: ^2.0.5
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
flutter_test: { sdk: flutter }
|
||||
bloc_test: ^9.1.0
|
||||
|
||||
|
||||
wyatt_analysis:
|
||||
hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub
|
||||
version: ^2.4.1
|
||||
version: ^2.4.1
|
||||
|
Loading…
x
Reference in New Issue
Block a user