从Flutter的webview导航到应用内屏幕

3
我有一个Flutter应用程序,其中包含一个Web视图,可以浏览特定的URL。该Web视图包含一个表单,当填写并提交表单后,Web视图根据用户的表单填写将其重定向到结果URL。
我正在使用InAppWebView插件用于Web视图。当我的重定向URL被访问时,我想导航到我的DashboardScreen屏幕。但是我无法导航到该屏幕,请帮忙解决。以下是我的构建方法:
Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("MyApp"),
        ),
        body: InAppWebView(
          initialUrl: "myURL",
          onWebViewCreated: (InAppWebViewController controller) {
            _webViewController = controller;
          },
          onLoadStop: (InAppWebViewController controller, String url) async {
            if (url.startsWith("redirectURL")) {
              // get your token from url
              Navigator.pop(context);
              Navigator.pushReplacement(context,
                  MaterialPageRoute(builder: (context) => DashboardScreen()));
            }
          },
        ),
      ),
    );
  }

你卡在哪里了? - basudev nayak
我无法从Webview导航到下一个屏幕。 - Ganesh Ghodake
1个回答

1
这是一个上下文问题。而且你不需要删除路由以进行替换。使用下面的有效代码可以解决这个问题。
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  home: Scaffold(
    key: scaffoldKey,
    appBar: AppBar(
      title: Text("MyApp"),
    ),
    body: InAppWebView(
      initialUrl: "https://www.google.com.pk/",
      onWebViewCreated: (InAppWebViewController controller) {},
      onLoadStop: (InAppWebViewController controller, String url) async {
        if (url.contains("youtube")) {
          // get your token from url
          // Navigator.pop(scaffoldKey.currentContext); // No need of this 
  line
          Navigator.pushReplacement(scaffoldKey.currentContext,
              MaterialPageRoute(builder: (context) => DD()));
        }
      },
    ),
  ),
 );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接