【发布时间】:2021-08-13 04:13:47
【问题描述】:
我正在我的应用程序的小部件树深处创建一个AlertDialog,并使用 Bloc 包通过单击 AlertDialog 中的按钮来发出事件。
问题是 AlertDialog 位于小部件树中 MaterialApp 的正下方,因此在上下文中找不到我的 BlocProvider,因为我在小部件树的下方定义了它。我也无法将 BlocProvider 移动到全局可用,因为这个提供的 bloc 依赖于只能在小部件树的下方访问的状态。
这是我用一个 TextField 调用 AlertDialog 的函数,它发出一个事件onChanged:
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Create Patient Questionnaire"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text("Give your patient Questionnaire a name!"),
UniversalTextField(
prefixText: "Name",
onChanged: (value) => context
.read<CreatePQBloc>()
.add(CreatePQDocumentNameChanged(documentName: value)),
),
],
),
),
);
我很想知道是否可以更改小部件树中 AlertDialog 的位置,或者是否有人知道解决此问题的方法。
【问题讨论】: