bloc_for_all 0.0.1 copy "bloc_for_all: ^0.0.1" to clipboard
bloc_for_all: ^0.0.1 copied to clipboard

discontinued

A new Flutter package.

example/lib/main.dart

import 'package:bloc_for_all/api/api_stream_builder.dart';
import 'package:example/movie_bloc.dart';
import 'package:example/movie_response.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Network Handling Demo',
      home: MovieScreen(),
    );
  }
}

class MovieScreen extends StatefulWidget {
  @override
  _MovieScreenState createState() => _MovieScreenState();
}

class _MovieScreenState extends State<MovieScreen> {
  MovieBloc _bloc;

  @override
  void initState() {
    super.initState();
    _bloc = MovieBloc();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        title: Text('Movie Mania',
            style: TextStyle(color: Colors.lightGreen, fontSize: 28)),
        backgroundColor: Colors.black54,
      ),
      backgroundColor: Colors.black54,
      body: RefreshIndicator(
          onRefresh: () => _bloc.fetchMovieList(),
          child:

//        StreamBuilder<ApiResponse<List<Movie>>>(
//
//
//          stream: _bloc.movieListStream,
//          builder: (context, snapshot) {
//            if (snapshot.hasData) {
//              switch (snapshot.data.status) {
//                case Status.LOADING:
//                  return Loading(loadingMessage: snapshot.data.message);
//                  break;
//                case Status.COMPLETED:
//                  return MovieList(movieList: snapshot.data.data);
//                  break;
//                case Status.ERROR:
//                  return Error(
//                    errorMessage: snapshot.data.message,
//                    onRetryPressed: () => _bloc.fetchMovieList(),
//                  );
//                  break;
//              }
//            }
//            return Container();
//          },
//        ),

              ApiStreamBuilder<List<Movie>>(
            stream: _bloc.apiDataSinkStream,
            loadingWidget: (value) {
              return Loading(loadingMessage: value);
            },
            errorWidget: (value) {
              return Error(
                errorMessage: value,
                onRetryPressed: () => _bloc.fetchMovieList(),
              );
            },
            dataWidget: (value) {
              return MovieList(movieList: value);
            },
          )),
    );
  }

  @override
  void dispose() {
    _bloc.dispose();
    super.dispose();
  }
}

class MovieList extends StatelessWidget {
  final List<Movie> movieList;

  const MovieList({Key key, this.movieList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      itemCount: movieList.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 1.5 / 1.8,
      ),
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Card(
            child: Padding(
              padding: const EdgeInsets.all(4.0),
              child: Image.network(
                'https://image.tmdb.org/t/p/w342${movieList[index].posterPath}',
                fit: BoxFit.fill,
              ),
            ),
          ),
        );
      },
    );
  }
}

class Error extends StatelessWidget {
  final String errorMessage;

  final Function onRetryPressed;

  const Error({Key key, this.errorMessage, this.onRetryPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            errorMessage,
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.lightGreen,
              fontSize: 18,
            ),
          ),
          SizedBox(height: 8),
          RaisedButton(
            color: Colors.lightGreen,
            child: Text('Retry', style: TextStyle(color: Colors.white)),
            onPressed: onRetryPressed,
          )
        ],
      ),
    );
  }
}

class Loading extends StatelessWidget {
  final String loadingMessage;

  const Loading({Key key, this.loadingMessage}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            loadingMessage,
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.lightGreen,
              fontSize: 24,
            ),
          ),
          SizedBox(height: 24),
          CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.lightGreen),
          ),
        ],
      ),
    );
  }
}