Home > other >  Navigator.pop not working and show black background?
Navigator.pop not working and show black background?

Time:01-20

I try back to previous screen. But UI only show black background and it only happens on this screen. I tried both Navigator.pop(context); and Navigator.of(context).pop();.

This my code:

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();
          },
        ),
      ),

CodePudding user response:

UI shows black background when there is nowhere to go or pop within app. What page you want to go using pop? Instead of Navigator.pop() use Navigator.push() to the page you want to go onPress.

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

CodePudding user response:

I can see it is your MaterialApp where you are tryng to call Navigator.pop(context). As this is the first screen on the stack both the pop methods will show a black screen. You can only pop when there are other screens below. Let's say there are 2 screens. You should use Navigator.push when calling screen2 from screen1 and call Navigator.pop(context) on the screen2 to get back to the screen1. If screen1 is your first and only screen then popping it would result into balck screen.

  •  Tags:  
  • Related