gpt4 book ai didi

flutter - 在 flutter 中重新启动(关闭并再次打开)后对话框状态不会改变

转载 作者:行者123 更新时间:2023-12-05 06:55:23 24 4
gpt4 key购买 nike

我在一个 StatelessWidget 中定义了一个按钮(这将具有 bloc 创建逻辑并使用 bloc 提供程序注入(inject),),单击该按钮时我会显示一个对话框并将 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 的最新数据。该对话框仅显示该 Bloc 中以前的任何数据。有人可以指出我在哪里犯了错误吗?

最佳答案

你的对话框没有显示新数据的原因是你正在创建一个新的 bloc 实例。即使你说你不是。

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. 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.value。您可以阅读更多相关信息 here

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

这将使用先前的实例并将其提供给子小部件,而不是创建新实例,并且在使用完后不会关闭 bloc。这对于对话框和导航很方便。

关于flutter - 在 flutter 中重新启动(关闭并再次打开)后对话框状态不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65369790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com