简体   繁体   English

Flutter web 调整大小触发 bloc 事件?

[英]Flutter web resizing triggering bloc event?

Whenever i resize my chrome browser it emits an event to add a new "variant" to my column, i have no idea what it could be triggering it.每当我调整我的 chrome 浏览器大小时,它都会发出一个事件来向我的专栏添加一个新的“变体”,我不知道它可能会触发它。 I know its triggering the on add variant event but i have no clue why its triggering when resizing, or zooming in or out.我知道它会触发 on add variant 事件,但我不知道为什么它会在调整大小时或放大或缩小时触发。

Builder建设者

                        BlocBuilder<VariantBloc, VariantState>(
                          builder: (context, state) {

                            if(state is AddedVariantState){
                              variants++;
                            }else if (state is DeletedVariantState){
                              variants--;
                              usecase.multipleUsecase.removeAt(state.index);
                            }

                            return Align( 
                              alignment: Alignment.centerLeft,
                              child: ListView.builder(
                              shrinkWrap: true,
                              itemCount: variants,
                              itemBuilder: (context, index) {
                                usecase.multipleUsecase.add(MultipleAddServiceUsecase(index: index));
                                return VariantTextfield(
                                  usecase: usecase.multipleUsecase[index],
                                  index: index,
                                );
                              },
                              ),
                            );
                               
                          },
                        ),

Button that triggers event触发事件的按钮

class MarketplaceAddProductVariantButton extends StatelessWidget {
  const MarketplaceAddProductVariantButton({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {    
    return Align(
      alignment: Alignment.centerLeft,
      child: NeumorphicButton(
        onPressed: () {
          context
                .read<VariantBloc>()
                .add(OnAddVariantEvent());
        },
        margin:
            EdgeInsets.only(left: ScreenUtils.percentWidth(context, .5)),
        style: marketplaceButtonsNeuStyle.copyWith(
            boxShape:
                NeumorphicBoxShape.roundRect(BorderRadius.circular(17))),
        child: Container(
            alignment: Alignment.center,
            height: ScreenUtils.percentHeight(context, 4.5),
            width: ScreenUtils.percentWidth(context, 12),
            child: AutoSizeText(
              'Agregar otro',
              textAlign: TextAlign.center,
              style: _textStyle(),
            )),
      ),
    );
  }

Bloc that just emits event.刚刚发出事件的集团。

VariantBloc() : super(VariantInitial()) {
    on<OnAddVariantEvent>((event, emit) {
      emit(AddedVariantState(());
    });

Before resizing.在调整大小之前。 在此处输入图像描述

After.后。 在此处输入图像描述

Actually, it's not triggering a new event.实际上,它并没有触发新事件。 When you resize the window the build function will be called and so the BlocBuilder will be rebuilt, and if the last emitted state is AddedVariantState then this condition if(state is AddedVariantState) will be true and the variants variable will increase.当您调整 window 的大小时, build function 将被调用,因此BlocBuilder将被重建,如果最后发出的 state 是AddedVariantState则此条件if(state is AddedVariantState)将为并且variants变量将增加。 The opposite behavior will happen if the last emitted state was DeletedVariantState .如果最后发出的 state 是DeletedVariantState ,则会发生相反的行为。 That's why you should put the variants variable in the VariantState class.这就是为什么您应该将variants变量放在VariantState class 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM