Add days widget

This commit is contained in:
Brandon Presley 2023-06-25 15:47:45 +12:00
parent 0a7675797b
commit 4e2ca56a69
2 changed files with 59 additions and 14 deletions

53
lib/days.dart Normal file
View File

@ -0,0 +1,53 @@
import 'package:flutter/material.dart';
class Days extends StatefulWidget {
final ValueChanged<String> onChanged;
const Days({required this.onChanged, super.key});
@override
createState() => _DaysState();
}
class _DaysState extends State<Days> {
final List<bool> _selections = List.generate(7, (_) => false);
final List<String> _days = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
];
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: List.generate(7, (index) {
return SwitchListTile(
title: Text(_days[index]),
value: _selections[index],
onChanged: (value) => _updateSelections(index),
);
}),
);
}
}

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
import 'package:fmassive/database.dart';
import 'package:fmassive/days.dart';
import 'package:fmassive/main.dart';
import 'package:moor_flutter/moor_flutter.dart';
@ -59,20 +60,11 @@ class _EditPlanPageState extends State<EditPlanPage> {
child: material.Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _daysController,
focusNode: daysNode,
decoration: const InputDecoration(labelText: 'Days'),
onTap: () {
_daysController.selection = TextSelection(
baseOffset: 0, extentOffset: _daysController.text.length);
},
onChanged: (value) {
Days(onChanged: (days) {
setState(() {
plan = plan.copyWith(days: Value(value));
days = days;
});
},
),
}),
TextFormField(
controller: _workoutsController,
focusNode: workoutsNode,