立即分享

This commit is contained in:
czz
2025-07-15 15:46:29 +08:00
parent 8fae793f61
commit f10963a172
2 changed files with 84 additions and 61 deletions

View File

@@ -28,36 +28,36 @@ class _BluetoothPageState extends State<BluetoothPage> {
void initState() {
super.initState();
obsData = Map<String, dynamic>.from(widget.data).obs; // 复制成 obs
String text;
// String text;
if (widget.data.containsKey('name') &&
widget.data['name'] != null &&
widget.data['name'].toString().isNotEmpty) {
text = widget.data['name'].toString();
} else {
text = '未命名'.tr;
}
// if (obsData.containsKey('name') &&
// obsData['name'] != null &&
// obsData['name'].toString().isNotEmpty) {
// text = obsData['name'].toString();
// } else {
// text = '未命名'.tr;
// }
_calculateTextHalfWidth(text);
// _calculateTextHalfWidth(text);
}
void _calculateTextHalfWidth(String text) {
final textSpan = TextSpan(
text: text,
style: TextStyle(
fontSize: 40.rpx,
height: 1,
),
);
final textPainter = TextPainter(
text: textSpan,
textDirection: ui.TextDirection.ltr,
);
textPainter.layout(); // 计算文本宽度
setState(() {
_textHalfWidth = textPainter.width / 2;
});
}
// void _calculateTextHalfWidth(String text) {
// final textSpan = TextSpan(
// text: text,
// style: TextStyle(
// fontSize: 40.rpx,
// height: 1,
// ),
// );
// final textPainter = TextPainter(
// text: textSpan,
// textDirection: ui.TextDirection.ltr,
// );
// textPainter.layout(); // 计算文本宽度
// setState(() {
// _textHalfWidth = textPainter.width / 2;
// });
// }
BoxConstraints? bodysize;
DeviceListController deviceListController = Get.find();
@@ -133,11 +133,14 @@ class _BluetoothPageState extends State<BluetoothPage> {
maxLines: 1, // 限制为单行
),
Positioned(
left:
MediaQuery.of(context).size.width /
2 +
_textHalfWidth +
22.rpx,
left: MediaQuery.of(context)
.size
.width /
2 +
_calculateTextHalfWidth(
obsData['name']?.toString() ??
'未命名') +
22.rpx,
top: 5.rpx,
child: ClickableContainer(
backgroundColor: Colors.transparent,
@@ -425,3 +428,18 @@ class _BluetoothPageState extends State<BluetoothPage> {
);
}
}
double _calculateTextHalfWidth(String text) {
final textPainter = TextPainter(
text: TextSpan(
text: text,
style: TextStyle(
fontSize: 40.rpx,
height: 1,
),
),
textDirection: ui.TextDirection.ltr,
);
textPainter.layout();
return textPainter.width / 2;
}