- 1 year ago
How do I open multiple webviews in flutter, each connecting to a different Proxy server?
Comment
1 / 1
To open multiple WebView
s in Flutter, you can use the WebView
widget provided by the webview_flutter
package. To connect each WebView
to a different proxy server, you can use the WebSettings
class and set the proxy settings for each instance.
Here's an example code snippet:
dartimport 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late List<WebViewController> _controllers;
final _urls = [
'https://example.com/',
'https://google.com/',
'https://flutter.dev/'
];
@override
void initState() {
super.initState();
_controllers = List.generate(_urls.length, (_) => WebViewController());
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
for (var i = 0; i < _urls.length; i++)
Expanded(
child: WebView(
key: UniqueKey(),
initialUrl: _urls[i],
onWebViewCreated: (controller) async {
await controller.setWebSettings(
WebSettings(
proxyEnabled: true,
proxyHostname: 'proxy${i + 1}.example.com',
proxyPort: 8080,
),
);
_controllers[i] = controller;
},
),
),
],
),
);
}
}
In this example, we create three WebView
s, each with a different initial URL, and set the proxy settings for each instance in the onWebViewCreated
callback function. We also store the WebViewController
instance for each WebView
in a list for later use.