Ste*_*ann 0 flutter flutter-dependencies flutter-hooks
几周前我读到了关于flutter hooks 的内容,现在想在我的新项目中实现它。我的“基本”小部件是一个有状态的小部件,它具有某些项目原因的混合RouteAware原因。此外,每个人都有一个提供BehaviourSubject. RouteAware该块必须由 Widget 处理,这就是其 a的原因StatefulWidget。我在这里没有详细介绍,但 bloc 是通过许多依赖项构建的,并且像这样 MyWidget(bloc: //resolve bloc here) 一样传递。
有人可以帮我将其转换为 aHookWidget以及如何添加useAwareHook 吗?
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
});
}
Run Code Online (Sandbox Code Playgroud)
要使用钩子调用处置方法,您可以使用useEffect钩子,其中函数的返回是您要用于处置的方法
useEffect((){
return bloc.dispose
}, const [])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2524 次 |
| 最近记录: |