fmassive/lib/days.dart

64 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
class Days extends StatefulWidget {
final ValueChanged<String> onChanged;
final String days;
const Days({required this.onChanged, required this.days, super.key});
@override
createState() => _DaysState();
}
class _DaysState extends State<Days> {
final List<String> _days = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
late List<bool> _selections;
@override
void initState() {
super.initState();
final dayList = widget.days.split(',');
_selections = _days.map((day) => dayList.contains(day)).toList();
}
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: [
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),
);
}),
),
),
]);
}
}