Files
Hirameki-SRS/lib/src/widgets/options_grid.dart
2025-10-31 07:39:32 +01:00

71 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
class OptionsGrid extends StatelessWidget {
final List<String> options;
final void Function(String) onSelected;
final Color? buttonColor;
final Color? textColor;
final bool isDisabled;
final String? selectedOption;
final List<String>? correctAnswers;
final bool showResult;
const OptionsGrid({
super.key,
required this.options,
required this.onSelected,
this.buttonColor,
this.textColor,
this.isDisabled = false,
this.selectedOption,
this.correctAnswers,
this.showResult = false,
});
@override
Widget build(BuildContext context) {
if (options.isEmpty) return const SizedBox.shrink();
final theme = Theme.of(context);
final bg = buttonColor ?? theme.colorScheme.primary;
final fg = textColor ?? theme.colorScheme.onPrimary;
return Wrap(
spacing: 10,
runSpacing: 10,
alignment: WrapAlignment.center,
children: options.map((o) {
Color currentButtonColor = bg;
Color currentTextColor = fg;
if (showResult) {
if (correctAnswers != null && correctAnswers!.contains(o)) {
currentButtonColor = theme.colorScheme.tertiary;
} else if (o == selectedOption) {
currentButtonColor = theme.colorScheme.error;
}
}
return SizedBox(
width: 160,
child: ElevatedButton(
onPressed: isDisabled ? null : () => onSelected(o),
style: ElevatedButton.styleFrom(
backgroundColor: currentButtonColor,
foregroundColor: currentTextColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8),
),
child: Text(
o,
style: theme.textTheme.titleMedium?.copyWith(color: currentTextColor),
textAlign: TextAlign.center,
),
),
);
}).toList(),
);
}
}