【问题标题】:Flutter change position of AlertDialog in widget Tree在小部件树中颤动更改 AlertDialog 的位置
【发布时间】: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 的位置,或者是否有人知道解决此问题的方法。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我通过将showDialog 的参数useRootNavigator 设置为false 解决了我的问题,这导致AlertDialog 位于小部件树的更下方,并且在我的情况下,在访问所有必要依赖项的范围内。

    showDialog(
      context: context,
      useRootNavigator: false,
      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)),
            ),
          ],
        ),
      ),
    );
    

    我也可以使用 BlocProvider 将 Column 包装在内容中,但是根据上面的回答,我不需要在小部件树中提供两次 bloc。

    【讨论】:

      猜你喜欢
      • 2022-06-18
      • 2021-06-27
      • 2018-10-18
      • 2021-01-12
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多