import 'package:flutter/material.dart';
import 'dart:math' as math;
class WirelessSignalVisualization extends StatefulWidget {
  const WirelessSignalVisualization({Key? key}) : super(key: key);
@override
State<WirelessSignalVisualization> createState() => _WirelessSignalVisualizationState();
}
class _WirelessSignalVisualizationState extends State<WirelessSignalVisualization> {
// 吸顶AP的位置,这里以画布中心为例
Offset apPosition = const Offset(150, 150);
// 画布大小
Size canvasSize = const Size(300, 300);
// 模拟墙体的线条列表,每个元素是一个包含两个Offset的列表,表示线段的两个端点
List<List<Offset>> walls = [
[const Offset(50, 50), const Offset(250, 50)],
[const Offset(250, 50), const Offset(250, 250)],
[const Offset(250, 250), const Offset(50, 250)],
[const Offset(50, 250), const Offset(50, 50)],
  ]; 
// 计算某点的信号强度,简化为距离越远强度越低
double _calculateSignalStrength(Offset point) {
double distance = math.sqrt(math.pow(point.dx - apPosition.dx, 2) + math.pow(point.dy - apPosition.dy, 2));
// 最大距离取画布对角线长度
double maxDistance = math.sqrt(math.pow(canvasSize.width, 2) + math.pow(canvasSize.height, 2));
// 将距离映射到0 - 1的信号强度范围
return 1 - (distance / maxDistance);
  }
// 根据信号强度获取对应的颜色,从绿色到红色渐变
Color _getColorFromStrength(double strength) {
int red = (255 * (1 - strength)).toInt();
int green = (255 * strength).toInt();
return Color.fromRGBO(red, green, 0, 1);
  }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('室内无线信号覆盖仿真'),
),
body: Center(
child: CustomPaint(
size: canvasSize,
painter: _SignalVisualizationPainter(
apPosition: apPosition,
walls: walls,
calculateSignalStrength: _calculateSignalStrength,
getColorFromStrength: _getColorFromStrength,
),
),
),
);
}
}
class _SignalVisualizationPainter extends CustomPainter {
final Offset apPosition;
final List<List<Offset>> walls;
final double Function(Offset point) calculateSignalStrength;
  final Color Function(double strength) getColorFromStrength;
_SignalVisualizationPainter({
required this.apPosition,
required this.walls,
required this.calculateSignalStrength,
required this.getColorFromStrength,
  });
@override
void paint(Canvas canvas, Size size) {
// 绘制代表信号强度的区域,将画布划分为小网格来近似模拟
int gridCount = 50;
double cellWidth = size.width / gridCount;
double cellHeight = size.height / gridCount;
for (int i = 0; i < gridCount; i++) {
for (int j = 0; j < gridCount; j++) {
double x = i * cellWidth;
double y = j * cellHeight;
Offset center = Offset(x + cellWidth / 2, y + cellHeight / 2);
double strength = calculateSignalStrength(center);
Color color = getColorFromStrength(strength);
Paint paint = Paint()..color = color;
canvas.drawRect(
Rect.fromLTWH(x, y, cellWidth, cellHeight),
paint,
);
}
    }
// 绘制吸顶AP
Paint apPaint = Paint()
..color = Colors.white
..style = PaintingStyle.fill;
    canvas.drawCircle(apPosition, 10, apPaint);
// 绘制墙体
Paint wallPaint = Paint()
..color = Colors.black
..strokeWidth = 3;
for (var wall in walls) {
canvas.drawLine(wall[0], wall[1], wallPaint);
}
  }
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}