refactor(bloc_helper): docs + nullable multiprovider attributes
This commit is contained in:
		
							parent
							
								
									72b27b27ee
								
							
						
					
					
						commit
						468aa72635
					
				| @ -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> | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -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,7 +88,26 @@ class MultiProvider extends StatelessWidget { | |||||||
|   final Widget child; |   final Widget child; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) => MultiRepositoryProvider( |   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, |       providers: repositoryProviders, | ||||||
|       child: MultiBlocProvider( |       child: MultiBlocProvider( | ||||||
|         providers: blocProviders, |         providers: blocProviders, | ||||||
| @ -96,3 +115,4 @@ class MultiProvider extends StatelessWidget { | |||||||
|       ), |       ), | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|  | } | ||||||
|  | |||||||
| @ -6,18 +6,16 @@ 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: | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user