Skip to content

Instantly share code, notes, and snippets.

@shishirthedev
Created July 28, 2019 14:04
Show Gist options
  • Save shishirthedev/7f4535ae9565925a91aeb54b6653a776 to your computer and use it in GitHub Desktop.
Save shishirthedev/7f4535ae9565925a91aeb54b6653a776 to your computer and use it in GitHub Desktop.
import 'package:blockpatter/base/BaseBloc.dart';
import 'package:flutter/material.dart';
class BlocProvider<T extends BaseBloc> extends StatefulWidget {
final Widget child;
final T bloc;
BlocProvider({Key key, @required this.bloc, @required this.child}) : super(key: key);
@override
_BlockProviderState<T> createState() => _BlockProviderState<T>();
static T of<T extends BaseBloc>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
return provider.bloc;
}
static Type _typeOf<T>() => T;
}
class _BlockProviderState<T extends BaseBloc> extends State<BlocProvider<T>> {
@override
Widget build(BuildContext context) {
return Container(child: widget.child);
}
@override
void dispose() {
widget.bloc.dispose();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment