Flutter: WebView Example

Last updated on July 9, 2022 A Goodman Loading... Post a comment

There are several packages that can help you implement a web view in your Flutter application. In this article, we’ll use webview_flutter, the most popular plugin for this kind of stuff.

Install The Plugin

Run the following command to add the flutter_webview plugin to your project:

flutter pub add webview_flutter

Implementation

Import webview_flutter into your Dart code:

import 'package:webview_flutter/webview_flutter.dart';

Then put a WebView widget provided by webview_flutter in your code:

SizedBox(
          width: double.infinity,
          // the most important part of this example
          child: WebView(
            initialUrl: 'https://www.kindacode.com/',
            // Enable Javascript on WebView
            javascriptMode: JavascriptMode.unrestricted,
)),

The Complete Example

import 'package:flutter/material.dart';

// don't forget this line
import 'package:webview_flutter/webview_flutter.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.amber),
      title: "Kindacode.comr",
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Webview Example'),
      ),
      body: const SizedBox(
          width: double.infinity,
          // the most important part of this example
          child: WebView(
            initialUrl: 'https://www.kindacode.com/',
            // Enable Javascript on WebView
            javascriptMode: JavascriptMode.unrestricted,
          )),
    );
  }
}

And here’s the result:

Conclusion

You’ve gone through a complete example of implementing a web view in an app. If you’d like to learn more new and interesting stuff about Flutter, take a look at the following articles:

You can also check out our Flutter category page, or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles