几周前我读到了颤振钩子,现在想在我的新项目中实现它。我的“基础”小部件是一个有状态的小部件,它具有RouteAware
某些项目原因的混合原因。此外,每个人都有一个提供BehaviourSubject
. 该 bloc 必须由 Widget 处理,这就是RouteAware
它的 a的原因和原因StatefulWidget
。我在这里没有详细介绍,但是 bloc 是用许多依赖项构建的,并且像这样 MyWidget(bloc: //resolve bloc here) 传递。
有人可以在这里帮我将其转换为 aHookWidget
以及如何添加useAware
Hook 吗?
class MyBloc{
void dispose(){}
void didPopNext(){}
void didPush(){}
}
class MyWidget extends StatefulWidget{
final MyBloc bloc;
MyWidget({key, this.bloc}) : super(key: key);
@override
MyWidgetState createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> with RouteAware{
@override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void dispose() {
routeObserver.unsubscribe(this);
widget.bloc.dispose();
super.dispose();
}
@override
void didPush() {
// Route was pushed onto navigator and is now topmost route.
widget.bloc.didPush();
}
@override
void didPopNext() {
// Covering route was popped off the navigator.
widget.bloc.didPopNext();
}
@override
Widget build(BuildContext context) => StreamBuilder(stream: widget.bloc.myStream, initialValue: widget.bloc.myStream.value, builder: (context, snapshot){
//work with snapshot
});
}