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 |  * the Free Software Foundation, either version 3 of the License, or | ||||||
|  * any later version. |  * 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 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
|  * GNU General Public License for more details. |  * GNU General Public License for more details. | ||||||
| @ -16,12 +16,10 @@ | |||||||
|  * along with this program. If not, see <https://www.gnu.org/licenses/>. |  * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| --> | --> | ||||||
| 
 | 
 | ||||||
| # Flutter - BloC Helper | # BloC Helper | ||||||
| 
 | 
 | ||||||
| <p align="left"> | <p align="left"> | ||||||
|   <a href="https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/src/branch/master/packages/wyatt_analysis"> |   <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/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" /> |   <img src="https://img.shields.io/badge/SDK-Flutter-blue?style=flat-square" alt="SDK: Flutter" /> | ||||||
| </p> | </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 | ### 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 | ```dart | ||||||
| @override | @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 | ### Both BlocProvider and BlocConsumer | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -17,7 +17,12 @@ | |||||||
| import 'package:flutter/widgets.dart'; | import 'package:flutter/widgets.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.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 { | abstract class SmartProvider { | ||||||
|  |   /// Creates a [BlocProvider] with a [Bloc] that is possibly already | ||||||
|  |   /// available in the widget tree. | ||||||
|   static BlocProvider<Bloc> |   static BlocProvider<Bloc> | ||||||
|       bloc<Bloc extends BlocBase<State>, State extends Object>( |       bloc<Bloc extends BlocBase<State>, State extends Object>( | ||||||
|     BuildContext context, { |     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>( |   static RepositoryProvider<Repository> repo<Repository>( | ||||||
|     BuildContext context, { |     BuildContext context, { | ||||||
|     required Repository Function(BuildContext) create, |     required Repository Function(BuildContext) create, | ||||||
|  | |||||||
| @ -77,9 +77,9 @@ import 'package:flutter_bloc/flutter_bloc.dart'; | |||||||
| class MultiProvider extends StatelessWidget { | class MultiProvider extends StatelessWidget { | ||||||
|   /// {@macro multi_provider} |   /// {@macro multi_provider} | ||||||
|   const MultiProvider({ |   const MultiProvider({ | ||||||
|     required this.repositoryProviders, |  | ||||||
|     required this.blocProviders, |  | ||||||
|     required this.child, |     required this.child, | ||||||
|  |     this.repositoryProviders = const <RepositoryProvider<dynamic>>[], | ||||||
|  |     this.blocProviders = const <BlocProvider>[], | ||||||
|     super.key, |     super.key, | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
| @ -88,11 +88,31 @@ class MultiProvider extends StatelessWidget { | |||||||
|   final Widget child; |   final Widget child; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) => MultiRepositoryProvider( |   Widget build(BuildContext context) { | ||||||
|         providers: repositoryProviders, |     if (repositoryProviders.isEmpty && blocProviders.isEmpty) { | ||||||
|         child: MultiBlocProvider( |       return child; | ||||||
|           providers: blocProviders, |     } | ||||||
|           child: 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 | publish_to: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub | ||||||
| 
 | 
 | ||||||
| environment: | environment: | ||||||
|   sdk: '>=2.17.0 <3.0.0' |   sdk: ">=2.17.0 <3.0.0" | ||||||
| 
 | 
 | ||||||
| dependencies: | dependencies: | ||||||
|   flutter: |   flutter: { sdk: flutter } | ||||||
|     sdk: flutter | 
 | ||||||
|    |  | ||||||
|   flutter_bloc: ^8.1.1 |   flutter_bloc: ^8.1.1 | ||||||
|   equatable: ^2.0.5 |   equatable: ^2.0.5 | ||||||
| 
 | 
 | ||||||
| dev_dependencies: | dev_dependencies: | ||||||
|   flutter_test: |   flutter_test: { sdk: flutter } | ||||||
|     sdk: flutter |  | ||||||
|   bloc_test: ^9.1.0 |   bloc_test: ^9.1.0 | ||||||
|    | 
 | ||||||
|   wyatt_analysis: |   wyatt_analysis: | ||||||
|     hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub |     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