Skip to content

Commit

Permalink
feat: ✨ list if device is connected
Browse files Browse the repository at this point in the history
to a wifi, cellular or ethernet network.
  • Loading branch information
lukki15 committed Dec 21, 2024
1 parent c9ec4dc commit 8b72ff7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/main_scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:forui/forui.dart';

import 'package:network_info_app/network_info.dart';

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

Expand All @@ -17,7 +19,7 @@ class _MainScaffoldState extends State<MainScaffold> {
final contents = [
const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text('Network Info Placeholder')],
children: [NetworkInfo()],
),
const Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
79 changes: 79 additions & 0 deletions lib/network_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:forui/forui.dart';
import 'package:connectivity_plus/connectivity_plus.dart';

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

@override
Widget build(BuildContext context) {
return _ConnectivityBuilder();
}
}

class _Connectivity extends StatelessWidget {
final List<ConnectivityResult> conductivities;
const _Connectivity({required this.conductivities});

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

if (Platform.isAndroid) {
tiles.add(FTile(
prefixIcon: FIcon(conductivities.contains(ConnectivityResult.mobile)
? FAssets.icons.signal
: FAssets.icons.signalLow),
title: const Text('Cellular'),
));
}

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

return Column(
spacing: 20,
children: tiles,
);
}
}

class _ConnectivityBuilder extends StatefulWidget {
const _ConnectivityBuilder();

@override
State<_ConnectivityBuilder> createState() => _ConnectivityBuilderState();
}

class _ConnectivityBuilderState extends State<_ConnectivityBuilder> {
final Future<List<ConnectivityResult>> connectivityResult =
Connectivity().checkConnectivity();

@override
Widget build(BuildContext context) {
return FutureBuilder<List<ConnectivityResult>>(
future: connectivityResult,
builder: (BuildContext context,
AsyncSnapshot<List<ConnectivityResult>> snapshot) {
if (snapshot.hasData) {
return _Connectivity(conductivities: snapshot.data!);
}
return _Connectivity(conductivities: []);
});
}
}

0 comments on commit 8b72ff7

Please sign in to comment.