Ganesh Ghodake
Ganesh Ghodake

Reputation: 359

Navigate to InApp screen from webview in flutter

I have a flutter app which has a web-view and browsing a specific URL. The web-view has form, when it is filled and submitted, the web-view redirects the user to the result URL according to his form fillings.

I am using : InAppWebView plugin for webviews.

I want to navigate to my DashboardScreen when my redirect url is hit. I am not able to navigate this screen. Please help.

Below is my build method :

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

Upvotes: 3

Views: 3312

Answers (1)

Hussnain Haidar
Hussnain Haidar

Reputation: 2258

It's a context issue. Also you don't have to remove a route for replacement. Use below working code will solve the issue.

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

Upvotes: 1

Related Questions