在我的项目中,我正在使用flutter_bloc库。
我有很多屏幕,对于每个屏幕,我正在使用ScreenBloc<ScreenEvent, ScreenState>
,效果很好。
但是,我发现自己在这些集团之间复制了状态类,例如Init Loading,Error,Loaded ShowBottomSheet等。
是否存在一种优雅的方式或模式来拥有一些基本国家,以便所有集团都能共享或扩展?
请考虑以下示例:
//For screen A I define the following:
class ScreenAEvent extends Equatable{}
//Screen A events..
//Screen A states..
class ScreenAState extends Equatable{}
class Init extends ScreenAState{}
class Loading extends ScreenAState{}
class Error extends ScreenAState{
final Exception e;
Error(this.e)
}
class ShowBottomSheet extends ScreenAState{} //Using BlocListener to show bottom sheet
class BlocScreenA extends Bloc<ScreenAEvent, ScreenAState>{
//Code for screen A bloc
}
//For screen B I define the following:
class ScreenBEvent extends Equatable{}
//Screen B events..
//Screen B states
class ScreenBState extends Equatable{}
class Init extends ScreenBState{} //Defining again Init
class Loading extends ScreenBState{} //Defining again Loading
class Error extends ScreenBState{ //Same for error
final Exception e;
Error(this.e)
}
class ShowBottomSheet extends ScreenBState{}
class BlocScreenB extends Bloc<ScreenBEvent, ScreenBState>{
//Code for screen B bloc
}
For each screen in the app, I have at least the Init, Loading, Error and Loaded states..
How can these implemented with the flutter_bloc library?