54 lines
1.2 KiB
Dart
54 lines
1.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class Days extends StatefulWidget {
|
|
final ValueChanged<String> onChanged;
|
|
|
|
const Days({required this.onChanged, super.key});
|
|
|
|
@override
|
|
createState() => _DaysState();
|
|
}
|
|
|
|
class _DaysState extends State<Days> {
|
|
final List<bool> _selections = List.generate(7, (_) => false);
|
|
final List<String> _days = [
|
|
'Monday',
|
|
'Tuesday',
|
|
'Wednesday',
|
|
'Thursday',
|
|
'Friday',
|
|
'Saturday',
|
|
'Sunday'
|
|
];
|
|
|
|
String _getSelectedDaysString() {
|
|
List<String> selectedDays = [];
|
|
for (int i = 0; i < _selections.length; i++) {
|
|
if (_selections[i]) {
|
|
selectedDays.add(_days[i]);
|
|
}
|
|
}
|
|
return selectedDays.join(",");
|
|
}
|
|
|
|
void _updateSelections(int index) {
|
|
setState(() {
|
|
_selections[index] = !_selections[index];
|
|
widget.onChanged(_getSelectedDaysString());
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: List.generate(7, (index) {
|
|
return SwitchListTile(
|
|
title: Text(_days[index]),
|
|
value: _selections[index],
|
|
onChanged: (value) => _updateSelections(index),
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
}
|