vpn_detector 1.2.0
vpn_detector: ^1.2.0 copied to clipboard
A Flutter package for reliable VPN detection on iOS and Android, featuring a clean, testable API and real-time status updates.
VPN Detector 🛡️ #
A Flutter package for reliable VPN detection on iOS and Android, featuring a clean, testable API and real-time status updates.
Features #
- ✅ VPN Status: Check if a VPN connection is active, returning a
VpnStatus
enum (active
ornotActive
). - 🔄 Real-time Updates: Listen to the
onVpnStatusChanged
stream to react whenever the VPN status changes. - 🧪 Testable by Design: Use
VpnDetector.withDependencies(...)
and@visibleForTesting
hooks to inject fakes in your TDD suite. - 📦 Zero Static Calls: No hidden singletons—inject both
Connectivity
and the platform interface for full control.
Installation #
Add to your pubspec.yaml
:
dependencies:
vpn_detector: ^<latest_version>
Then run:
flutter pub get
Usage #
Basic Check #
import 'package:vpn_detector/vpn_detector.dart';
void main() async {
final status = await VpnDetector().isVpnActive();
if (status == VpnStatus.active) {
print('VPN is active');
} else {
print('VPN is not active');
}
}
Stream Listening #
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:vpn_detector/vpn_detector.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final VpnDetector _detector;
late StreamSubscription<VpnStatus> _sub;
VpnStatus _status = VpnStatus.notActive;
@override
void initState() {
super.initState();
_detector = VpnDetector();
_sub = _detector.onVpnStatusChanged.listen((status) {
setState(() => _status = status);
});
}
@override
void dispose() {
_sub.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Text(_status == VpnStatus.active ? 'ACTIVE' : 'NOT ACTIVE');
}
}
Contributing #
Contributions are welcome! Please open an issue or submit a pull request.