flutter_demo_gps/lib/blocs/bloc_provider.dart

42 lines
922 B
Dart

import 'package:flutter/material.dart';
//
// Base for all BLoCs.
// This class was adapted from https://github.com/boeledi/Streams-Block-Reactive-Programming-in-Flutter
//
abstract class BlocBase {
void dispose();
}
class BlocProvider<T extends BlocBase> extends StatefulWidget {
const 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){
BlocProvider<T>? provider = context.findAncestorWidgetOfExactType<BlocProvider<T>>();
return provider!.bloc;
}
}
class BlocProviderState<T> extends State<BlocProvider<BlocBase>>{
@override
void dispose(){
widget.bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context){
return widget.child;
}
}