To use a WebView in your Flutter app, you need to import the webview_flutter
package. Here's how:
-
Add the package to your
pubspec.yaml
file:dependencies: flutter: sdk: flutter webview_flutter: ^3.0.0 # Replace with latest version
-
Run
flutter pub get
in your terminal to install the package. -
Import the package in your Dart file:
import 'package:webview_flutter/webview_flutter.dart';
Now you can use the WebView
widget to display web content in your Flutter app.
Example Usage:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewExample extends StatefulWidget {
@override
_WebViewExampleState createState() => _WebViewExampleState();
}
class _WebViewExampleState extends State<WebViewExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('WebView Example'),
),
body: WebView(
initialUrl: 'https://www.google.com',
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}
This code will display the Google homepage in a WebView.
Key Points:
initialUrl
: Sets the initial URL to load in the WebView.javascriptMode
: Controls whether JavaScript is enabled in the WebView.WebView
widget: The main widget responsible for displaying the web content.
Remember to enable the INTERNET
permission in your AndroidManifest.xml
file if you're using Android.