Flutter 我如何使用Flatter_bloc完美地处理屏幕导航?
在使用Flatter_bloc时,我使用blocbuilder根据当前状态更改屏幕。现在我的问题是,如何使用phones back按钮返回到以前的状态以浏览以前的屏幕?您可以使用Widget**WillPopScope包装您的构建方法** 例如:Flutter 我如何使用Flatter_bloc完美地处理屏幕导航?,flutter,bloc,Flutter,Bloc,在使用Flatter_bloc时,我使用blocbuilder根据当前状态更改屏幕。现在我的问题是,如何使用phones back按钮返回到以前的状态以浏览以前的屏幕?您可以使用Widget**WillPopScope包装您的构建方法** 例如: @override Widget build(BuildContext context) { return new WillPopScope( onWillPop: _onWillPop, child: /// your
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: _onWillPop,
child: /// your widget,
);
}
在**onWillPop**方法中,添加获取以前状态的事件,并返回false,直到返回第一个状态
Future<bool> _onWillPop() async {
if(BlocProvider.of<YourBloc>(context).state is! FirstState) {
/// Add the event in YourBloc to fetch the previous states.
....
....
return false;
} else {
return true;
}
}
Future\u onWillPop()异步{
if(BlocProvider.of(context).state为!FirstState){
///在YourBloc中添加事件以获取以前的状态。
....
....
返回false;
}否则{
返回true;
}
}