fmassive/lib/days.dart

64 lines
1.5 KiB
Dart
Raw Normal View History

2023-06-25 15:47:45 +12:00
import 'package:flutter/material.dart';
class Days extends StatefulWidget {
final ValueChanged<String> onChanged;
2023-11-10 23:43:06 +13:00
final String days;
2023-06-25 15:47:45 +12:00
2023-11-10 23:43:06 +13:00
const Days({required this.onChanged, required this.days, super.key});
2023-06-25 15:47:45 +12:00
@override
createState() => _DaysState();
}
class _DaysState extends State<Days> {
final List<String> _days = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
2023-11-10 23:43:06 +13:00
late List<bool> _selections;
@override
void initState() {
super.initState();
final dayList = widget.days.split(',');
_selections = _days.map((day) => dayList.contains(day)).toList();
}
2023-06-25 15:47:45 +12:00
String _getSelectedDaysString() {
List<String> selectedDays = [];
2023-11-10 23:43:06 +13:00
for (int i = 0; i < _selections.length; i++)
if (_selections[i]) selectedDays.add(_days[i]);
2023-06-25 15:47:45 +12:00
return selectedDays.join(",");
}
void _updateSelections(int index) {
setState(() {
_selections[index] = !_selections[index];
widget.onChanged(_getSelectedDaysString());
});
}
@override
Widget build(BuildContext context) {
2023-11-10 23:43:06 +13:00
return Column(children: [
Text('Days', style: Theme.of(context).textTheme.headlineSmall),
Expanded(
child: ListView(
children: List.generate(7, (index) {
return SwitchListTile(
title: Text(_days[index]),
value: _selections[index],
onChanged: (value) => _updateSelections(index),
);
}),
),
),
]);
2023-06-25 15:47:45 +12:00
}
}