一次大的提交
This commit is contained in:
@@ -0,0 +1,766 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import '../../../../../core/bluetooth/ble_manager.dart';
|
||||
import '../pages/ble_device_detail_page.dart';
|
||||
|
||||
class BluetoothScanModal extends StatefulWidget {
|
||||
const BluetoothScanModal({super.key});
|
||||
|
||||
@override
|
||||
State<BluetoothScanModal> createState() => _BluetoothScanModalState();
|
||||
}
|
||||
|
||||
class _BluetoothScanModalState extends State<BluetoothScanModal> {
|
||||
BluetoothAdapterState _adapterState = BluetoothAdapterState.unknown;
|
||||
List<ScanResult> _scanResults = [];
|
||||
bool _isConnecting = false;
|
||||
BluetoothDevice? _connectingDevice;
|
||||
BluetoothDevice? _connectedDevice;
|
||||
int _connectingCountdown = 15;
|
||||
Timer? _connectingTimer;
|
||||
bool _isStartingScan = false;
|
||||
|
||||
StreamSubscription<BluetoothAdapterState>? _stateSubscription;
|
||||
StreamSubscription<List<ScanResult>>? _scanSubscription;
|
||||
StreamSubscription<BluetoothDevice?>? _connectionSubscription;
|
||||
StreamSubscription<BluetoothDevice?>? _connectingSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initBluetooth();
|
||||
}
|
||||
|
||||
Future<void> _initBluetooth() async {
|
||||
// 先设置扫描结果监听,确保 startScan 前订阅已就绪
|
||||
_scanSubscription = BleManager.instance.scanResults.listen((results) {
|
||||
if (mounted) {
|
||||
final mgrConnected = BleManager.instance.connectedDevice;
|
||||
setState(() {
|
||||
_connectedDevice = mgrConnected;
|
||||
_scanResults = _sortResults(results);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 先设置连接状态监听
|
||||
_connectionSubscription = BleManager.instance.connectionStream.listen((
|
||||
device,
|
||||
) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_connectedDevice = device;
|
||||
_isConnecting = false;
|
||||
if (device != null) {
|
||||
_scanResults = _sortResults(_scanResults);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 监听连接中状态(跨页面同步:详情页连接时列表页也能看到"连接中")
|
||||
_connectingSubscription = BleManager.instance.connectingStream.listen((
|
||||
device,
|
||||
) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
if (device != null) {
|
||||
_isConnecting = true;
|
||||
_connectingDevice = device;
|
||||
} else {
|
||||
_isConnecting = false;
|
||||
_connectingDevice = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 初始检查蓝牙状态并启动扫描(只调用一次)
|
||||
final currentState = await BleManager.instance.checkBluetooth();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_adapterState = currentState
|
||||
? BluetoothAdapterState.on
|
||||
: BluetoothAdapterState.off;
|
||||
});
|
||||
if (currentState) {
|
||||
_startScan();
|
||||
}
|
||||
}
|
||||
|
||||
// 监听后续蓝牙开关变化(用户操作)
|
||||
_stateSubscription = BleManager.instance.adapterState.listen((state) {
|
||||
if (mounted) {
|
||||
setState(() => _adapterState = state);
|
||||
if (state == BluetoothAdapterState.on) {
|
||||
_startScan();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _startScan() async {
|
||||
if (_isStartingScan) return;
|
||||
_isStartingScan = true;
|
||||
try {
|
||||
await BleManager.instance.requestPermissions();
|
||||
if (!mounted) return;
|
||||
await BleManager.instance.startScan(continuous: true);
|
||||
} finally {
|
||||
_isStartingScan = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _refreshScan() async {
|
||||
setState(() {
|
||||
_scanResults = [];
|
||||
});
|
||||
await BleManager.instance.refreshScan();
|
||||
if (!mounted) return;
|
||||
}
|
||||
|
||||
Future<void> _openBluetooth() async {
|
||||
await BleManager.instance.openBluetooth();
|
||||
}
|
||||
|
||||
/// 排序:已连接设备排第一,有名称设备优先
|
||||
List<ScanResult> _sortResults(List<ScanResult> results) {
|
||||
final mgrConnected = BleManager.instance.connectedDevice;
|
||||
final sorted = List<ScanResult>.from(results);
|
||||
sorted.sort((a, b) {
|
||||
// 第一级:已连接排最前
|
||||
final aConnected = a.device.remoteId == mgrConnected?.remoteId;
|
||||
final bConnected = b.device.remoteId == mgrConnected?.remoteId;
|
||||
if (aConnected && !bConnected) return -1;
|
||||
if (!aConnected && bConnected) return 1;
|
||||
// 第二级:有名称的排前面
|
||||
final aHasName = _hasDeviceName(a);
|
||||
final bHasName = _hasDeviceName(b);
|
||||
if (aHasName && !bHasName) return -1;
|
||||
if (!aHasName && bHasName) return 1;
|
||||
return 0;
|
||||
});
|
||||
return sorted;
|
||||
}
|
||||
|
||||
/// 判断设备是否有可读名称(非 MAC 地址)
|
||||
bool _hasDeviceName(ScanResult r) {
|
||||
final advData = r.advertisementData;
|
||||
return advData.advName.isNotEmpty ||
|
||||
r.device.platformName.isNotEmpty ||
|
||||
(advData.localName?.isNotEmpty == true) ||
|
||||
r.device.advName.isNotEmpty;
|
||||
}
|
||||
|
||||
/// 从详情页返回时同步连接状态(包括连接中和已连接)
|
||||
void _syncConnectedDevice() {
|
||||
final mgrConnected = BleManager.instance.connectedDevice;
|
||||
final mgrConnecting = BleManager.instance.connectingDevice;
|
||||
if (mgrConnected?.remoteId != _connectedDevice?.remoteId ||
|
||||
mgrConnecting?.remoteId != _connectingDevice?.remoteId) {
|
||||
setState(() {
|
||||
_connectedDevice = mgrConnected;
|
||||
_connectingDevice = mgrConnecting;
|
||||
_isConnecting = mgrConnecting != null;
|
||||
_scanResults = _sortResults(_scanResults);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _connectDevice(ScanResult result) async {
|
||||
// 如果已连接其他设备,弹出提示
|
||||
final currentConnected = BleManager.instance.connectedDevice;
|
||||
if (currentConnected != null &&
|
||||
currentConnected.remoteId != result.device.remoteId) {
|
||||
final connName = currentConnected.platformName.isNotEmpty
|
||||
? currentConnected.platformName
|
||||
: '${currentConnected.remoteId}';
|
||||
if (!mounted) return;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('已有设备连接'),
|
||||
content: Text('当前已连接设备:$connName\n\n请先断开当前设备后再连接新设备。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('知道了'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isConnecting = true;
|
||||
_connectingDevice = result.device;
|
||||
_connectingCountdown = 15;
|
||||
});
|
||||
|
||||
// 启动倒计时
|
||||
_connectingTimer?.cancel();
|
||||
_connectingTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (!mounted) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
if (_connectingCountdown > 0) {
|
||||
_connectingCountdown--;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
final error = await BleManager.instance.connect(result.device);
|
||||
|
||||
// 取消倒计时
|
||||
_connectingTimer?.cancel();
|
||||
_connectingTimer = null;
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_isConnecting = false;
|
||||
_connectingDevice = null;
|
||||
if (error == null) {
|
||||
_connectedDevice = result.device;
|
||||
}
|
||||
});
|
||||
|
||||
if (error == null && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'已连接 ${result.device.platformName.isEmpty ? result.device.remoteId : result.device.platformName}',
|
||||
),
|
||||
),
|
||||
);
|
||||
} else if (error != null && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(error),
|
||||
backgroundColor: Colors.red,
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _disconnectDevice() async {
|
||||
await BleManager.instance.disconnect();
|
||||
if (mounted) {
|
||||
setState(() => _connectedDevice = null);
|
||||
}
|
||||
}
|
||||
|
||||
bool get _isBluetoothOn => _adapterState == BluetoothAdapterState.on;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_stateSubscription?.cancel();
|
||||
_scanSubscription?.cancel();
|
||||
_connectionSubscription?.cancel();
|
||||
_connectingSubscription?.cancel();
|
||||
_connectingTimer?.cancel();
|
||||
unawaited(BleManager.instance.stopScan());
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
onPopInvokedWithResult: (didPop, result) {
|
||||
// 弹窗关闭时立即停止扫描,不等待 dispose() 动画延迟
|
||||
BleManager.instance.stopScan();
|
||||
},
|
||||
child: Container(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: MediaQuery.of(context).size.height * 0.85,
|
||||
),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(16),
|
||||
topRight: Radius.circular(16),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
Flexible(
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
_buildStatusCard(),
|
||||
if (_connectedDevice != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildConnectedDeviceCard(),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
_buildDeviceList(),
|
||||
const SizedBox(height: 12),
|
||||
_buildTipBar(),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
decoration: const BoxDecoration(
|
||||
color: Color(0xFF1677FF),
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(16),
|
||||
topRight: Radius.circular(16),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'蓝牙设备',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
_connectedDevice != null
|
||||
? '当前已连接: ${_connectedDevice!.platformName.isEmpty ? _connectedDevice!.remoteId : _connectedDevice!.platformName}'
|
||||
: _isBluetoothOn
|
||||
? '正在扫描周边设备...'
|
||||
: '请先打开蓝牙',
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh, color: Colors.white, size: 22),
|
||||
onPressed: _isBluetoothOn ? _refreshScan : null,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.white),
|
||||
onPressed: () {
|
||||
BleManager.instance.stopScan();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusCard() {
|
||||
final isOn = _isBluetoothOn;
|
||||
final hasConnected = _connectedDevice != null;
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF2F3F5),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: isOn ? const Color(0xFF165DFF) : const Color(0xFF86909C),
|
||||
),
|
||||
child: const Icon(Icons.bluetooth, color: Colors.white, size: 24),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
isOn ? '蓝牙已开启' : '蓝牙未打开',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
hasConnected
|
||||
? '已连接设备'
|
||||
: isOn
|
||||
? '正在扫描周边设备'
|
||||
: '请打开蓝牙以扫描附近设备',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
isOn
|
||||
? Container(
|
||||
width: 10,
|
||||
height: 10,
|
||||
decoration: const BoxDecoration(
|
||||
color: Color(0xFF00B42A),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
)
|
||||
: GestureDetector(
|
||||
onTap: _openBluetooth,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 14,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF165DFF),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: const Text(
|
||||
'去打开',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeviceList() {
|
||||
// 过滤掉已连接设备(它已在状态卡片下方单独显示)
|
||||
final otherResults = _connectedDevice != null
|
||||
? _scanResults
|
||||
.where((r) => r.device.remoteId != _connectedDevice!.remoteId)
|
||||
.toList()
|
||||
: _scanResults;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'附近设备',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (!_isBluetoothOn)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.bluetooth_disabled,
|
||||
size: 48,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
'蓝牙未开启,无法扫描设备',
|
||||
style: TextStyle(color: Color(0xFF86909C)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
else if (otherResults.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
'正在扫描附近设备...',
|
||||
style: TextStyle(color: Color(0xFF86909C)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
for (int i = 0; i < otherResults.length; i++) ...[
|
||||
_buildDeviceItem(otherResults[i]),
|
||||
if (i < otherResults.length - 1) const SizedBox(height: 10),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 已连接设备卡片(始终显示在状态卡片下方,不依赖扫描结果)
|
||||
Widget _buildConnectedDeviceCard() {
|
||||
final device = _connectedDevice!;
|
||||
final name = device.platformName.isNotEmpty
|
||||
? device.platformName
|
||||
: (device.advName.isNotEmpty ? device.advName : '${device.remoteId}');
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BleDeviceDetailPage(device: device),
|
||||
),
|
||||
).then((_) => _syncConnectedDevice());
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF0FFF4),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: const Color(0xFF00B42A), width: 1),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF00B42A).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.bluetooth_connected,
|
||||
color: Color(0xFF00B42A),
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
name,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 6,
|
||||
vertical: 2,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF00B42A),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: const Text(
|
||||
'已连接',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${device.remoteId}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: _disconnectDevice,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 10,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF00B42A),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: const Text(
|
||||
'断开',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeviceItem(ScanResult result) {
|
||||
final device = result.device;
|
||||
final advData = result.advertisementData;
|
||||
// 优先使用广告数据中的名称(含扫描响应),手机蓝牙列表也是这样显示的
|
||||
final name = advData.advName.isNotEmpty
|
||||
? advData.advName
|
||||
: (device.platformName.isNotEmpty
|
||||
? device.platformName
|
||||
: (advData.localName?.isNotEmpty == true
|
||||
? advData.localName!
|
||||
: (device.advName.isNotEmpty
|
||||
? device.advName
|
||||
: '${device.remoteId}')));
|
||||
final isConnected = _connectedDevice?.remoteId == device.remoteId;
|
||||
final isConnecting =
|
||||
_connectingDevice?.remoteId == device.remoteId && _isConnecting;
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) =>
|
||||
BleDeviceDetailPage(device: device, scanResult: result),
|
||||
),
|
||||
).then((_) => _syncConnectedDevice());
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF7F8FA),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF165DFF).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.devices,
|
||||
color: Color(0xFF165DFF),
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
name,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'${device.remoteId}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
if (isConnected) {
|
||||
_disconnectDevice();
|
||||
} else if (!isConnecting) {
|
||||
_connectDevice(result);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 10,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: isConnected
|
||||
? const Color(0xFF00B42A)
|
||||
: const Color(0xFFFF7D00),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: isConnecting
|
||||
? Text(
|
||||
'${_connectingCountdown}s',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
)
|
||||
: Text(
|
||||
isConnected ? '已连接' : '连接',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTipBar() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFFFF7E6),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Row(
|
||||
children: [
|
||||
Icon(Icons.info_outline, color: Color(0xFFFF7D00), size: 16),
|
||||
SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'请选择现场本机设备,避免连接无关设备',
|
||||
style: TextStyle(fontSize: 12, color: Color(0xFFFF7D00)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -4,9 +4,11 @@ import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_state.dart'; // 🔥 导入 AppUserState
|
||||
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
||||
import 'package:maibu_satabot_v2/features/remote_control/presentation/widgets/webrtc/webrtc_local_player.dart';
|
||||
import '../../../device_run_param/presentation/pages/robot_param_settings_page.dart';
|
||||
import '../../../device_run_param/presentation/pages/robot_param_settings_page.dart';
|
||||
|
||||
/// 机器人顶部信息卡片
|
||||
class RobotHeaderCard extends StatefulWidget {
|
||||
class RobotHeaderCard extends StatefulWidget {
|
||||
final Map<String, dynamic> robot;
|
||||
|
||||
const RobotHeaderCard({super.key, required this.robot});
|
||||
@@ -58,20 +60,26 @@ class _RobotHeaderCardState extends State<RobotHeaderCard> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.robot['name'] as String,
|
||||
(widget.robot['alias'] as String?)?.isNotEmpty == true
|
||||
? widget.robot['alias'] as String
|
||||
: '暂无别名',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'ID: ${widget.robot['id']}',
|
||||
widget.robot['name'] as String,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -115,7 +123,23 @@ class _RobotHeaderCardState extends State<RobotHeaderCard> {
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 设置图标
|
||||
const Icon(Icons.settings, size: 20, color: Color(0xFF86909C)),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => RobotParamSettingsPage(
|
||||
robot: widget.robot,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Icon(
|
||||
Icons.settings,
|
||||
size: 20,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../../home/presentation/pages/running_status_page.dart';
|
||||
import 'dart:async';
|
||||
|
||||
/// 机器人状态栏
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../../../../../components/device_status_modal.dart';
|
||||
import '../../../../devices/presentation/bloc/device_status_bloc.dart';
|
||||
import '../../../../devices/presentation/bloc/device_status_state.dart';
|
||||
import '../../../../remote_control/presentation/bloc/remote_control_cubit.dart';
|
||||
|
||||
/// 机器人状态栏 - 实时显示设备推送的运行状态
|
||||
class RobotStatusBar extends StatelessWidget {
|
||||
final Map<String, dynamic> robot;
|
||||
|
||||
@@ -11,67 +18,127 @@ class RobotStatusBar extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const RunningStatusPage(),
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(16),
|
||||
topRight: Radius.circular(16),
|
||||
),
|
||||
),
|
||||
builder: (BuildContext ctx) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider.value(value: GetIt.I<DeviceStatusBloc>()),
|
||||
BlocProvider.value(value: GetIt.I<RemoteControlCubit>()),
|
||||
],
|
||||
child: const DeviceStatusModal(),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x0D000000),
|
||||
blurRadius: 8,
|
||||
offset: Offset(0, 2),
|
||||
child: StreamBuilder<DeviceStatusState>(
|
||||
stream: GetIt.I<DeviceStatusBloc>().stream,
|
||||
initialData: GetIt.I<DeviceStatusBloc>().state,
|
||||
builder: (context, snapshot) {
|
||||
final state = snapshot.data;
|
||||
|
||||
String speed = '--';
|
||||
String mode = '--';
|
||||
String battery = '--';
|
||||
String signal = '--';
|
||||
Color signalColor = const Color(0xFF86909C);
|
||||
Color batteryColor = const Color(0xFF86909C);
|
||||
|
||||
if (state is DeviceStatusUpdated) {
|
||||
final s = state.status;
|
||||
speed = s.leftMeasureSpeed.toStringAsFixed(0);
|
||||
mode = s.controlMode == '3' ? '远程' : '本地';
|
||||
battery = s.battery.isNotEmpty ? s.battery : '--';
|
||||
|
||||
final qual = s.qual;
|
||||
if (qual >= 4) {
|
||||
signal = '强';
|
||||
signalColor = const Color(0xFF00B42A);
|
||||
} else if (qual >= 2) {
|
||||
signal = '中';
|
||||
signalColor = const Color(0xFFFF7D00);
|
||||
} else if (qual >= 1) {
|
||||
signal = '弱';
|
||||
signalColor = const Color(0xFFF53F3F);
|
||||
} else {
|
||||
signal = '无';
|
||||
signalColor = const Color(0xFF86909C);
|
||||
}
|
||||
|
||||
final batValue = int.tryParse(battery) ?? 0;
|
||||
if (batValue > 50) {
|
||||
batteryColor = const Color(0xFF00B42A);
|
||||
} else if (batValue > 20) {
|
||||
batteryColor = const Color(0xFFFF7D00);
|
||||
} else if (batValue > 0) {
|
||||
batteryColor = const Color(0xFFF53F3F);
|
||||
}
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x0D000000),
|
||||
blurRadius: 8,
|
||||
offset: Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Row(
|
||||
children: [
|
||||
_buildStatusItem(
|
||||
label: '速度',
|
||||
value: '1.2',
|
||||
unit: 'm/s',
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Row(
|
||||
children: [
|
||||
_buildStatusItem(
|
||||
label: '转速',
|
||||
value: speed,
|
||||
unit: 'rpm',
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '模式',
|
||||
value: mode,
|
||||
unit: '',
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '电量',
|
||||
value: battery,
|
||||
unit: '%',
|
||||
valueColor: batteryColor,
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '信号',
|
||||
value: signal,
|
||||
unit: '',
|
||||
valueColor: signalColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '里程',
|
||||
value: '2.36',
|
||||
unit: 'km',
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '电量',
|
||||
value: '82',
|
||||
unit: '%',
|
||||
valueColor: const Color(0xFF00B42A),
|
||||
),
|
||||
Container(width: 1, height: 40, color: const Color(0xFFE5E6EB)),
|
||||
_buildStatusItem(
|
||||
label: '信号',
|
||||
value: '强',
|
||||
unit: '',
|
||||
valueColor: const Color(0xFF00B42A),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
size: 16,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
size: 16,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -88,27 +155,31 @@ class RobotStatusBar extends StatelessWidget {
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
fontSize: 12,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const SizedBox(height: 6),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: valueColor ?? const Color(0xFF1D2129),
|
||||
Flexible(
|
||||
child: Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: valueColor ?? const Color(0xFF1D2129),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
if (unit.isNotEmpty)
|
||||
Text(
|
||||
unit,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontSize: 11,
|
||||
color: valueColor ?? const Color(0xFF1D2129),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user