9.4 C
New York
Saturday, December 2, 2023

Flutter: WebView Example

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:Advertisements

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:

Container(
     width: double.infinity,
     height: 600,
        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(MyApp());
}

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

class MyHomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('WebView Example'),
      ),
      body: Container(
          width: double.infinity,
          // height: 600,
          // the most important part of this example
          child: WebView(
            initialUrl: 'https://flutter.dev/',
            // Enable Javascript on WebView
            javascriptMode: JavascriptMode.unrestricted,
          )),
    );
  }
}

And here’s the result:

Conclusion

Advertisements

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.

Advertisements

Related Articles

Latest Articles