簡體   English   中英

對話框 state 在 flutter 中重新啟動(再次關閉和打開)后不會改變

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

我在一個 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,
    );
  }

粘貼 _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);
  }
}

我面臨的問題是每當對話框在關閉后再次打開時,它都不會顯示來自該集團的最新數據。 該對話框僅顯示該集團中的任何先前數據。 有人可以指出,我在哪里犯了錯誤?

您的對話框未顯示新數據的原因是您正在創建一個新的 bloc 實例。 即使你說你不是。

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

在某些情況下,BlocProvider 可用於為小部件樹的新部分提供現有的cubit。 當需要為新路線提供現有肘部時,這將最常用。 在這種情況下,BlocProvider 將不會自動關閉 cubit,因為它沒有創建它。

要不創建新實例,您需要使用BlocProvider.value 你可以在這里閱讀更多關於它的信息

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

這不會創建一個新實例,而是使用前一個實例並將其提供給子小部件,並且在使用完成后不會關閉該塊。 這對於對話框和導航很方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM