DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Guide for Voice Search Integration to Your Flutter Streaming App
  • Why Choose Flutter App Development When You Need a Mobile App
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • Weighing the Trade-Offs of Native vs. Cross-Platform Mobile App Development

Trending

  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Why We Still Struggle With Manual Test Execution in 2025
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  1. DZone
  2. Coding
  3. Frameworks
  4. Flutter Webview URL Listeners

Flutter Webview URL Listeners

By 
Omer Yilmaz user avatar
Omer Yilmaz
DZone Core CORE ·
Updated Nov. 22, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
39.5K Views

Join the DZone community and get the full member experience.

Join For Free

butterfly-on-window

If you are new to Flutter development and want to learn how to use WebView, handle various WebView operations, and open native application pages from WebView, here are some tips for you.

What Is WebView?

If developers want to show any web page or website or load a URL inside an application, a WebView can be displayed without the help of any browser, so, basically, it displays the content of web pages directly inside the application.

Let’s get started on how to integrateWebView inside a Flutter application. If Flutter is not configured in your Android Studio, please visit the link below to know how to configure it.

https://flutter.dev/docs/get-started/editor

You may also like: Practical Flutter: 6 Tips for Newcomers.

Create a Flutter Application

  • Open Android Studio.

  • Open  File → New → New Flutter Project → Flutter Application, and click on Next.

  • Enter the project name, and give the Flutter SDK path on your machine

Creating a new Flutter application

Creating a new Flutter application
  • Click on Next.

  • Enter your company domain and package name and click Finish.

Finishing creating Flutter application

Finishing creating Flutter application
  • Android Studio will generate a default application. Click on the run button, and if everything works fine, it should show below page on the device

Flutter Demo Home Page

Flutter Demo Home Page
  • Add the following dependency in your pubspec.yaml file and click on Packages get.

flutter_inappbrowser: ^1.2.1

flutter_inappbrowser: ^1.2.1

Flutter in browser
  • Go to Android→ app→ build.gradle. In the default Config, change minSdkVersion to 17.

  • Remove the auto-generated code and add the following code to create a simple page

( flutter-webview-best-practices/webviewapp/lib/main.dart )

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

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

class WebViewWebPage extends StatefulWidget {
 @override
 _WebViewWebPageState createState() => _WebViewWebPageState();
}

class _WebViewWebPageState extends State<WebViewWebPage> {
 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text("Webview App"),
     ),
   );
 }
}


iOS setup

If you're running on macOS and have Xcode installed on your system, go to iOS → Right-click → Flutter → Open iOS module in Xcode.

Creating a Flutter application

Creating a Flutter application

Once the project gets loaded, select Runner project. Then, go to the info tab and set the following key io.flutter.embedded_views_preview value to true, as shown below.

Setting key value to true

Setting key value to true

After running the application, it will show following the page. As you see, we cleaned up our generated default Flutter APP Codes, and the application now shows a blank Webview App screen.

WebView app

WebView app

Add the WebView Component

Now, let’s add a WebView to load the page. Add InAppWebView inside your view to load URLs, as shown below.

flutter-webview-best-practices/webviewapp/lib/main.dart

class _WebViewWebPageState extends State<WebViewWebPage> {
 var URL = "https://google.com.tr";

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text("Webview App"),
     ),
     body: Container(
       child: InAppWebView(
           initialUrl: URL,
           onWebViewCreated: (InAppWebViewController controller) {}),
     ),
   );
 }
}


Show Progress While Loading the WebView

Let’s add and progress bar while the user opens the page in WebView component.

flutter-webview-best-practices/webviewapp/lib/main.dart

 class _WebViewWebPageState extends State<WebViewWebPage> {

 // URL to load
 var URL = "https://google.com.tr";
 // Webview progress
 double progress = 0;

 @override
 Widget build(BuildContext context) {
   return Scaffold(
       appBar: AppBar(
         title: Text("Webview App"),
       ),
       body: Container(
           child: Column(
               children: <Widget>[
         (progress != 1.0)
             ? LinearProgressIndicator(
                 value: progress,
                 backgroundColor: Colors.grey[200],
                 valueColor: AlwaysStoppedAnimation<Color>(Colors.purple))
             : null,    // Should be removed while showing
         Expanded(
           child: Container(
             child: InAppWebView(
               initialUrl: URL,
               initialHeaders: {},
               initialOptions: {},
               onWebViewCreated: (InAppWebViewController controller) {
               },
               onLoadStart: (InAppWebViewController controller, String url) {
               },
               onProgressChanged:
                   (InAppWebViewController controller, int progress) {
                 setState(() {
                   this.progress = progress / 100;
                 });
               },
             ),
           ),
         )
       ].where((Object o) => o != null).toList())));  //Remove null widgets
 }
}


 The above code will display a progress bar as shown below

Progress bar

Progress bar

How to Open Native Flutter Page From the WebView

To open a native page, the application needs to listen to URL changes in the WebView, and when a particular URL starts to load, we can open the Flutter native page.

Let’s look at an example :


When a user opens the LinkedIn page, the application should redirect to the Flutter native page.

Whenever any URL starts loading, the WebView gets a callback in the onLoadStart method. So, to listen for URL changes, modify the onLoadStart method, as shown below:

flutter-webview-best-practices/webviewapp/lib/main.dart 

onLoadStart: (InAppWebViewController controller, String url) 
{
                  // Listen URL change
                  if(URL == LISTENINGURL) {     // Add your URL here
                  // TODO open native page
                  }
                }


Create one Flutter native page by going to lib → right-click → new → dart file →  SecondPage and pressing Ok. 

flutter-webview-best-practices/webviewapp/lib/SecondPage.dart

class SecondPage extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text("Second Page"),
     ),
     body: Container(
      child: Center(
      child: Text("Hey,there!"),
       ),
     ),
   );
 }
}

 

Open this native page with the onLoadStart method, as shown below:

if(URL == LISTENINGURL){
 Navigator.of(context, rootNavigator: true)
     .push(MaterialPageRoute(
     builder: (context) => new SecondPage()));
}


Run the application type LinkedIn on Google and click on search. It will show the following screen:

Google in WebView application

Google in WebView application

Click on the first link, and it will open the following Flutter native page.

Flutter native page.

Flutter native page


Further Reading

  • Flutter vs. React Native: Choosing the Best Hybrid Framework for Mobile Development.
  • Serverless Application with Google Flutter and Lambda.
  • The Journey of an App – From Native to Progressive.
Flutter (software) mobile app

Opinions expressed by DZone contributors are their own.

Related

  • Guide for Voice Search Integration to Your Flutter Streaming App
  • Why Choose Flutter App Development When You Need a Mobile App
  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • Weighing the Trade-Offs of Native vs. Cross-Platform Mobile App Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!