Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. abstract class BlocBase {
  2. void dispose();
  3. }
  4.  
  5. class BlocProvider<T extends BlocBase> extends StatefulWidget {
  6. BlocProvider({
  7. Key key,
  8. @required this.child,
  9. @required this.bloc,
  10. }) : super(key: key);
  11. final T bloc;
  12. final Widget child;
  13. @override
  14. State<StatefulWidget> createState() => _BlocProviderState<T>();
  15. static T of<T extends BlocBase>(BuildContext context) {
  16. final type = _typeOf<_BlocProviderInherited<T>>();
  17. _BlocProviderInherited<T> provider = context
  18. .ancestorInheritedElementForWidgetOfExactType(type)
  19. ?.widget;
  20. return provider?.bloc;
  21. }
  22. static Type _typeOf<T>() => T;
  23. }
  24.  
  25. class _BlocProviderState<T extends BlocBase> extends State<BlocProvider<T>> {
  26. @override
  27. Widget build(BuildContext context) {
  28. return new _BlocProviderInherited(
  29. child: widget.child,
  30. bloc: widget.bloc
  31. );
  32. }
  33. @override
  34. void dispose() {
  35. widget.bloc?.dispose();
  36. super.dispose();
  37. }
  38. }
  39.  
  40. class _BlocProviderInherited<T> extends InheritedWidget {
  41. _BlocProviderInherited({
  42. Key key,
  43. @required Widget child,
  44. @required this.bloc
  45. }) : super(key: key, child: child);
  46. final T bloc;
  47. @override
  48. bool updateShouldNotify(InheritedWidget oldWidget) => false;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement