From 4a83465d826f388ea1ef638818cb2b288e4cb749 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Tue, 28 Nov 2023 20:04:20 +1300 Subject: [PATCH] Make whole plan edit scrollable --- lib/days.dart | 63 ------------------------------------------ lib/edit_plan.dart | 69 ++++++++++++++++++++++++++++++---------------- lib/exercises.dart | 60 ---------------------------------------- 3 files changed, 45 insertions(+), 147 deletions(-) delete mode 100644 lib/days.dart delete mode 100644 lib/exercises.dart diff --git a/lib/days.dart b/lib/days.dart deleted file mode 100644 index 9cf5467..0000000 --- a/lib/days.dart +++ /dev/null @@ -1,63 +0,0 @@ -import 'package:flutter/material.dart'; - -class Days extends StatefulWidget { - final ValueChanged onChanged; - final String days; - - const Days({required this.onChanged, required this.days, super.key}); - - @override - createState() => _DaysState(); -} - -class _DaysState extends State { - final List _days = [ - 'Monday', - 'Tuesday', - 'Wednesday', - 'Thursday', - 'Friday', - 'Saturday', - 'Sunday' - ]; - late List _selections; - - @override - void initState() { - super.initState(); - final dayList = widget.days.split(','); - _selections = _days.map((day) => dayList.contains(day)).toList(); - } - - String _getSelectedDaysString() { - List 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), - ); - }), - ), - ), - ]); - } -} diff --git a/lib/edit_plan.dart b/lib/edit_plan.dart index c70df10..a981420 100644 --- a/lib/edit_plan.dart +++ b/lib/edit_plan.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/material.dart' as material; +import 'package:fmassive/constants.dart'; import 'package:fmassive/database.dart'; -import 'package:fmassive/days.dart'; -import 'package:fmassive/exercises.dart'; import 'package:fmassive/main.dart'; import 'package:moor_flutter/moor_flutter.dart'; @@ -18,6 +17,8 @@ class EditPlanPage extends StatefulWidget { class _EditPlanPageState extends State { late PlansCompanion plan; List names = []; + List? daySelections; + List? exerciseSelections; Future> getDistinctNames() async { final names = await (db.gymSets.selectOnly(distinct: true) @@ -29,10 +30,15 @@ class _EditPlanPageState extends State { @override void initState() { super.initState(); + final dayList = widget.plan.days.value.split(','); + daySelections = weekdayNames.map((day) => dayList.contains(day)).toList(); getDistinctNames().then((value) { setState(() { names = value; + final exercises = widget.plan.exercises.value.split(','); + exerciseSelections = + names.map((name) => exercises.contains(name)).toList(); }); }); @@ -110,30 +116,45 @@ class _EditPlanPageState extends State { } List get getChildren { + final List children = [ + Text('Days', style: Theme.of(context).textTheme.headlineSmall), + ]; + + final days = List.generate(7, (index) { + return SwitchListTile( + title: Text(weekdayNames[index]), + value: daySelections![index], + onChanged: (value) { + setState(() { + daySelections![index] = value; + }); + }, + ); + }); + + final exercises = List.generate(names.length, (index) { + return SwitchListTile( + title: Text(names[index] ?? ''), + value: exerciseSelections![index], + onChanged: (value) { + setState(() { + exerciseSelections![index] = value; + }); + }, + ); + }); + + children.addAll(days); + children.add( + Text('Exercises', style: Theme.of(context).textTheme.headlineSmall), + ); + children.addAll(exercises); + return [ Expanded( - child: Days( - onChanged: (days) { - setState(() { - plan = plan.copyWith(days: Value(days)); - }); - }, - days: plan.days.value, - ), - ), - names.isNotEmpty - ? Expanded( - child: Exercises( - onChanged: (exercises) { - setState(() { - plan = plan.copyWith(exercises: Value(exercises)); - }); - }, - exercises: plan.exercises.value, - names: names, - ), - ) - : const CircularProgressIndicator(), + child: ListView( + children: children, + )) ]; } } diff --git a/lib/exercises.dart b/lib/exercises.dart deleted file mode 100644 index 276e658..0000000 --- a/lib/exercises.dart +++ /dev/null @@ -1,60 +0,0 @@ -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), - ); - }), - ), - ), - ]); - } -}