80 lines
2.1 KiB
Dart
80 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class MulButton extends StatefulWidget {
|
|
// final controller = Get.find(tag: 'gnss');
|
|
final List<Color> colors;
|
|
final Function(int, bool) onSelectionChanged;
|
|
|
|
MulButton({
|
|
required this.colors,
|
|
required this.onSelectionChanged,
|
|
});
|
|
|
|
@override
|
|
_MulButtonState createState() => _MulButtonState();
|
|
}
|
|
|
|
class _MulButtonState extends State<MulButton> {
|
|
late List<bool> selectedColors;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
selectedColors = List.generate(widget.colors.length, (index) => false);
|
|
}
|
|
|
|
void updateSelection(int index, bool value) {
|
|
setState(() {
|
|
selectedColors[index] = value;
|
|
// List<Color> selected = [];
|
|
// for (int i = 0; i < selectedColors.length; i++) {
|
|
// if (selectedColors[i]) {
|
|
// selected.add(widget.colors[i]);
|
|
// }
|
|
// }
|
|
widget.onSelectionChanged(index, value); // 通知父组件
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Wrap(
|
|
spacing: 8.0, // 水平间距
|
|
runSpacing: 4.0, // 垂直间距
|
|
alignment: WrapAlignment.start,
|
|
children: List.generate(widget.colors.length, (index) {
|
|
return Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Checkbox(
|
|
value: selectedColors[index],
|
|
onChanged: (bool? value) {
|
|
if (value != null) {
|
|
updateSelection(index, value);
|
|
}
|
|
},
|
|
activeColor: widget.colors[index],
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(4), // 设置边角
|
|
),
|
|
),
|
|
Text(
|
|
colorToString(widget.colors[index]),
|
|
style: TextStyle(color: widget.colors[index]),
|
|
),
|
|
],
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
String colorToString(Color color) {
|
|
if (color == Colors.blue) return 'GPS';
|
|
if (color == Colors.red) return 'BDS';
|
|
if (color == Colors.green) return 'GLO';
|
|
if (color == Colors.orange) return 'ALS';
|
|
return 'Unknown Color';
|
|
}
|
|
}
|