import 'package:flutter/material.dart'; class Exercises extends StatefulWidget { final ValueChanged onChanged; final String? exercises; final List names; const Exercises( {required this.onChanged, required this.exercises, super.key, required this.names}); @override createState() => _ExercisesState(); } class _ExercisesState extends State { late List _selections; @override initState() { super.initState(); final exercises = widget.exercises?.split(','); _selections = widget.names.map((name) => exercises?.contains(name) ?? false).toList(); } String _getExercises() { List 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), ); }), ), ), ]); } }