Integrating a WebView in Flutter allows you to display web pages directly inside your app without opening an external browser. This is useful for loading websites, payment gateways, blogs, or dashboards.
In this guide, youβll learn how to implement WebView in Flutter with a practical example.
π What is WebView in Flutter?
A WebView is a widget that lets you display web content (HTML, CSS, JavaScript) inside a mobile app.
Flutter uses the official plugin:
- webview_flutter
π¦ Step 1: Add Dependency
Open your pubspec.yaml file and add:
dependencies:
flutter:
sdk: flutter
webview_flutter: ^4.0.7
Then run:
flutter pub get
βοΈ Step 2: Platform Setup
β Android Setup
Go to:
android/app/src/main/AndroidManifest.xml
Add internet permission:
<uses-permission android:name="android.permission.INTERNET"/>
β iOS Setup
Open:
ios/Runner/Info.plist
Add:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
π» Step 3: Basic WebView Example
Create a new Dart file:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';class WebViewExample extends StatefulWidget {
const WebViewExample({super.key}); @override
State<WebViewExample> createState() => _WebViewExampleState();
}class _WebViewExampleState extends State<WebViewExample> {
late final WebViewController controller; @override
void initState() {
super.initState(); controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse('https://flutter.dev'));
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Flutter WebView"),
),
body: WebViewWidget(controller: controller),
);
}
}
π Step 4: Enable JavaScript
To enable JavaScript:
controller.setJavaScriptMode(JavaScriptMode.unrestricted);
This is required for modern websites.
π Step 5: Handle Navigation
You can control navigation like this:
controller.setNavigationDelegate(
NavigationDelegate(
onPageStarted: (url) {
print("Page started loading: $url");
},
onPageFinished: (url) {
print("Page finished loading: $url");
},
onNavigationRequest: (request) {
if (request.url.contains("youtube.com")) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
);
π Step 6: Show Loading Indicator
Add a loading spinner:
bool isLoading = true;controller.setNavigationDelegate(
NavigationDelegate(
onPageFinished: (url) {
setState(() {
isLoading = false;
});
},
),
);
UI:
Stack(
children: [
WebViewWidget(controller: controller),
if (isLoading)
const Center(child: CircularProgressIndicator()),
],
)
π Step 7: Handle Back Button
Allow back navigation:
Future<bool> _onWillPop() async {
if (await controller.canGoBack()) {
controller.goBack();
return false;
}
return true;
}
Wrap with:
WillPopScope(
onWillPop: _onWillPop,
child: WebViewWidget(controller: controller),
)
π Step 8: Load Local HTML
You can load HTML directly:
controller.loadHtmlString("""
<html>
<body>
<h1>Hello Flutter</h1>
</body>
</html>
""");
π Step 9: Security Best Practices
- Avoid enabling JavaScript unnecessarily
- Restrict unknown URLs
- Use HTTPS websites only
- Validate user inputs in web forms
β‘ Common Use Cases
- In-app browser
- Payment gateway integration
- Display blog or CMS content
- Admin dashboards
- Help & support pages
β Common Issues & Fixes
1. WebView Not Loading
- Check internet permission
- Verify URL (https required)
2. Blank Screen
- Enable JavaScript
- Check SSL issues
3. Slow Performance
- Optimize website
- Avoid heavy scripts
π― Conclusion
Using WebView in Flutter is simple with the webview_flutter plugin. It allows you to seamlessly integrate web content into your app while maintaining full control over navigation and behavior.
0 Comments