import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../login_in/connect/blue_tooth.dart'; import '../login_in/connect/index.dart'; import '../login_in/getx/blue_tooth.dart'; final BlueToothController blueToothController = Get.put(BlueToothController()); BlueSetting blueSetting = BlueSetting(); class AntennaSetting extends GetView { const AntennaSetting({super.key}); @override Widget build(BuildContext context) { final size = MediaQuery.of(context).size; final isPortrait = MediaQuery.of(context).orientation == Orientation.portrait; // 获取当前的brightness信息 Brightness currentBrightness = Theme.of(context).brightness; bool isDarkMode = currentBrightness == Brightness.dark; Widget simpleDevice = Card( clipBehavior: Clip.hardEdge, child: CustomPaint( size: isPortrait ? Size(size.width, size.height / 2) : Size(size.width / 2, size.height - 80), painter: SimpleDevice(controller, isDarkMode), ), ); List inputList = [ // const Text("中心点位置(m):"), // Row( // children: [ // Expanded( // child: TextFormField( // enabled: false, // 设置为false以禁止输入 // initialValue: // controller.centerPoint.value.dx.toStringAsFixed(3), // keyboardType: TextInputType.number, // decoration: const InputDecoration(prefixText: 'x:'), // validator: (value) { // if (value == null || value.isEmpty) { // return '请输入'; // } // return null; // }, // onChanged: (String value) { // if (value != "") { // double point = double.parse(value); // if (point != 0) { // controller.centerPoint.value = // Offset(point, controller.centerPoint.value.dy); // } // } // }), // ), // const SizedBox( // width: 5, // ), // Expanded( // child: TextFormField( // enabled: false, // 设置为false以禁止输入 // initialValue: // controller.centerPoint.value.dy.toStringAsFixed(3), // keyboardType: TextInputType.number, // decoration: const InputDecoration(prefixText: 'y:'), // validator: (value) { // if (value == null || value.isEmpty) { // return '请输入'; // } // return null; // }, // onChanged: (String value) { // if (value != "") { // double point = double.parse(value); // if (point != 0) { // controller.centerPoint.value = // Offset(controller.centerPoint.value.dx, point); // } // } // }), // ) // ], // ), const Text("左侧位置(m):"), Row( children: [ Expanded( child: TextFormField( initialValue: controller.leftPoint.value.dx.toStringAsFixed(3), keyboardType: TextInputType.number, decoration: const InputDecoration(prefixText: 'x:'), validator: (value) { if (value == null || value.isEmpty) { return '请输入值'; } return null; }, onChanged: (String value) { if (value != "") { double point = double.parse(value); if (point != 0) { controller.leftPoint.value = Offset(point, controller.leftPoint.value.dy); } } }), ), const SizedBox( width: 5, ), Expanded( child: TextFormField( initialValue: controller.leftPoint.value.dy.toStringAsFixed(3), keyboardType: TextInputType.number, decoration: const InputDecoration(prefixText: 'y:'), validator: (value) { if (value == null || value.isEmpty) { return '请输入值'; } return null; }, onChanged: (String value) { if (value != "") { double point = double.parse(value); if (point != 0) { controller.leftPoint.value = Offset(controller.leftPoint.value.dx, point); } } }), ), ], ), const Text("右侧位置(m):"), Row( children: [ Expanded( child: TextFormField( initialValue: controller.rightPoint.value.dx.toStringAsFixed(3), keyboardType: TextInputType.number, decoration: const InputDecoration(prefixText: 'x:'), validator: (value) { if (value == null || value.isEmpty) { return '请输入值'; } return null; }, onChanged: (String value) { if (value != "") { double point = double.parse(value); if (point != 0) { controller.rightPoint.value = Offset(point, controller.rightPoint.value.dy); } } }), ), const SizedBox( width: 5, ), Expanded( child: TextFormField( initialValue: controller.rightPoint.value.dy.toStringAsFixed(3), keyboardType: TextInputType.number, decoration: const InputDecoration(prefixText: 'y:'), validator: (value) { if (value == null || value.isEmpty) { return '请输入值'; } return null; }, onChanged: (String value) { if (value != "") { double point = double.parse(value); if (point != 0) { controller.rightPoint.value = Offset(controller.rightPoint.value.dx, point); } } }), ) ], ), ]; Widget button = TextButton( onPressed: () { Offset center = controller.leftPoint.value; Offset right = controller.rightPoint.value - controller.leftPoint.value; Connect().writeAntenna( Offset(-center.dx, center.dy), Offset(right.dx, -right.dy), blueToothController.connectedDeviceID.value); }, child: const Text("位置更新")); return Scaffold( appBar: AppBar( title: const Text('天线位置设置'), toolbarHeight: 40, ), body: SingleChildScrollView( child: isPortrait ? Column( children: [ simpleDevice, Card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [...inputList, button], ), ) ], ) : Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ simpleDevice, Expanded( child: Container( padding: const EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [...inputList, button], ), )) ], ), ), ); } } // 矩形图 class SimpleDevice extends CustomPainter { final AntennaController controller; final bool isDarkMode; SimpleDevice(this.controller, this.isDarkMode) : super(); @override void paint(Canvas canvas, Size size) { final Paint paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2.0; paint.color = isDarkMode ? Colors.white : Colors.black; final Path path = Path(); double w = size.width; if (size.width > size.height) { w = size.height; } double unit = w * .1; double width = w * .2; double height = w * .8; double left = (size.width - width) / 2; double top = (size.height - height) / 2; canvas.translate(left, top); canvas.drawRect(Rect.fromLTWH(0, 0, width, height), paint); Offset center = Offset(controller.centerPoint.value.dx * unit, controller.centerPoint.value.dy * unit); double radius = 10; canvas.drawCircle(center, radius, paint); canvas.translate(center.dx, center.dy); Offset leftPoint = Offset(controller.leftPoint.value.dx * unit, controller.leftPoint.value.dy * unit); Offset rightPoint = Offset(controller.rightPoint.value.dx * unit, controller.rightPoint.value.dy * unit); canvas.drawLine(const Offset(10, 0), rightPoint, paint); canvas.drawLine(const Offset(-10, 0), leftPoint, paint); canvas.drawCircle(rightPoint, 10, paint); canvas.drawCircle(leftPoint, 10, paint); canvas.translate(-center.dx, -center.dy); canvas.drawPath(path, paint); canvas.translate(-left, -top); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => true; } class AntennaController extends GetxController { var leftPoint = const Offset(-1, 3).obs; var rightPoint = const Offset(1, 3).obs; var centerPoint = const Offset(1.0, 0.5).obs; }