Uso correto de app_colors.dart
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'dart:async';
|
||||
import 'constants/app_colors.dart';
|
||||
import 'constants/app_strings.dart';
|
||||
|
||||
class BluetoothScreen extends StatefulWidget {
|
||||
const BluetoothScreen({super.key});
|
||||
@@ -21,18 +22,16 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Ouvir os resultados da busca e ordenar
|
||||
_scanResultsSubscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
if (mounted) {
|
||||
// Criar uma cópia da lista e ordenar
|
||||
List<ScanResult> sortedResults = List.from(results);
|
||||
sortedResults.sort((a, b) {
|
||||
bool aHasName = _hasRealName(a);
|
||||
bool bHasName = _hasRealName(b);
|
||||
|
||||
if (aHasName && !bHasName) return -1; // 'a' vem primeiro
|
||||
if (!aHasName && bHasName) return 1; // 'b' vem primeiro
|
||||
return 0; // Mantém a ordem original entre iguais
|
||||
if (aHasName && !bHasName) return -1;
|
||||
if (!aHasName && bHasName) return 1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
setState(() {
|
||||
@@ -41,7 +40,6 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
}
|
||||
});
|
||||
|
||||
// Ouvir o estado da busca
|
||||
_isScanningSubscription = FlutterBluePlus.isScanning.listen((state) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
@@ -51,7 +49,6 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
});
|
||||
}
|
||||
|
||||
// Função auxiliar para verificar se tem nome real
|
||||
bool _hasRealName(ScanResult r) {
|
||||
return r.advertisementData.advName.isNotEmpty || r.device.platformName.isNotEmpty;
|
||||
}
|
||||
@@ -83,15 +80,15 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
|
||||
Future.delayed(const Duration(seconds: 8), () {
|
||||
if (mounted && _scanResults.isEmpty && _isScanning) {
|
||||
_showSnackBar("Nenhum dispositivo encontrado.", AppColors.coral);
|
||||
_showSnackBar(AppStrings.noDevicesFound, AppColors.coral);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
_showSnackBar("Erro ao iniciar busca: $e", Colors.red);
|
||||
_showSnackBar("${AppStrings.scanError}$e", AppColors.error);
|
||||
}
|
||||
} else {
|
||||
_showSnackBar("Permissões negadas.", AppColors.coral);
|
||||
_showSnackBar(AppStrings.permissionsDenied, AppColors.coral);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,22 +96,21 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
try {
|
||||
await FlutterBluePlus.stopScan();
|
||||
} catch (e) {
|
||||
_showSnackBar("Erro ao parar busca: $e", Colors.red);
|
||||
_showSnackBar("${AppStrings.stopScanError}$e", AppColors.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> connectToDevice(BluetoothDevice device) async {
|
||||
try {
|
||||
String name = device.platformName.isNotEmpty ? device.platformName : "Dispositivo";
|
||||
_showSnackBar("Conectando a $name...", Colors.white70);
|
||||
String name = device.platformName.isNotEmpty ? device.platformName : AppStrings.defaultDeviceName;
|
||||
_showSnackBar("${AppStrings.connectingTo}$name...", AppColors.white.withValues(alpha: 0.7));
|
||||
await device.connect();
|
||||
_showSnackBar("Conectado com sucesso!", Colors.green);
|
||||
_showSnackBar(AppStrings.connectedSuccess, AppColors.success);
|
||||
} catch (e) {
|
||||
_showSnackBar("Falha ao conectar: $e", Colors.red);
|
||||
_showSnackBar("${AppStrings.connectFail}$e", AppColors.error);
|
||||
}
|
||||
}
|
||||
|
||||
// Lógica para capturar o nome
|
||||
String _getDeviceName(ScanResult r) {
|
||||
if (r.advertisementData.advName.isNotEmpty) {
|
||||
return r.advertisementData.advName;
|
||||
@@ -122,7 +118,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
return r.device.platformName;
|
||||
} else {
|
||||
String id = r.device.remoteId.toString();
|
||||
return "Disp. [${id.length > 5 ? id.substring(id.length - 5) : id}]";
|
||||
return "${AppStrings.deviceIdPrefix}${id.length > 5 ? id.substring(id.length - 5) : id}]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,9 +138,9 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Bluetooth', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
title: const Text(AppStrings.bluetoothTitle, style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
backgroundColor: AppColors.transparent,
|
||||
elevation: 0,
|
||||
foregroundColor: AppColors.white,
|
||||
),
|
||||
@@ -156,7 +152,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
AppColors.coral.withOpacity(0.1),
|
||||
AppColors.coral.withValues(alpha: 0.1),
|
||||
AppColors.background,
|
||||
],
|
||||
),
|
||||
@@ -173,8 +169,8 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: _isScanning
|
||||
? AppColors.coral.withOpacity(0.1)
|
||||
: AppColors.backgroundGrey.withOpacity(0.3),
|
||||
? AppColors.coral.withValues(alpha: 0.1)
|
||||
: AppColors.backgroundGrey.withValues(alpha: 0.3),
|
||||
border: Border.all(
|
||||
color: _isScanning ? AppColors.coral : AppColors.backgroundGrey,
|
||||
width: 2,
|
||||
@@ -183,7 +179,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
child: Icon(
|
||||
_isScanning ? Icons.bluetooth_searching : Icons.bluetooth,
|
||||
size: 60,
|
||||
color: _isScanning ? AppColors.coral : AppColors.white.withOpacity(0.7),
|
||||
color: _isScanning ? AppColors.coral : AppColors.white.withValues(alpha: 0.7),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
@@ -217,7 +213,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
),
|
||||
),
|
||||
Text(
|
||||
_isScanning ? 'PARAR BUSCA' : 'BUSCAR AGORA',
|
||||
_isScanning ? AppStrings.stopSearch : AppStrings.startSearch,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
@@ -239,7 +235,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
_isScanning ? "Procurando..." : "Dispositivos próximos",
|
||||
_isScanning ? AppStrings.searching : AppStrings.nearbyDevices,
|
||||
style: const TextStyle(
|
||||
color: AppColors.white,
|
||||
fontSize: 14,
|
||||
@@ -251,11 +247,11 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.coral.withOpacity(0.2),
|
||||
color: AppColors.coral.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: const Text(
|
||||
"ATIVO",
|
||||
AppStrings.active,
|
||||
style: TextStyle(color: AppColors.coral, fontSize: 10, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
@@ -270,11 +266,11 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.bluetooth_disabled, size: 40, color: AppColors.white.withOpacity(0.1)),
|
||||
Icon(Icons.bluetooth_disabled, size: 40, color: AppColors.white.withValues(alpha: 0.1)),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
"Inicie a busca para conectar",
|
||||
style: TextStyle(color: AppColors.white.withOpacity(0.3)),
|
||||
AppStrings.startSearchInstruction,
|
||||
style: TextStyle(color: AppColors.white.withValues(alpha: 0.3)),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -285,35 +281,35 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
itemBuilder: (context, index) {
|
||||
final r = _scanResults[index];
|
||||
final name = _getDeviceName(r);
|
||||
final isUnknown = name.startsWith("Disp. [");
|
||||
final isUnknown = name.startsWith(AppStrings.deviceIdPrefix);
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.backgroundGrey.withOpacity(0.2),
|
||||
color: AppColors.backgroundGrey.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: AppColors.backgroundGrey.withOpacity(0.3),
|
||||
backgroundColor: AppColors.backgroundGrey.withValues(alpha: 0.3),
|
||||
child: Icon(
|
||||
isUnknown ? Icons.devices_other : Icons.bluetooth,
|
||||
color: isUnknown ? AppColors.white.withOpacity(0.4) : AppColors.white,
|
||||
color: isUnknown ? AppColors.white.withValues(alpha: 0.4) : AppColors.white,
|
||||
size: 20
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
name,
|
||||
style: TextStyle(
|
||||
color: isUnknown ? AppColors.white.withOpacity(0.5) : AppColors.white,
|
||||
color: isUnknown ? AppColors.white.withValues(alpha: 0.5) : AppColors.white,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 15,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
r.device.remoteId.toString(),
|
||||
style: TextStyle(color: AppColors.white.withOpacity(0.3), fontSize: 11),
|
||||
style: TextStyle(color: AppColors.white.withValues(alpha: 0.3), fontSize: 11),
|
||||
),
|
||||
trailing: ElevatedButton(
|
||||
onPressed: () => connectToDevice(r.device),
|
||||
@@ -324,7 +320,7 @@ class _BluetoothScreenState extends State<BluetoothScreen> {
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
elevation: 0,
|
||||
),
|
||||
child: const Text("CONECTAR", style: TextStyle(fontSize: 11, fontWeight: FontWeight.bold)),
|
||||
child: const Text(AppStrings.connect, style: TextStyle(fontSize: 11, fontWeight: FontWeight.bold)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user