enforce_proxy 1.0.0 copy "enforce_proxy: ^1.0.0" to clipboard
enforce_proxy: ^1.0.0 copied to clipboard

discontinued

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 'dart:async';

import 'package:enforce_proxy/enforce_proxy.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _platformVersion = 'Unknown';
  String _proxySetting = 'Unknown';
  final _enforceProxyPlugin = EnforceProxy();

  @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 _enforceProxyPlugin.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 _enforceProxyPlugin.getProxySetting() ?? 'No proxy detected';
    } on PlatformException {
      proxySetting = 'Failed to get proxy setting.';
    }

    if (!mounted) return;

    setState(() {
      _proxySetting = proxySetting;
    });
  }

  Future<void> _applySystemProxy() async {
    try {
      await _enforceProxyPlugin.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 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'),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
160
points
144
downloads

Publisher

verified publishersatoyan.net

Weekly Downloads

A Flutter plugin to detect system proxy settings (HTTP/HTTPS) and enforce them on native network stacks. Supports Android, iOS, macOS, Windows, and Linux.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on enforce_proxy

Packages that implement enforce_proxy