vpn_connection_detector 1.0.3 vpn_connection_detector: ^1.0.3 copied to clipboard
VPN connection detecting in Dart. Singleton class with state stream & connection check.
import 'package:flutter/material.dart';
import 'package:vpn_connection_detector/vpn_connection_detector.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final stream = VpnConnectionDetector();
@override
void dispose() {
stream.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Vpn Connection Monitor"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'VPN is active(Stream):',
),
StreamBuilder<VpnConnectionState>(
stream: stream.vpnConnectionStream,
builder: (context, AsyncSnapshot<VpnConnectionState> snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapshot.data.toString(),
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
fontSize: 20,
),
),
);
} else {
return const CircularProgressIndicator();
}
}),
const Divider(),
const Text(
'VPN is active(Static):',
),
FutureBuilder<bool>(
future: VpnConnectionDetector.isVpnActive(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapshot.data.toString(),
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
fontSize: 20,
),
),
);
} else {
return const CircularProgressIndicator();
}
}),
],
),
),
);
}
}