fmassive/lib/exercises.dart

61 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
class Exercises extends StatefulWidget {
final ValueChanged<String> onChanged;
final String? exercises;
final List<String?> names;
const Exercises(
{required this.onChanged,
required this.exercises,
super.key,
required this.names});
@override
createState() => _ExercisesState();
}
class _ExercisesState extends State<Exercises> {
late List<bool> _selections;
@override
initState() {
super.initState();
final exercises = widget.exercises?.split(',');
_selections =
widget.names.map((name) => exercises?.contains(name) ?? false).toList();
}
String _getExercises() {
List<String> exercises = [];
for (int i = 0; i < _selections.length; i++)
if (_selections[i]) exercises.add(widget.names[i] ?? '');
return exercises.join(",");
}
void _updateSelections(int index) {
setState(() {
_selections[index] = !_selections[index];
widget.onChanged(_getExercises());
});
}
@override
Widget build(BuildContext context) {
return Column(children: [
Text('Exercises', style: Theme.of(context).textTheme.headlineSmall),
Expanded(
child: ListView(
children: List.generate(widget.names.length, (index) {
return SwitchListTile(
title: Text(widget.names[index] ?? ''),
value: _selections[index],
onChanged: (value) => _updateSelections(index),
);
}),
),
),
]);
}
}