LCOV - code coverage report
Current view: top level - view - view_state_builder.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 15 15 100.0 %
Date: 2020-03-25 12:10:52 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:bloc/bloc.dart';
       2             : import 'package:flutter/widgets.dart';
       3             : import 'package:flutter_bloc/flutter_bloc.dart';
       4             : import 'package:flutter_bloc_patterns/src/view/view_state.dart';
       5             : 
       6             : /// Builder function for the the initial state.
       7             : typedef InitialBuilder = Widget Function(BuildContext context);
       8             : 
       9             : /// Builder function for the data loading state.
      10             : typedef LoadingBuilder = Widget Function(BuildContext context);
      11             : 
      12             : /// Builder function for a success state. The data was fetched and nonnull
      13             : /// element was returned.
      14             : typedef SuccessBuilder<T> = Widget Function(BuildContext context, T data);
      15             : 
      16             : /// Builder function for the data refreshing state. Can only occur after
      17             : /// [SuccessBuilder].
      18             : typedef RefreshingBuilder<T> = Widget Function(BuildContext context, T data);
      19             : 
      20             : /// Builder function for no result. The data was fetched
      21             : /// successfully, but a null element was returned.
      22             : typedef EmptyBuilder = Widget Function(BuildContext context);
      23             : 
      24             : /// Builder function for an error. It contains an [error] that has caused
      25             : /// which may allow a view to react differently on different errors.
      26             : typedef ErrorBuilder = Widget Function(
      27             :   BuildContext context,
      28             :   dynamic error,
      29             : );
      30             : 
      31             : /// [ViewStateBuilder] is responsible for building the UI based on the [ViewState].
      32             : /// It's a wrapper over the [BlocBuilder] widget so it accepts a [bloc] object and
      33             : /// a set of handy callbacks, which corresponds to each possible state:
      34             : /// [onReady] builder for the the initial state,
      35             : /// [onLoading] builder for the data loading state,
      36             : /// [onRefreshing] builder for the data refreshing state,
      37             : /// [onSuccess] builder for the data success state,
      38             : /// [onEmpty] builder for for no result state,
      39             : /// [onError] builder function for an error state.
      40             : ///
      41             : /// [T] - the type of elements,
      42             : /// [B] - the type of bloc.
      43             : class ViewStateBuilder<T, B extends Bloc<dynamic, ViewState>>
      44             :     extends BlocBuilder<B, ViewState> {
      45           1 :   ViewStateBuilder({
      46             :     Key key,
      47             :     B bloc,
      48             :     InitialBuilder onReady,
      49             :     LoadingBuilder onLoading,
      50             :     RefreshingBuilder<T> onRefreshing,
      51             :     SuccessBuilder<T> onSuccess,
      52             :     EmptyBuilder onEmpty,
      53             :     ErrorBuilder onError,
      54             :     BlocBuilderCondition<ViewState> condition,
      55           1 :   }) : super(
      56             :           key: key,
      57             :           bloc: bloc,
      58             :           condition: condition,
      59           1 :           builder: (BuildContext context, ViewState state) {
      60           1 :             if (state is Initial) {
      61           1 :               return onReady?.call(context) ?? const SizedBox();
      62           1 :             } else if (state is Loading) {
      63           1 :               return onLoading?.call(context) ?? const SizedBox();
      64           1 :             } else if (state is Refreshing<T>) {
      65           2 :               return onRefreshing?.call(context, state.data) ??
      66             :                   const SizedBox();
      67           1 :             } else if (state is Success<T>) {
      68           2 :               return onSuccess?.call(context, state.data) ?? const SizedBox();
      69           1 :             } else if (state is Empty) {
      70           1 :               return onEmpty?.call(context) ?? const SizedBox();
      71           1 :             } else if (state is Failure) {
      72           2 :               return onError?.call(context, state.error) ?? const SizedBox();
      73             :             } else {
      74             :               return const SizedBox();
      75             :             }
      76             :           },
      77             :         );
      78             : }

Generated by: LCOV version 1.14