From 8ad2cfacfe8decba8ee57c759949b3ae409ec5a7 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Mon, 28 Dec 2020 12:45:38 +0100 Subject: [PATCH] Move workout day form to own file --- lib/widgets/workouts/forms.dart | 110 ++++++++++++++++ lib/widgets/workouts/workout_plan_detail.dart | 122 ++---------------- 2 files changed, 124 insertions(+), 108 deletions(-) create mode 100644 lib/widgets/workouts/forms.dart diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart new file mode 100644 index 00000000..c4262396 --- /dev/null +++ b/lib/widgets/workouts/forms.dart @@ -0,0 +1,110 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:wger/locale/locales.dart'; +import 'package:wger/models/workouts/day.dart'; +import 'package:wger/models/workouts/workout_plan.dart'; +import 'package:wger/providers/workout_plans.dart'; + +class DayFormWidget extends StatelessWidget { + final WorkoutPlan workout; + + DayFormWidget({ + Key key, + @required this.dayController, + @required Map dayData, + @required this.workout, + }) : _dayData = dayData, + super(key: key); + + final TextEditingController dayController; + final Map _dayData; + final _form = GlobalKey(); + + @override + Widget build(BuildContext context) { + return Container( + margin: EdgeInsets.all(20), + child: Form( + key: _form, + child: Column( + children: [ + Text( + AppLocalizations.of(context).newDay, + style: Theme.of(context).textTheme.headline6, + ), + TextFormField( + decoration: InputDecoration(labelText: AppLocalizations.of(context).description), + controller: dayController, + onSaved: (value) { + _dayData['description'] = value; + }, + validator: (value) { + const minLenght = 5; + const maxLenght = 100; + if (value.isEmpty || value.length < minLenght || value.length > maxLenght) { + return 'Please enter between $minLenght and $maxLenght characters.'; + } + return null; + }, + ), + TextFormField( + decoration: InputDecoration(labelText: 'TODO: Checkbox for days'), + enabled: false, + ), + //...Day().weekdays.values.map((dayName) => DayCheckbox(dayName)).toList(), + ElevatedButton( + child: Text(AppLocalizations.of(context).save), + onPressed: () async { + if (!_form.currentState.validate()) { + return; + } + _form.currentState.save(); + + try { + Provider.of(context, listen: false) + .addDay(Day(description: dayController.text, daysOfWeek: [1]), workout); + dayController.clear(); + Navigator.of(context).pop(); + } catch (error) { + await showDialog( + context: context, + builder: (ctx) => AlertDialog( + title: Text('An error occurred!'), + content: Text('Something went wrong.'), + actions: [ + TextButton( + child: Text('Okay'), + onPressed: () { + Navigator.of(ctx).pop(); + }, + ) + ], + ), + ); + } + }, + ), + ], + ), + ), + ); + } +} diff --git a/lib/widgets/workouts/workout_plan_detail.dart b/lib/widgets/workouts/workout_plan_detail.dart index 9c86fe3a..fff479fb 100644 --- a/lib/widgets/workouts/workout_plan_detail.dart +++ b/lib/widgets/workouts/workout_plan_detail.dart @@ -18,12 +18,11 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; -import 'package:provider/provider.dart'; import 'package:wger/locale/locales.dart'; -import 'package:wger/models/workouts/day.dart'; import 'package:wger/models/workouts/workout_plan.dart'; -import 'package:wger/providers/workout_plans.dart'; +import 'package:wger/widgets/core/bottom_sheet.dart'; import 'package:wger/widgets/workouts/day.dart'; +import 'package:wger/widgets/workouts/forms.dart'; class DayCheckbox extends StatefulWidget { String name; @@ -66,21 +65,6 @@ class _WorkoutPlanDetailState extends State { 'daysOfWeek': [1], }; - Widget showDaySheet(BuildContext outerContext, WorkoutPlan workout) { - showModalBottomSheet( - context: outerContext, - builder: (BuildContext context) { - return Container( - child: DayFormWidget( - dayController: dayController, - dayData: _dayData, - workout: workout, - ), - ); - }, - ); - } - @override Widget build(BuildContext context) { return SingleChildScrollView( @@ -106,10 +90,18 @@ class _WorkoutPlanDetailState extends State { Column( children: [ ElevatedButton( - child: Text(AppLocalizations.of(context).add), - onPressed: () { - showDaySheet(context, widget._workoutPlan); - }), + child: Text(AppLocalizations.of(context).add), + onPressed: () { + showFormBottomSheet( + context, + AppLocalizations.of(context).newDay, + DayFormWidget( + dayController: dayController, + dayData: _dayData, + workout: widget._workoutPlan, + )); + }, + ), ], ), ], @@ -118,89 +110,3 @@ class _WorkoutPlanDetailState extends State { ); } } - -class DayFormWidget extends StatelessWidget { - final WorkoutPlan workout; - - DayFormWidget({ - Key key, - @required this.dayController, - @required Map dayData, - @required this.workout, - }) : _dayData = dayData, - super(key: key); - - final TextEditingController dayController; - final Map _dayData; - final _form = GlobalKey(); - - @override - Widget build(BuildContext context) { - return Container( - margin: EdgeInsets.all(20), - child: Form( - key: _form, - child: Column( - children: [ - Text( - AppLocalizations.of(context).newDay, - style: Theme.of(context).textTheme.headline6, - ), - TextFormField( - decoration: InputDecoration(labelText: AppLocalizations.of(context).description), - controller: dayController, - onSaved: (value) { - _dayData['description'] = value; - }, - validator: (value) { - const minLenght = 5; - const maxLenght = 100; - if (value.isEmpty || value.length < minLenght || value.length > maxLenght) { - return 'Please enter between $minLenght and $maxLenght characters.'; - } - return null; - }, - ), - TextFormField( - decoration: InputDecoration(labelText: 'TODO: Checkbox for days'), - enabled: false, - ), - //...Day().weekdays.values.map((dayName) => DayCheckbox(dayName)).toList(), - ElevatedButton( - child: Text(AppLocalizations.of(context).save), - onPressed: () async { - if (!_form.currentState.validate()) { - return; - } - _form.currentState.save(); - - try { - Provider.of(context, listen: false) - .addDay(Day(description: dayController.text, daysOfWeek: [1]), workout); - //dayController.text = ''; - //Navigator.of(context).pop(); - } catch (error) { - await showDialog( - context: context, - builder: (ctx) => AlertDialog( - title: Text('An error occurred!'), - content: Text('Something went wrong.'), - actions: [ - TextButton( - child: Text('Okay'), - onPressed: () { - Navigator.of(ctx).pop(); - }, - ) - ], - ), - ); - } - }, - ), - ], - ), - ), - ); - } -}