【问题标题】:Navigator.pop not working and show black background?Navigator.pop 不工作并显示黑色背景?
【发布时间】:2022-01-19 04:26:29
【问题描述】:

我尝试返回上一个屏幕。但是 UI 只显示黑色背景,并且只发生在这个屏幕上。我尝试了Navigator.pop(context); 和Navigator.of(context).pop();。

这是我的代码:

return MaterialApp(
    home: SafeArea(
  child: Scaffold(
    body: BlocBuilder<TimeKeepingCubit, TimeKeepingState>(
    builder: (context, state)
{
  var scale = 1.0;
  if (state.controller != null){
    var camera = state.controller!.value;
    // fetch screen size
    final size = MediaQuery.of(context).size;

    // calculate scale depending on screen and camera ratios
    // this is actually size.aspectRatio / (1 / camera.aspectRatio)
    // because camera preview size is received as landscape
    // but we're calculating for portrait orientation
    var scale = size.aspectRatio * camera.aspectRatio;

    // to prevent scaling down, invert the value
    if (scale < 1) scale = 1 / scale;
  }
  return Stack(
    children: [
      state.controller == null ? SizedBox() : Center(
        child: Transform.scale(
          scale: scale,
          child: Center(
            child: CameraPreview(state.controller!),
          ),
        ),
      ),
      Visibility(
        visible: state is TimeKeepingLoading,
        child: Center(
            child: Container(
                width: 68,
                height: 68,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: AppColors.whiteE0E0E0),
                child: CupertinoActivityIndicator())
        ),
      ),
      buildAlignScafoldScan(),
      Positioned(
        top: 0,
        left: 0,
        child: IconButton(
          icon: SvgPicture.asset(Res.ic_back_left_blue),
          onPressed: () {
            Navigator.pop(context);
            // Navigator.of(context).pop();
          },
        ),
      ),

【问题讨论】:

    标签: flutter navigation flutter-layout navigator


    【解决方案1】:

    当应用内无处可去或弹出时,UI 显示黑色背景。你想用pop去哪个页面?而不是 Navigator.pop() 使用 Navigator.push() 到你想去 onPress 的页面。

     onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondPage()),
            );
          }, 
    

    【讨论】:

      【解决方案2】:

      我可以看到它是您的MaterialApp,您正尝试拨打Navigator.pop(context)。由于这是堆栈上的第一个屏幕,pop 方法都将显示黑屏。只有当下面有其他屏幕时,您才能弹出。假设有2个屏幕。从 screen1 调用 screen2 并在 screen2 上调用 Navigator.pop(context) 以返回到 screen1 时,您应该使用 Navigator.push。如果 screen1 是您的第一个也是唯一一个屏幕,则弹出它会导致黑屏。

      【讨论】:

        【解决方案3】:

        我终于找到了我的问题。这就是这段代码中的上下文

        BlocBuilder<TimeKeepingCubit, TimeKeepingState>(
            builder: (context, state)
        

        make 在里面使用Navigator.pop(context); 会导致黑屏。我的解决方案是创建一个作用域使用Navigator.pop(context); 的函数不再在BlocBuilder 中。它将使用主屏幕的context。

        void backToPreScreen() {
          Navigator.pop(context);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-30
          • 2010-12-02
          • 1970-01-01
          相关资源
          最近更新 更多