Flutter依次删除对话框

时间:2018-11-16 06:55:29

标签: flutter

我试图按特定顺序删除对话框,但出现以下错误:

layer = "mixed4d_3x3_bottleneck_pre_relu"  #@param ["mixed4d_3x3_bottleneck_pre_relu", "mixed3a", "mixed3b", "mixed4a", "mixed4c", "mixed5a"]
iter_n = 12 #@param {type:"slider", max: 50}
strength = 120 #@param {type:"slider", max: 1000}
zooming_steps = 500 #@param {type:"slider", max: 512}
zoom_factor = 1.1 #@param {type:"number"}

tf.get_default_graph().finalize

frame = img0
img_y, img_x, _ = img0.shape
for i in range(zooming_steps):
  if i > 20:
    layer = "mixed3a"
  if i > 40:
    layer = "mixed4a"

  if i > 70:
    layer = "mixed4d_3x3_bottleneck_pre_relu"

  if i > 100:
    layer = "mixed5a"

  frame = render_deepdream(tf.square(T(layer)), frame, False)
  clear_output()
  showarray(frame,i=i)
  print("iteration: " +str(i))
  newsize = np.int32(np.float32(frame.shape[:2])*zoom_factor)
  frame = resize(frame, newsize)
  frame = frame[(newsize[0]-img_y)//2:(newsize[0]-img_y)//2+img_y,
                (newsize[1]-img_x)//2:(newsize[1]-img_x)//2+img_x,:]

这里是顺序:

  1. 列表项
  2. 打开“确认”对话框1
  3. 关闭(onPressed)对话框1
  4. 打开加载对话框2。
  5. 关闭(Navigator.of(context).pop())
  6. 打开对话框3,显示成功消息。

对话框1和2的Cloese都带有Navigator.of(context).pop()。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

使用功能Navigator.of(context).pop()时,请不要使用上一个对话框中的上下文,请尝试使用页面的上下文。 关于Looking up a deactivated widget's ancestor is unsafe的原因是上一个对话框已关闭,但是您使用了该对话框的上下文。您可以看到源代码:

static NavigatorState of(
    BuildContext context, {
      bool rootNavigator = false,
      bool nullOk = false,
    }) {
    final NavigatorState navigator = rootNavigator
        ? context.rootAncestorStateOfType(const TypeMatcher<NavigatorState>())
        : context.ancestorStateOfType(const TypeMatcher<NavigatorState>());// here check the ancestor state,and throw the error
    assert(() {
      if (navigator == null && !nullOk) {
        throw FlutterError(
          'Navigator operation requested with a context that does not include a Navigator.\n'
          'The context used to push or pop routes from the Navigator must be that of a '
          'widget that is a descendant of a Navigator widget.'
        );
      }
      return true;
    }());
    return navigator;
  }