Skip to content

Commit

Permalink
feat: ✨ list the wifi network properties
Browse files Browse the repository at this point in the history
if the wifi is available
  • Loading branch information
lukki15 committed Dec 21, 2024
1 parent b389c33 commit a519eb7
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 17 deletions.
10 changes: 2 additions & 8 deletions lib/main_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ class _MainScaffoldState extends State<MainScaffold> {
const FHeader(title: Text('Network Scan')),
];
final contents = [
const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [NetworkInfo()],
),
const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text('Network Scan Placeholder')],
),
const NetworkInfo(),
const Center(child: Text('Network Scan Placeholder')),
];
final footers = [
FBottomNavigationBarItem(
Expand Down
39 changes: 30 additions & 9 deletions lib/network_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter/material.dart';
import 'package:forui/forui.dart';
import 'package:connectivity_plus/connectivity_plus.dart';

import 'package:network_info_app/network_stats.dart';

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

Expand All @@ -20,34 +22,53 @@ class _Connectivity extends StatelessWidget {

@override
Widget build(BuildContext context) {
List<FTile> tiles = [
List<Widget> tiles = [
FTile(
prefixIcon: FIcon(conductivities.contains(ConnectivityResult.wifi)
? FAssets.icons.wifi
: FAssets.icons.wifiOff),
prefixIcon: FIcon(
conductivities.contains(ConnectivityResult.wifi)
? FAssets.icons.wifi
: FAssets.icons.wifiOff,
color: conductivities.contains(ConnectivityResult.wifi)
? Colors.green
: Colors.red,
),
title: const Text('Wi-Fi'),
)
];

if (conductivities.contains(ConnectivityResult.wifi)) {
tiles.add(NetworkStats());
}

if (Platform.isAndroid &&
conductivities.contains(ConnectivityResult.mobile)) {
tiles.add(SizedBox(height: 10));
tiles.add(FTile(
prefixIcon: FIcon(FAssets.icons.signal),
prefixIcon: FIcon(
FAssets.icons.signal,
color: Colors.green,
),
title: const Text('Cellular'),
));
}

if (Platform.isLinux) {
tiles.add(SizedBox(height: 10));
tiles.add(FTile(
prefixIcon: FIcon(conductivities.contains(ConnectivityResult.ethernet)
? FAssets.icons.ethernetPort
: FAssets.icons.unplug),
prefixIcon: FIcon(
conductivities.contains(ConnectivityResult.ethernet)
? FAssets.icons.ethernetPort
: FAssets.icons.unplug,
color: conductivities.contains(ConnectivityResult.ethernet)
? Colors.green
: Colors.red,
),
title: const Text('Ethernet'),
));
}

return Column(
spacing: 20,
spacing: 10,
children: tiles,
);
}
Expand Down
131 changes: 131 additions & 0 deletions lib/network_stats.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:forui/forui.dart';

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

@override
State<NetworkStats> createState() => _NetworkStatsState();
}

class _NetworkStatsState extends State<NetworkStats> {
List<List<String>> _connectionStatus = [];
final NetworkInfo _networkInfo = NetworkInfo();

@override
void initState() {
super.initState();
_initNetworkInfo();
}

@override
Widget build(BuildContext context) {
return FTileGroup(
children: List.generate(
_connectionStatus.length,
(index) => FTile(
title: Text(_connectionStatus[index][0]),
details: Text(_connectionStatus[index][1]),
)),
);
}

Future<void> _initNetworkInfo() async {
String? wifiName,
wifiBSSID,
wifiIPv4,
wifiIPv6,
wifiGatewayIP,
wifiBroadcast,
wifiSubmask;

try {
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
// Request permissions as recommended by the plugin documentation:
// https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus
if (await Permission.locationWhenInUse.request().isGranted) {
wifiName = await _networkInfo.getWifiName();
} else {
wifiName = 'Unauthorized to get Wifi Name';
}
} else {
wifiName = await _networkInfo.getWifiName();
}
} on PlatformException catch (e) {
developer.log('Failed to get Wifi Name', error: e);
wifiName = 'Failed to get Wifi Name';
}

try {
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
// Request permissions as recommended by the plugin documentation:
// https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus
if (await Permission.locationWhenInUse.request().isGranted) {
wifiBSSID = await _networkInfo.getWifiBSSID();
} else {
wifiBSSID = 'Unauthorized to get Wifi BSSID';
}
} else {
wifiName = await _networkInfo.getWifiName();
}
} on PlatformException catch (e) {
developer.log('Failed to get Wifi BSSID', error: e);
wifiBSSID = 'Failed to get Wifi BSSID';
}

try {
wifiIPv4 = await _networkInfo.getWifiIP();
} on PlatformException catch (e) {
developer.log('Failed to get Wifi IPv4', error: e);
wifiIPv4 = 'Failed to get Wifi IPv4';
}

try {
wifiIPv6 = await _networkInfo.getWifiIPv6();
} on PlatformException catch (e) {
developer.log('Failed to get Wifi IPv6', error: e);
wifiIPv6 = 'Failed to get Wifi IPv6';
}

try {
wifiSubmask = await _networkInfo.getWifiSubmask();
} on PlatformException catch (e) {
developer.log('Failed to get Wifi submask address', error: e);
wifiSubmask = 'Failed to get Wifi submask address';
}

try {
wifiBroadcast = await _networkInfo.getWifiBroadcast();
} on PlatformException catch (e) {
developer.log('Failed to get Wifi broadcast', error: e);
wifiBroadcast = 'Failed to get Wifi broadcast';
}

try {
wifiGatewayIP = await _networkInfo.getWifiGatewayIP();
} on PlatformException catch (e) {
developer.log('Failed to get Wifi gateway address', error: e);
wifiGatewayIP = 'Failed to get Wifi gateway address';
}

setState(() {
_connectionStatus = [
['Wifi Name', wifiName ?? "N/A"],
['Wifi BSSID', wifiBSSID ?? "N/A"],
['Wifi IPv4', wifiIPv4 ?? "N/A"],
['Wifi IPv6', wifiIPv6 ?? "N/A"],
['Wifi Broadcast', wifiBroadcast ?? "N/A"],
['Wifi Gateway', wifiGatewayIP ?? "N/A"],
['Wifi Submask', wifiSubmask ?? "N/A"],
];
});
}
}

0 comments on commit a519eb7

Please sign in to comment.