Navigator.pop不工作并显示黑色背景



我试着回到上一个屏幕。但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();
},
),
),

UI在应用程序中无处可去或弹出时显示黑色背景。你想使用pop转到哪个页面?不要使用Navigator.pop((,而是使用Navigator.push()到您要在Press上转到的页面。

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

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

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

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

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

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

最新更新