From de1c7fbfcb4e32cf112692fa54966ab8a0e951a1 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Thu, 13 Apr 2023 20:18:45 +0200 Subject: [PATCH] refactor(bloc_helper): docs + nullable multiprovider attributes --- packages/wyatt_bloc_helper/README.md | 14 ++++---- .../lib/src/utils/smart_provider.dart | 7 ++++ .../lib/src/widgets/multi_provider.dart | 36 ++++++++++++++----- packages/wyatt_bloc_helper/pubspec.yaml | 14 ++++---- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/packages/wyatt_bloc_helper/README.md b/packages/wyatt_bloc_helper/README.md index 5b055eb4..ea4ffd4f 100644 --- a/packages/wyatt_bloc_helper/README.md +++ b/packages/wyatt_bloc_helper/README.md @@ -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 . --> -# Flutter - BloC Helper +# BloC Helper

- - Style: Wyatt Analysis - + Style: Wyatt Analysis SDK: Flutter

@@ -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 diff --git a/packages/wyatt_bloc_helper/lib/src/utils/smart_provider.dart b/packages/wyatt_bloc_helper/lib/src/utils/smart_provider.dart index 046da441..9ac68ddb 100644 --- a/packages/wyatt_bloc_helper/lib/src/utils/smart_provider.dart +++ b/packages/wyatt_bloc_helper/lib/src/utils/smart_provider.dart @@ -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, 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 repo( BuildContext context, { required Repository Function(BuildContext) create, diff --git a/packages/wyatt_bloc_helper/lib/src/widgets/multi_provider.dart b/packages/wyatt_bloc_helper/lib/src/widgets/multi_provider.dart index 4cb3c2bf..4ddfbea0 100644 --- a/packages/wyatt_bloc_helper/lib/src/widgets/multi_provider.dart +++ b/packages/wyatt_bloc_helper/lib/src/widgets/multi_provider.dart @@ -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 >[], + this.blocProviders = const [], 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, + ), + ); + } } diff --git a/packages/wyatt_bloc_helper/pubspec.yaml b/packages/wyatt_bloc_helper/pubspec.yaml index 00af1bf5..a133308c 100644 --- a/packages/wyatt_bloc_helper/pubspec.yaml +++ b/packages/wyatt_bloc_helper/pubspec.yaml @@ -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 \ No newline at end of file + version: ^2.4.1