Created
March 4, 2025 21:26
-
-
Save saroar/d1bb395a38915587c2fc69a27f464c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | |
import 'dart:convert' show base64Encode, utf8; | |
class WebViewScreen extends StatefulWidget { | |
final String targetUrl; | |
final String proxyHost; | |
final int proxyPort; | |
final String username; | |
final String password; | |
const WebViewScreen({ | |
required this.targetUrl, | |
required this.proxyHost, | |
required this.proxyPort, | |
required this.username, | |
required this.password, | |
Key? key, | |
}) : super(key: key); | |
@override | |
_WebViewScreenState createState() => _WebViewScreenState(); | |
} | |
class _WebViewScreenState extends State<WebViewScreen> { | |
@override | |
void initState() { | |
super.initState(); | |
print("Initializing WebView with URL: ${widget.targetUrl}"); | |
print("Proxy Host: ${widget.proxyHost}"); | |
print("Proxy Port: ${widget.proxyPort}"); | |
print("Credentials: ${widget.username}:${widget.password}"); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// Build the proxy authentication header. | |
final proxyAuthHeader = | |
'Basic ${base64Encode(utf8.encode('${widget.username}:${widget.password}'))}'; | |
return Scaffold( | |
appBar: AppBar(title: const Text('WebView')), | |
body: InAppWebView( | |
initialUrlRequest: URLRequest( | |
url: WebUri(widget.targetUrl), | |
headers: { | |
// This header is added to the HTTP request. | |
'Proxy-Authorization': proxyAuthHeader, | |
}, | |
), | |
initialOptions: InAppWebViewGroupOptions( | |
crossPlatform: InAppWebViewOptions( | |
javaScriptEnabled: true, | |
useShouldOverrideUrlLoading: true, | |
), | |
), | |
onReceivedHttpAuthRequest: (controller, challenge) async { | |
print("Auth requested for ${challenge.protectionSpace.host}"); | |
return HttpAuthResponse( | |
action: HttpAuthResponseAction.PROCEED, | |
username: widget.username, | |
password: widget.password, | |
permanentPersistence: true, | |
); | |
}, | |
onLoadStart: (controller, url) { | |
print("Loading: $url"); | |
}, | |
onLoadStop: (controller, url) { | |
print("Finished: $url"); | |
}, | |
onLoadError: (controller, url, code, message) { | |
print("Error: $message ($code) for $url"); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar(content: Text('Failed to load page: $message')), | |
); | |
controller.stopLoading(); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment