首页 > 解决方案 > showing black screen on backpress Flutter

问题描述

I am using Webview with WillPopScope, webview internal back button is working fine but on the first page back press is not working.

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SafeArea(child: MyHomePage()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  InAppWebViewController _controller;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => canGoBack(context),
      child: Scaffold(
        body: InAppWebView(
          initialUrlRequest:
              URLRequest(url: Uri.parse("https://www.google.com/")),
          onWebViewCreated: onWebviewCreated,
        ),
      ),
    );
  }

  void onWebviewCreated(InAppWebViewController controller) {
    _controller = controller;
  }

  canGoBack(BuildContext context) async {
    if (await _controller.canGoBack()) {
      _controller.goBack();
      return Future.value(false);
    } else {
      return Future.value(true);
    }
  }
}

tried with

canGoBack(BuildContext context) async {
        if (await _controller.canGoBack()) {
          _controller.goBack();
          return Future.value(false);
        } else {
          Navigator.pop(context, true);
          return Future.value(false);
        }
      }

the issue is showing the black if there is no webview history.

标签: androidflutter

解决方案


您没有任何以前的根并且您正在使用

Navigator.pop(context, true); // in canGoBack function

这就是问题的原因。删除该行,它将起作用。


推荐阅读