55 lines
1.3 KiB
Dart
55 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class SelectButton extends StatefulWidget {
|
|
final List<String> options;
|
|
final int defaultIndex;
|
|
final bool allowNull;
|
|
final bool isDisabled;
|
|
final void Function(int selection) onChange;
|
|
const SelectButton(
|
|
{super.key,
|
|
required this.options,
|
|
this.defaultIndex = 0,
|
|
this.allowNull = true,
|
|
this.isDisabled = false,
|
|
required this.onChange});
|
|
|
|
@override
|
|
State<SelectButton> createState() => _SelectButtonState();
|
|
}
|
|
|
|
class _SelectButtonState extends State<SelectButton> {
|
|
int _selection = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selection = widget.defaultIndex;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Wrap(
|
|
spacing: 5.0,
|
|
children: List<Widget>.generate(
|
|
widget.options.length,
|
|
(int index) {
|
|
return ChoiceChip(
|
|
label: Text(widget.options[index]),
|
|
selected: _selection == index,
|
|
onSelected: (bool selected) {
|
|
if (widget.isDisabled) return;
|
|
if (!selected && !widget.allowNull) return;
|
|
|
|
setState(() {
|
|
_selection = selected ? index : -1;
|
|
widget.onChange(_selection);
|
|
});
|
|
},
|
|
);
|
|
},
|
|
).toList(),
|
|
);
|
|
}
|
|
}
|