简体   繁体   English

对话框 state 在 flutter 中重新启动(再次关闭和打开)后不会改变

[英]Dialog state does not change after restarting (closing and opening again) in flutter

I have defined one button inside one StatelessWidget (This will have the bloc creation logic and injecting using bloc provider, ), on click of the button i am showing a dialog and passing the bloc instance to it, as shown in the code.我在一个 StatelessWidget 中定义了一个按钮(这将具有 bloc 创建逻辑并使用 bloc 提供程序进行注入),单击该按钮时,我将显示一个对话框并将 bloc 实例传递给它,如代码所示。


//EsignBloc is defined here in parent statelessWidget. Defined i.e. creating the bloc instance and passing through the BlocProvider. Removed the code for simplicity


//This listener will be called when Button defined inside statelessWidget will be clicked. this is responsible for showing the dialog.
void _onClickHere(
    BuildContext context,
  ) {
    final dialog = Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(AppConstants.borderRadius),
      ),
      elevation: 0.0,
      backgroundColor: Colors.transparent,
      child: _GetSignUsingOtpView(),
    );

    showDialog(
      context: context,
      builder: (_) => BlocProvider<EsignBloc>(
        create: (_) => BlocProvider.of<EsignBloc>(context), // passing already created bloc to dialog
        child: WillPopScope(
          onWillPop: () => Future.value(false),
          child: dialog,
        ),
      ),
      barrierDismissible: false,
    );
  }

Pasting some code of _GetSignUsingOtpView()粘贴 _GetSignUsingOtpView() 的一些代码

class _GetSignUsingOtpView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<EsignBloc, EsignState>(builder: (context, state) {
      return Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.circular(
            AppConstants.borderRadius,
          ),
        ),
        child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Align(
                  alignment: Alignment.centerRight,
                  child: InkWell(
                    onTap: () => _closeDialog(context),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 8.0, right: 8),
                      child: Icon(
                        Icons.cancel,
                        color: AppColor.primaryDark,
                        size: SizeConfig.safeBlockVertical * 2,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(24),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      PrimaryText(text: state.otp), // data does not change after closing and opeing dialog again 
                      PrimaryText(text: state.remainingTime), // data does not change after closing and opeing dialog again 
                    ],
                  ),
                ),
              ],
            ),
      );
    });
  }

  void _closeDialog(BuildContext context) {
    Navigator.pop(context);
  }
}

The problem that I am facing is whenever the dialog opens again after closing, it doesn't show the latest data from the bloc.我面临的问题是每当对话框在关闭后再次打开时,它都不会显示来自该集团的最新数据。 The dialog just shows whatever previous data is in the bloc.该对话框仅显示该集团中的任何先前数据。 Can someone point it out, where i am making the mistake?有人可以指出,我在哪里犯了错误?

The reason that your dialog is not showing new data is because you are creating a new instance of the bloc.您的对话框未显示新数据的原因是您正在创建一个新的 bloc 实例。 Even though you say that you're not.即使你说你不是。

BlocProvider<EsignBloc>(
  create: (_) => BlocProvider.of<EsignBloc>(context), // passing already created bloc to dialog
  child: ...

In some cases, BlocProvider can be used to provide an existing cubit to a new portion of the widget tree.在某些情况下,BlocProvider 可用于为小部件树的新部分提供现有的cubit。 This will be most commonly used when an existing cubit needs to be made available to a new route.当需要为新路线提供现有肘部时,这将最常用。 In this case, BlocProvider will not automatically close the cubit since it did not create it.在这种情况下,BlocProvider 将不会自动关闭 cubit,因为它没有创建它。

To not create a new instance, you need to use BlocProvider.value .要不创建新实例,您需要使用BlocProvider.value You can read more about it here你可以在这里阅读更多关于它的信息

BlocProvider.value(
  value: BlocProvider.of<EsignBloc>(context),
  child: ...,
);

Instead of creating a new instance, this will use the previous instance and provide it to the child widget and will NOT close the bloc when its done using it.这不会创建一个新实例,而是使用前一个实例并将其提供给子小部件,并且在使用完成后不会关闭该块。 This is handy for dialogs and navigation.这对于对话框和导航很方便。

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

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