2024-07-30 18:20:27 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2024-07-31 19:04:11 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-07-30 18:20:27 +08:00
|
|
|
|
|
|
|
class SingleButton extends StatefulWidget {
|
2024-07-31 19:04:11 +08:00
|
|
|
final controller = Get.find(tag: 'gnss');
|
2024-07-30 18:20:27 +08:00
|
|
|
final List<Color> colors;
|
|
|
|
final Function(Color) onSelectionChanged;
|
|
|
|
|
|
|
|
SingleButton({
|
|
|
|
required this.colors,
|
|
|
|
required this.onSelectionChanged,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_SingleButtonState createState() => _SingleButtonState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SingleButtonState extends State<SingleButton> {
|
|
|
|
late Color selectedColor;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
selectedColor = widget.colors[0]; // 默认选中第一个颜色
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateSelection(Color color) {
|
|
|
|
setState(() {
|
|
|
|
selectedColor = color;
|
|
|
|
widget.onSelectionChanged(color); // 通知父组件
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: List.generate(widget.colors.length, (index) {
|
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Radio<Color>(
|
|
|
|
value: widget.colors[index],
|
|
|
|
groupValue: selectedColor,
|
|
|
|
onChanged: (Color? value) {
|
|
|
|
if (value != null) {
|
|
|
|
updateSelection(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
activeColor: widget.colors[index],
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
colorToString(widget.colors[index]),
|
|
|
|
style: TextStyle(color: widget.colors[index]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String colorToString(Color color) {
|
2024-07-31 19:04:11 +08:00
|
|
|
if (color == Colors.blue) return 'GPS';
|
|
|
|
if (color == Colors.red) return 'BDS';
|
|
|
|
if (color == Colors.green) return 'GLO';
|
|
|
|
if (color == Colors.orange) return 'ALS';
|
2024-07-30 18:20:27 +08:00
|
|
|
return 'Unknown Color';
|
|
|
|
}
|
|
|
|
}
|