Advertisement
rajath_pai

BluetoothDeviceListEntry

Aug 4th, 2021
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.61 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
  3.  
  4. class BluetoothDeviceListEntry extends ListTile {
  5.   BluetoothDeviceListEntry({
  6.     required BluetoothDevice device,
  7.     int? rssi,
  8.     GestureTapCallback? onTap,
  9.     GestureLongPressCallback? onLongPress,
  10.     bool enabled = true,
  11.   }) : super(
  12.           onTap: onTap,
  13.           onLongPress: onLongPress,
  14.           enabled: enabled,
  15.           leading:
  16.               Icon(Icons.devices), // @TODO . !BluetoothClass! class aware icon
  17.           title: Text(device.name ?? ""),
  18.           subtitle: Text(device.address.toString()),
  19.           trailing: Row(
  20.             mainAxisSize: MainAxisSize.min,
  21.             children: <Widget>[
  22.               rssi != null
  23.                   ? Container(
  24.                       margin: new EdgeInsets.all(8.0),
  25.                       child: DefaultTextStyle(
  26.                         style: _computeTextStyle(rssi),
  27.                         child: Column(
  28.                           mainAxisSize: MainAxisSize.min,
  29.                           children: <Widget>[
  30.                             Text(rssi.toString()),
  31.                             Text('dBm'),
  32.                           ],
  33.                         ),
  34.                       ),
  35.                     )
  36.                   : Container(width: 0, height: 0),
  37.               device.isConnected
  38.                   ? Icon(Icons.import_export)
  39.                   : Container(width: 0, height: 0),
  40.               device.isBonded
  41.                   ? Icon(Icons.link)
  42.                   : Container(width: 0, height: 0),
  43.             ],
  44.           ),
  45.         );
  46.  
  47.   static TextStyle _computeTextStyle(int rssi) {
  48.     /**/ if (rssi >= -35)
  49.       return TextStyle(color: Colors.greenAccent[700]);
  50.     else if (rssi >= -45)
  51.       return TextStyle(
  52.           color: Color.lerp(
  53.               Colors.greenAccent[700], Colors.lightGreen, -(rssi + 35) / 10));
  54.     else if (rssi >= -55)
  55.       return TextStyle(
  56.           color: Color.lerp(
  57.               Colors.lightGreen, Colors.lime[600], -(rssi + 45) / 10));
  58.     else if (rssi >= -65)
  59.       return TextStyle(
  60.           color: Color.lerp(Colors.lime[600], Colors.amber, -(rssi + 55) / 10));
  61.     else if (rssi >= -75)
  62.       return TextStyle(
  63.           color: Color.lerp(
  64.               Colors.amber, Colors.deepOrangeAccent, -(rssi + 65) / 10));
  65.     else if (rssi >= -85)
  66.       return TextStyle(
  67.           color: Color.lerp(
  68.               Colors.deepOrangeAccent, Colors.redAccent, -(rssi + 75) / 10));
  69.     else
  70.       /*code symmetry*/
  71.       return TextStyle(color: Colors.redAccent);
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement