Created
November 19, 2018 03:51
-
-
Save burhanrashid52/b148c2d15c4d95fb33a82e1199570c48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: BlocProvider( | |
bloc: CounterBloc(), | |
child: MyHomePage(), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Bloc Provider"), | |
actions: <Widget>[ | |
CartCount(), | |
], | |
), | |
body: Center( | |
child: StreamBuilder<int>( | |
initialData: 0, | |
stream: _counterBloc.outCounter, | |
builder: (context, snapshot) { | |
return Text(snapshot.data.toString(), | |
style: Theme.of(context).textTheme.display1); | |
}, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
_counterBloc.incrementCount(); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class CounterBloc implements BlocBase { | |
int _counter = 0; | |
StreamController<int> _counterController = StreamController<int>.broadcast(); | |
Stream<int> get outCounter => _counterController.stream; | |
@override | |
void dispose() { | |
_counterController.close(); | |
} | |
void incrementCount() { | |
_counter++; | |
_counterController.sink.add(_counter); | |
} | |
} | |
abstract class BlocBase { | |
void dispose(); | |
} | |
// Generic BLoC provider | |
class BlocProvider<T extends BlocBase> extends StatefulWidget { | |
BlocProvider({ | |
Key key, | |
@required this.child, | |
@required this.bloc, | |
}) : super(key: key); | |
final T bloc; | |
final Widget child; | |
@override | |
_BlocProviderState<T> createState() => _BlocProviderState<T>(); | |
static T of<T extends BlocBase>(BuildContext context) { | |
final type = _typeOf<BlocProvider<T>>(); | |
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type); | |
return provider.bloc; | |
} | |
static Type _typeOf<T>() => T; | |
} | |
class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> { | |
@override | |
void dispose() { | |
widget.bloc.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return widget.child; | |
} | |
} | |
class CartCount extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context); | |
print(_counterBloc); | |
return Center( | |
child: Stack( | |
fit: StackFit.loose, | |
children: <Widget>[ | |
Align( | |
alignment: AlignmentDirectional.center, | |
child: Padding( | |
padding: const EdgeInsets.only(right: 16.0), | |
child: Icon(Icons.shopping_cart), | |
)), | |
Align( | |
alignment: AlignmentDirectional.topEnd, | |
child: Padding( | |
padding: const EdgeInsets.only(left: 16.0), | |
child: CircleAvatar( | |
backgroundColor: Colors.red, | |
radius: 10.0, | |
child: StreamBuilder<int>( | |
initialData: 0, | |
stream: _counterBloc.outCounter, | |
builder: (context, snapshot) { | |
return Text(snapshot.data.toString()); | |
}, | |
), | |
), | |
), | |
), | |
], | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment