flutter_proxy_detector 1.0.0
flutter_proxy_detector: ^1.0.0 copied to clipboard
A Flutter plugin to detect system proxy settings (HTTP/HTTPS) and enforce them on native network stacks. Supports Android, iOS, macOS, Windows, and Linux.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_proxy_detector/flutter_proxy_detector.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _proxySetting = 'Unknown';
final _flutterProxyDetectorPlugin = FlutterProxyDetector();
@override
void initState() {
super.initState();
initPlatformState();
_getProxySetting();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _flutterProxyDetectorPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _getProxySetting() async {
String proxySetting;
try {
proxySetting =
await _flutterProxyDetectorPlugin.getProxySetting() ??
'No proxy detected';
} on PlatformException {
proxySetting = 'Failed to get proxy setting.';
}
if (!mounted) return;
setState(() {
_proxySetting = proxySetting;
});
}
Future<void> _applySystemProxy() async {
try {
await _flutterProxyDetectorPlugin.applySystemProxyIfAvailable();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Applied system proxy if available')),
);
await _getProxySetting(); // Refresh setting display
} on PlatformException catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to apply proxy: ${e.message}')),
);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Proxy Detection Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Running on: $_platformVersion\n'),
Text('Proxy Setting: $_proxySetting\n'),
ElevatedButton(
onPressed: _getProxySetting,
child: const Text('Refresh Proxy Setting'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _applySystemProxy,
child: const Text('Apply System Proxy'),
),
],
),
),
),
);
}
}