internet_permission 1.0.2
internet_permission: ^1.0.2 copied to clipboard
A Flutter plugin for managing internet permissions and checking network connectivity. Check if device is connected to internet, get connection type (WiFi, Mobile, Ethernet, VPN).
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:internet_permission/internet_permission.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _internetPermission = InternetPermission();
String _platformVersion = 'Unknown';
bool _isConnected = false;
String _connectionType = 'none';
bool _isLoading = true;
@override
void initState() {
super.initState();
_initPlugin();
}
Future<void> _initPlugin() async {
setState(() => _isLoading = true);
try {
final version = await _internetPermission.getPlatformVersion();
final connected = await _internetPermission.isConnected();
final type = await _internetPermission.getConnectionType();
if (mounted) {
setState(() {
_platformVersion = version ?? 'Unknown';
_isConnected = connected;
_connectionType = type;
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() {
_platformVersion = 'Error: $e';
_isLoading = false;
});
}
}
}
IconData _getConnectionIcon() {
switch (_connectionType) {
case 'wifi':
return Icons.wifi;
case 'mobile':
return Icons.signal_cellular_alt;
case 'ethernet':
return Icons.settings_ethernet;
case 'vpn':
return Icons.vpn_key;
default:
return Icons.wifi_off;
}
}
Color _getStatusColor() {
return _isConnected ? Colors.green : Colors.red;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Internet Permission Example'),
centerTitle: true,
elevation: 2,
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: RefreshIndicator(
onRefresh: _initPlugin,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Platform Info Card
Card(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Icon(
Icons.info_outline,
size: 48,
color: Colors.blue,
),
const SizedBox(height: 12),
Text(
'Platform',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
_platformVersion,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
),
),
const SizedBox(height: 20),
// Connection Status Card
Card(
elevation: 4,
color: _getStatusColor().withOpacity(0.1),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Icon(
_getConnectionIcon(),
size: 64,
color: _getStatusColor(),
),
const SizedBox(height: 16),
Text(
_isConnected
? 'Connected to Internet'
: 'No Internet Connection',
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(
color: _getStatusColor(),
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
decoration: BoxDecoration(
color: _getStatusColor(),
borderRadius: BorderRadius.circular(20),
),
child: Text(
_connectionType.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
),
const SizedBox(height: 32),
// Refresh Button
ElevatedButton.icon(
onPressed: _initPlugin,
icon: const Icon(Icons.refresh),
label: const Text('Check Connection'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
textStyle: const TextStyle(fontSize: 16),
),
),
const SizedBox(height: 32),
// Info Text
Text(
'Pull down to refresh',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.grey,
),
),
],
),
),
),
),
);
}
}