如何将bloc对象作为参数传递给小部件:
我希望能够将一个块(TimerBloc)或另一个块(AnotherBloc)传递给此类,并使用该块将带有该块的BlocBuilder返回。
类似于: TimerField(bloc:TimerBloc) 或与此类似: TimerField(bloc:AnotherBloc)
class TimerField extends StatelessWidget {
final [Not sure of the class] bloc;
const TimerField({
Key key,
@required this.bloc,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocBuilder<[THE BLOC THAT WAS PASSED IN], [AND THE STATE]>(
.......
答案 0 :(得分:0)
abstract class BaseBloc extends Bloc<EventType, StateType> {}
class TimerBloc extends BaseBloc {
@override
StateType get initialState => // ...
@override
Stream<StateType> mapEventToState(EventType event) async* {
// ...
}
}
class AnotherBloc extends BaseBloc {
@override
StateType get initialState => // ...
@override
Stream<StateType> mapEventToState(EventType event) async* {
// ...
}
}
class TimerField extends StatelessWidget {
final BaseBloc bloc;
const TimerField({
Key key,
@required this.bloc,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocBuilder<BaseBloc, StateType>(
bloc: bloc,
builder: ....
);
}