import 'package:flutter/material.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import '../l10n/l10n.dart'; import 'signal_ui.dart'; /// A reusable tile widget for displaying a MeshCore device in a list class DeviceTile extends StatelessWidget { final ScanResult scanResult; final VoidCallback onTap; /// True when this entry came from the OS systemDevices query (connected via /// GATT, not advertising) rather than a live scan. Such entries have no real /// rssi, so the tile shows a connected badge instead of a signal bar (#570). final bool systemConnected; const DeviceTile({ super.key, required this.scanResult, required this.onTap, this.systemConnected = false, }); @override Widget build(BuildContext context) { final device = scanResult.device; final rssi = scanResult.rssi; // Prefer the LIVE advertised name over the OS-cached platformName, which can // be a stale/default cache (e.g. "nimble", the NimBLE stack default) that // masks the device's real advertised name. (#189) final adv = scanResult.advertisementData.advName; final name = adv.isNotEmpty ? adv : device.platformName; return ListTile( leading: systemConnected ? _buildConnectedBadge(context) : _buildSignalIcon(rssi), title: Text( name.isNotEmpty ? name : context.l10n.common_unknownDevice, style: const TextStyle(fontWeight: FontWeight.w500), ), subtitle: Text(device.remoteId.toString()), trailing: ElevatedButton( onPressed: onTap, child: Text(context.l10n.common_connect), ), onTap: onTap, ); } // #570: OS-connected entries have no advertisement, so rssi is a sentinel. // Show a neutral connected badge, never a (green) signal bar for a fake 0. Widget _buildConnectedBadge(BuildContext context) { final color = Theme.of(context).colorScheme.primary; return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.bluetooth_connected, color: color), Text( context.l10n.scanner_connectedBadge, style: TextStyle(fontSize: 10, color: color), ), ], ); } Widget _buildSignalIcon(int rssi) { final tier = rssi >= -60 ? 0 : rssi >= -70 ? 1 : rssi >= -80 ? 2 : rssi >= -90 ? 3 : 4; final signalUi = signalUiForStrengthTier(tier); return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(signalUi.icon, color: signalUi.color), Text( '$rssi dBm', style: TextStyle(fontSize: 10, color: signalUi.color), ), ], ); } }