Skip to content

Commit

Permalink
flutter_bloc v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Oct 23, 2018
1 parent fe6e6de commit 961d378
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
10 changes: 9 additions & 1 deletion packages/flutter_bloc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ Initial Version of the library.

# 0.1.1

Minor Updates to Documentation
Minor Updates to Documentation

# 0.2.0

Updates to `BlocBuilder` and `BlocProvider`

- `BlocBuilder` does not automatically dispose a `Bloc`. Developers are now responsible for determining when to call `Bloc.dispose()`
- `BlocProvider` support for `of(context)` with generics
- Support for multiple nested `BlocProviders` with different Bloc Types.
1 change: 1 addition & 0 deletions packages/flutter_bloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ At this point we have sucessfully separated our presentational layer from our bu
## Examples

- [Simple Counter Example](https://github.com/felangel/Bloc/tree/master/packages/flutter_bloc/example) - an example of how to create a `CounterBloc` to implement the classic Flutter Counter app.
- [Login Flow Example](https://github.com/felangel/Bloc/tree/master/examples/flutter_login) - an example of how to use the `bloc` and `flutter_bloc` packages to implement a Login Flow.

### Contributors

Expand Down
18 changes: 1 addition & 17 deletions packages/flutter_bloc/lib/src/bloc_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef Widget BlocWidgetBuilder<E, S>(BuildContext context, S state);
/// [BlocBuilder] handles building the widget in response to new states.
/// BlocBuilder analagous to [StreamBuilder] but has simplified API
/// to reduce the amount of boilerplate code needed.
class BlocBuilder<E, S> extends StatefulWidget {
class BlocBuilder<E, S> extends StatelessWidget {
final Bloc<E, S> bloc;
final BlocWidgetBuilder<E, S> builder;

Expand All @@ -20,16 +20,6 @@ class BlocBuilder<E, S> extends StatefulWidget {
assert(builder != null),
super(key: key);

@override
State<StatefulWidget> createState() => _BlocBuilderState<E, S>(bloc, builder);
}

class _BlocBuilderState<E, S> extends State<BlocBuilder<E, S>> {
final Bloc<dynamic, S> bloc;
final BlocWidgetBuilder<E, S> builder;

_BlocBuilderState(this.bloc, this.builder);

@override
Widget build(BuildContext context) {
return StreamBuilder<S>(
Expand All @@ -40,10 +30,4 @@ class _BlocBuilderState<E, S> extends State<BlocBuilder<E, S>> {
},
);
}

@override
void dispose() {
bloc.dispose();
super.dispose();
}
}
12 changes: 8 additions & 4 deletions packages/flutter_bloc/lib/src/bloc_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:bloc/bloc.dart';
/// A Flutter widget which provides a bloc to its children via `BlocProvider.of(context)`.
/// It is used as a DI widget so that a single instance of a bloc can be provided
/// to multiple widgets within a subtree.
class BlocProvider extends InheritedWidget {
class BlocProvider<T extends Bloc<dynamic, dynamic>> extends InheritedWidget {
/// The Bloc which is to be made available throughout the subtree
final Bloc bloc;
final T bloc;

BlocProvider({
Key key,
Expand All @@ -24,6 +24,10 @@ class BlocProvider extends InheritedWidget {

/// Method that allows widgets to access the bloc as long as their `BuildContext`
/// contains a `BlocProvider` instance.
static Bloc of(BuildContext context) =>
(context.inheritFromWidgetOfExactType(BlocProvider) as BlocProvider).bloc;
static B of<B extends Bloc<dynamic, dynamic>>(BuildContext context) {
Type typeOf<B>() => B;
BlocProvider<B> provider =
context.ancestorWidgetOfExactType(typeOf<BlocProvider<B>>());
return provider.bloc;
}
}
2 changes: 1 addition & 1 deletion packages/flutter_bloc/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_bloc
description: Flutter Widgets that make it easy to implement the BLoC Design Pattern (Business Logic Component). Built to be used with the bloc package.
version: 0.1.1
version: 0.2.0
author: felix.angelov <felangelov@gmail.com>
homepage: https://github.com/felangel/bloc/tree/master/packages/flutter_bloc

Expand Down
10 changes: 5 additions & 5 deletions packages/flutter_bloc/test/bloc_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import 'package:flutter_bloc/flutter_bloc.dart';
class MyAppNoBloc extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
return BlocProvider<Bloc>(
bloc: null,
child: Container(),
);
}
}

class MyApp extends StatelessWidget {
final Bloc _bloc;
final CounterBloc _bloc;
final Widget _child;

const MyApp({Key key, @required Bloc bloc, @required Widget child})
const MyApp({Key key, @required CounterBloc bloc, @required Widget child})
: _bloc = bloc,
_child = child,
super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocProvider(
home: BlocProvider<CounterBloc>(
bloc: _bloc,
child: _child,
),
Expand All @@ -39,7 +39,7 @@ class MyApp extends StatelessWidget {
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
CounterBloc _counterBloc = BlocProvider.of(context) as CounterBloc;
CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context);
assert(_counterBloc != null);

return Scaffold(
Expand Down

0 comments on commit 961d378

Please sign in to comment.