wifi_item.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:smartledz_wifi_test/models/wifi_model.dart';
  4. class WifiItem extends StatefulWidget {
  5. final WifiModel wifiModel; // wifi模型
  6. final bool active; // 是否选中
  7. final Function(WifiModel wifiModel)? onTap; // 是否选中
  8. const WifiItem({
  9. super.key,
  10. required this.wifiModel,
  11. this.active = false,
  12. this.onTap,
  13. });
  14. @override
  15. State<WifiItem> createState() => _WifiItemState();
  16. }
  17. class _WifiItemState extends State<WifiItem> {
  18. // 测试结果与颜色的对应关系
  19. final Map<String, Color> resultAsColor = {
  20. "未测试": Colors.grey,
  21. "正在测试": Colors.blue,
  22. "测试通过": Colors.green,
  23. "未通过": Colors.red,
  24. "连接失败": Colors.red,
  25. };
  26. @override
  27. Widget build(BuildContext context) {
  28. double borderWidth = .7.sp; // 边框宽度
  29. Color resultColor = Colors.grey; // 测试结果文本颜色
  30. if(resultAsColor.containsKey(widget.wifiModel.result)){
  31. resultColor = resultAsColor[widget.wifiModel.result]!;
  32. }
  33. return GestureDetector(
  34. onTap: ()=>widget.onTap?.call(widget.wifiModel),
  35. child: Container(
  36. height: 50.sp,
  37. decoration: BoxDecoration(
  38. color: widget.active? Colors.pink.shade100: null,
  39. border: Border(
  40. bottom: BorderSide(color: Colors.grey, width: borderWidth)
  41. )
  42. ),
  43. child: Row(
  44. children: [
  45. Expanded(
  46. child: Container(
  47. alignment: Alignment.centerLeft,
  48. padding: EdgeInsets.symmetric(horizontal: 5.sp),
  49. child: Text(widget.wifiModel.ssid, style: TextStyle(fontSize: 18.sp)),
  50. ),
  51. ),
  52. Container(
  53. alignment: Alignment.center,
  54. width: 50.w,
  55. decoration: BoxDecoration(
  56. border: Border.symmetric(vertical: BorderSide(color: Colors.grey, width: borderWidth))
  57. ),
  58. child: Text(widget.wifiModel.rssi.toString(), style: TextStyle(fontSize: 14.sp, color: Colors.green)),
  59. ),
  60. Container(
  61. alignment: Alignment.center,
  62. width: 80.w,
  63. child: Text(widget.wifiModel.result, style: TextStyle(fontSize: 14.sp, color: resultColor)),
  64. ),
  65. ],
  66. ),
  67. ),
  68. );
  69. }
  70. }