Guest User

Untitled

a guest
Nov 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. debugShowCheckedModeBanner: false,
  11. home: BlocProvider(
  12. bloc: CounterBloc(),
  13. child: MyHomePage(),
  14. ),
  15. );
  16. }
  17. }
  18.  
  19. class MyHomePage extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context);
  23. return Scaffold(
  24. appBar: AppBar(
  25. title: Text("Bloc Provider"),
  26. actions: <Widget>[
  27. CartCount(),
  28. ],
  29. ),
  30. body: Center(
  31. child: StreamBuilder<int>(
  32. initialData: 0,
  33. stream: _counterBloc.outCounter,
  34. builder: (context, snapshot) {
  35. return Text(snapshot.data.toString(),
  36. style: Theme.of(context).textTheme.display1);
  37. },
  38. ),
  39. ),
  40. floatingActionButton: FloatingActionButton(
  41. onPressed: () {
  42. _counterBloc.incrementCount();
  43. },
  44. child: Icon(Icons.add),
  45. ),
  46. );
  47. }
  48. }
  49.  
  50. class CounterBloc implements BlocBase {
  51. int _counter = 0;
  52.  
  53. StreamController<int> _counterController = StreamController<int>.broadcast();
  54.  
  55. Stream<int> get outCounter => _counterController.stream;
  56.  
  57. @override
  58. void dispose() {
  59. _counterController.close();
  60. }
  61.  
  62. void incrementCount() {
  63. _counter++;
  64. _counterController.sink.add(_counter);
  65. }
  66. }
  67.  
  68. abstract class BlocBase {
  69. void dispose();
  70. }
  71.  
  72. // Generic BLoC provider
  73. class BlocProvider<T extends BlocBase> extends StatefulWidget {
  74. BlocProvider({
  75. Key key,
  76. @required this.child,
  77. @required this.bloc,
  78. }) : super(key: key);
  79.  
  80. final T bloc;
  81. final Widget child;
  82.  
  83. @override
  84. _BlocProviderState<T> createState() => _BlocProviderState<T>();
  85.  
  86. static T of<T extends BlocBase>(BuildContext context) {
  87. final type = _typeOf<BlocProvider<T>>();
  88. BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
  89. return provider.bloc;
  90. }
  91.  
  92. static Type _typeOf<T>() => T;
  93. }
  94.  
  95. class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
  96. @override
  97. void dispose() {
  98. widget.bloc.dispose();
  99. super.dispose();
  100. }
  101.  
  102. @override
  103. Widget build(BuildContext context) {
  104. return widget.child;
  105. }
  106. }
  107.  
  108. class CartCount extends StatelessWidget {
  109. @override
  110. Widget build(BuildContext context) {
  111. CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context);
  112. print(_counterBloc);
  113. return Center(
  114. child: Stack(
  115. fit: StackFit.loose,
  116. children: <Widget>[
  117. Align(
  118. alignment: AlignmentDirectional.center,
  119. child: Padding(
  120. padding: const EdgeInsets.only(right: 16.0),
  121. child: Icon(Icons.shopping_cart),
  122. )),
  123. Align(
  124. alignment: AlignmentDirectional.topEnd,
  125. child: Padding(
  126. padding: const EdgeInsets.only(left: 16.0),
  127. child: CircleAvatar(
  128. backgroundColor: Colors.red,
  129. radius: 10.0,
  130. child: StreamBuilder<int>(
  131. initialData: 0,
  132. stream: _counterBloc.outCounter,
  133. builder: (context, snapshot) {
  134. return Text(snapshot.data.toString());
  135. },
  136. ),
  137. ),
  138. ),
  139. ),
  140. ],
  141. ));
  142. }
  143. }
Add Comment
Please, Sign In to add comment