From a41a46a7d440b277c777bfe6053388b9e80a2769 Mon Sep 17 00:00:00 2001 From: Tomi Leppänen Date: Fri, 13 Sep 2024 17:13:58 +0300 Subject: Make wired network settings functional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Get all network devices and show local IP. This removes public IP from display but leaves the code to fetch it for possible future use. Note that this does not attempt to filter out wireless or virtual network devices. Change-Id: I52de896ed3827c86782ee1cf29208a295532c3f2 Signed-off-by: Tomi Leppänen --- .../settings_screens/wired/wired_screen.dart | 198 ++++++++++++--------- 1 file changed, 110 insertions(+), 88 deletions(-) diff --git a/lib/presentation/screens/settings/settings_screens/wired/wired_screen.dart b/lib/presentation/screens/settings/settings_screens/wired/wired_screen.dart index 35225b8..1d2e679 100644 --- a/lib/presentation/screens/settings/settings_screens/wired/wired_screen.dart +++ b/lib/presentation/screens/settings/settings_screens/wired/wired_screen.dart @@ -1,3 +1,4 @@ +import 'dart:io'; import 'package:flutter_ics_homescreen/core/utils/helpers.dart'; import 'package:flutter_ics_homescreen/export.dart'; import 'package:get_ip_address/get_ip_address.dart'; @@ -19,15 +20,44 @@ class WiredScreen extends StatefulWidget { State createState() => _WiredScreenState(); } +class Interface { + final String deviceName; + final String? deviceIP; + + const Interface({required this.deviceName, required this.deviceIP}); +} + class _WiredScreenState extends State { - String deviceIP = "192.168.234.120"; + String publicIP = "N/A"; + List interfaces = List.empty(); + @override void initState() { super.initState(); - getIPAddress(); + getDeviceInfo(); } - getIPAddress() async { + getDeviceInfo() async { + final list = await NetworkInterface.list(type: InternetAddressType.IPv4); + final newInterfaces = list.map((interface) { + String? deviceIP; + for (var address in interface.addresses) { + if (!address.isLinkLocal && !address.isLoopback && !address.isMulticast) { + deviceIP = address.address; + break; + } + } + return Interface( + deviceName: interface.name, + deviceIP: deviceIP, + ); + }).toList(growable: false); + setState(() { + interfaces = newInterfaces; + }); + } + + getPublicIPAddress() async { try { /// Initialize Ip Address var ipAddress = IpAddress(type: RequestType.text); @@ -35,7 +65,7 @@ class _WiredScreenState extends State { /// Get the IpAddress based on requestType. dynamic data = await ipAddress.getIpAddress(); setState(() { - deviceIP = data.toString(); + publicIP = data.toString(); }); } on IpAddressException catch (exception) { /// Handle the exception. @@ -54,95 +84,87 @@ class _WiredScreenState extends State { context.flow().update((state) => AppState.settings); }, ), - Container( - margin: const EdgeInsets.symmetric(horizontal: 120, vertical: 40), - padding: const EdgeInsets.symmetric(vertical: 17, horizontal: 24), - height: 140, - alignment: Alignment.center, - decoration: BoxDecoration( - gradient: LinearGradient( - begin: Alignment.centerLeft, - end: Alignment.centerRight, - stops: const [ - 0, - 0.01, - 0.8 - ], - colors: [ - Colors.white, - AGLDemoColors.neonBlueColor, - AGLDemoColors.neonBlueColor.withOpacity(0.15) - ]), - ), - child: Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - // const SizedBox( - // width: 20, - // ), - Expanded( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - const Text( - 'hernet_0090451v407b_cable', - style: TextStyle(color: Colors.white, fontSize: 40), - ), - Text( - 'connected, $deviceIP', - style: - const TextStyle(color: Colors.white, fontSize: 26), - ), - ], + Expanded(child: ListView.separated( + padding: const EdgeInsets.symmetric(horizontal: 120, vertical: 40), + separatorBuilder: (BuildContext context, int index) => const SizedBox(height: 24), + itemCount: interfaces.length, + itemBuilder: (BuildContext context, int index) { + if (index >= interfaces.length) + return null; + final String deviceName = interfaces[index].deviceName; + bool connected = interfaces[index].deviceIP != null; + final String deviceIP = interfaces[index].deviceIP ?? "N/A"; + return Container( + padding: const EdgeInsets.symmetric(vertical: 17, horizontal: 24), + height: 140, + alignment: Alignment.center, + decoration: BoxDecoration( + gradient: LinearGradient( + begin: Alignment.centerLeft, + end: Alignment.centerRight, + stops: const [ + 0, + 0.01, + 0.8 + ], + colors: [ + Colors.white, + AGLDemoColors.neonBlueColor, + AGLDemoColors.neonBlueColor.withOpacity(0.15) + ]), + ), + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + // const SizedBox( + // width: 20, + // ), + Expanded( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + deviceName, + style: const TextStyle(color: Colors.white, fontSize: 40), + ), + Text( + connected ? 'connected, $deviceIP' : 'disconnected', + style: const TextStyle(color: Colors.white, fontSize: 26), + ), + ], + ), ), - ), - Container( - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(4), - color: AGLDemoColors.buttonFillEnabledColor, - border: Border.all(color: AGLDemoColors.neonBlueColor), - boxShadow: [Helpers.boxDropShadowRegular]), - child: Material( - color: Colors.transparent, - child: InkWell( - onTap: () {}, - borderRadius: BorderRadius.circular(4), - child: const Padding( - padding: - EdgeInsets.symmetric(vertical: 30, horizontal: 40), - child: Text( - "Configure", - style: TextStyle( - color: AGLDemoColors.periwinkleColor, - fontSize: 26), + Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(4), + color: AGLDemoColors.buttonFillEnabledColor, + border: Border.all(color: AGLDemoColors.neonBlueColor), + boxShadow: [Helpers.boxDropShadowRegular]), + child: Material( + color: Colors.transparent, + child: InkWell( + onTap: () {}, + borderRadius: BorderRadius.circular(4), + child: const Padding( + padding: + EdgeInsets.symmetric(vertical: 30, horizontal: 40), + child: Text( + "Configure", + style: TextStyle( + color: AGLDemoColors.periwinkleColor, + fontSize: 26), + ), ), ), ), ), - ), - // ElevatedButton( - // style: ElevatedButton.styleFrom( - // backgroundColor: const Color(0xFF1C2D92), - // side: - // const BorderSide(color: Color(0xFF285DF4), width: 2), - // ), - // child: const Padding( - // padding: - // EdgeInsets.symmetric(vertical: 15.0, horizontal: 0), - // child: Text( - // 'Configure', - // style: TextStyle( - // color: Color(0xFFC1D8FF), - // fontSize: 26, - // ), - // ), - // ), - // onPressed: () {}, - // ), - ]), - ), + ] + ), + ); + }, + )), ], ); } -- cgit 1.2.3-korg