379 lines
16 KiB
Dart
379 lines
16 KiB
Dart
import 'dart:collection';
|
|
|
|
import 'package:aitrainer_app/bloc/exercise_new/exercise_new_bloc.dart';
|
|
import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
|
|
import 'package:aitrainer_app/localization/app_language.dart';
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/repository/customer_repository.dart';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:aitrainer_app/util/trans.dart';
|
|
import 'package:aitrainer_app/widgets/app_bar.dart';
|
|
import 'package:aitrainer_app/widgets/bmi_widget.dart';
|
|
import 'package:aitrainer_app/widgets/bmr_widget.dart';
|
|
import 'package:aitrainer_app/widgets/size_widget.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:keyboard_actions/keyboard_actions.dart';
|
|
|
|
class ExerciseNewPage extends StatefulWidget {
|
|
_ExerciseNewPageState createState() => _ExerciseNewPageState();
|
|
}
|
|
|
|
class _ExerciseNewPageState extends State<ExerciseNewPage> with Trans {
|
|
final FocusNode _nodeText1 = FocusNode();
|
|
final FocusNode _nodeText2 = FocusNode();
|
|
|
|
KeyboardActionsConfig _buildConfig(BuildContext context) {
|
|
return KeyboardActionsConfig(
|
|
keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
|
|
keyboardBarColor: Colors.grey[200],
|
|
keyboardSeparatorColor: Colors.black26,
|
|
nextFocus: true,
|
|
actions: [
|
|
KeyboardActionsItem(focusNode: _nodeText2, toolbarButtons: [
|
|
(node) {
|
|
return GestureDetector(
|
|
onTap: () => node.unfocus(),
|
|
child: Container(
|
|
padding: EdgeInsets.all(8.0),
|
|
color: Colors.orange[500],
|
|
child: Text(
|
|
t("Done"),
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
]),
|
|
KeyboardActionsItem(
|
|
focusNode: _nodeText1,
|
|
toolbarButtons: [
|
|
//button 2
|
|
(node) {
|
|
return GestureDetector(
|
|
onTap: () => node.unfocus(),
|
|
child: Container(
|
|
color: Colors.orange,
|
|
padding: EdgeInsets.all(8.0),
|
|
child: Text(
|
|
t("Done"),
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final ExerciseType exerciseType = ModalRoute.of(context).settings.arguments;
|
|
// ignore: close_sinks
|
|
final menuBloc = BlocProvider.of<MenuBloc>(context);
|
|
setContext(context);
|
|
|
|
return BlocProvider(
|
|
create: (context) => ExerciseNewBloc(
|
|
exerciseRepository: ExerciseRepository(),
|
|
menuBloc: menuBloc,
|
|
customerRepository: CustomerRepository(),
|
|
exerciseType: exerciseType)
|
|
..add(ExerciseNewLoad()),
|
|
child: BlocConsumer<ExerciseNewBloc, ExerciseNewState>(
|
|
listener: (context, state) {
|
|
if (state is ExerciseNewLoading) {
|
|
//LoadingDialog.show(context);
|
|
} else if (state is ExerciseNewError) {
|
|
//LoadingDialog.hide(context);
|
|
Scaffold.of(context).showSnackBar(
|
|
SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
|
|
}
|
|
},
|
|
builder: (context, state) {
|
|
if (state is ExerciseNewReady) {
|
|
//LoadingDialog.hide(context);
|
|
}
|
|
final exerciseBloc = BlocProvider.of<ExerciseNewBloc>(context);
|
|
return getExerciseWidget(exerciseBloc, exerciseType, menuBloc);
|
|
},
|
|
));
|
|
}
|
|
|
|
Widget getExerciseWidget(ExerciseNewBloc exerciseBloc, ExerciseType exerciseType, MenuBloc menuBloc) {
|
|
exerciseBloc.exerciseRepository.setExerciseType(exerciseType);
|
|
final String exerciseName = AppLanguage().appLocal == Locale("en")
|
|
? exerciseBloc.exerciseRepository.exerciseType.name
|
|
: exerciseBloc.exerciseRepository.exerciseType.nameTranslation;
|
|
|
|
String exerciseDescription = AppLanguage().appLocal == Locale("en")
|
|
? exerciseBloc.exerciseRepository.exerciseType.description
|
|
: exerciseBloc.exerciseRepository.exerciseType.descriptionTranslation;
|
|
if (exerciseDescription == null) {
|
|
exerciseDescription = "";
|
|
}
|
|
|
|
//print(exerciseBloc.exerciseRepository.exerciseType.name);
|
|
|
|
if (exerciseBloc.exerciseRepository.exerciseType.name == "BMR") {
|
|
return BMR(exerciseBloc: exerciseBloc);
|
|
}
|
|
if (exerciseBloc.exerciseRepository.exerciseType.name == "BMI") {
|
|
return BMI(exerciseBloc: exerciseBloc);
|
|
}
|
|
if (exerciseBloc.exerciseRepository.exerciseType.name == "Sizes") {
|
|
return SizeWidget(exerciseBloc: exerciseBloc);
|
|
}
|
|
|
|
String exerciseTask = "";
|
|
if (exerciseBloc.exerciseRepository.exerciseType.is1RM && menuBloc.ability.toString() == ExerciseAbility.oneRepMax.toString()) {
|
|
exerciseTask = "Please take a relative bigger weight and repeat 12-20 times";
|
|
exerciseBloc.quantity = 12;
|
|
} else if (exerciseBloc.exerciseRepository.exerciseType.isEndurance &&
|
|
menuBloc.ability.toString() == ExerciseAbility.endurance.toString()) {
|
|
exerciseTask = "Please take a medium weight and repeat 20-30 times";
|
|
exerciseBloc.quantity = 20;
|
|
}
|
|
|
|
return Form(
|
|
child: Scaffold(
|
|
resizeToAvoidBottomInset: true,
|
|
appBar: AppBarNav(depth: 1),
|
|
body: Container(
|
|
width: MediaQuery.of(context).size.width,
|
|
height: MediaQuery.of(context).size.height,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
image: AssetImage('asset/image/WT_black_background.png'),
|
|
fit: BoxFit.fill,
|
|
alignment: Alignment.center,
|
|
),
|
|
),
|
|
child: KeyboardActions(
|
|
config: _buildConfig(context),
|
|
child: Container(
|
|
padding: const EdgeInsets.only(top: 5, left: 75, right: 75),
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.vertical,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text(t('Save Exercise'), style: TextStyle(fontSize: 14, color: Colors.blue[200])),
|
|
Divider(),
|
|
Text(
|
|
exerciseName,
|
|
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.orange),
|
|
overflow: TextOverflow.fade,
|
|
maxLines: 2,
|
|
softWrap: true,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
SizedBox(
|
|
height: 15,
|
|
),
|
|
Text(
|
|
exerciseDescription,
|
|
style: GoogleFonts.inter(fontSize: 12, color: Colors.yellow[300]),
|
|
maxLines: 8,
|
|
overflow: TextOverflow.fade,
|
|
softWrap: true,
|
|
),
|
|
InkWell(
|
|
child: Text(
|
|
t("More »"),
|
|
style: GoogleFonts.inter(fontSize: 12, color: Colors.blue[200]),
|
|
),
|
|
onTap: () => {
|
|
Navigator.of(context).pushNamed('exerciseTypeDescription', arguments: exerciseBloc.exerciseRepository),
|
|
},
|
|
),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
Text(
|
|
t(exerciseTask),
|
|
style: GoogleFonts.inter(
|
|
fontSize: 14,
|
|
color: Colors.orange,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
maxLines: 3,
|
|
textAlign: TextAlign.center,
|
|
overflow: TextOverflow.fade,
|
|
softWrap: true,
|
|
),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
columnQuantityUnit(exerciseBloc),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
columnQuantity(exerciseBloc),
|
|
Divider(),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
FlatButton(
|
|
onPressed: () => {
|
|
confirmationDialog(exerciseBloc),
|
|
},
|
|
child: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Image.asset('asset/icon/gomb_orange_a.png', width: 140, height: 60),
|
|
Text(
|
|
t("Save"),
|
|
style: TextStyle(fontSize: 16, color: Colors.white),
|
|
),
|
|
],
|
|
)),
|
|
]),
|
|
))),
|
|
),
|
|
));
|
|
}
|
|
|
|
Column columnQuantityUnit(ExerciseNewBloc bloc) {
|
|
Column row = Column();
|
|
if (bloc.exerciseRepository.exerciseType != null && bloc.exerciseRepository.exerciseType.unitQuantity == "1") {
|
|
row = Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
|
|
TextFormField(
|
|
focusNode: _nodeText1,
|
|
decoration: InputDecoration(
|
|
contentPadding: EdgeInsets.only(left: 25, top: 5, bottom: 5),
|
|
labelText: t(bloc.exerciseRepository.exerciseType.unitQuantityUnit),
|
|
labelStyle: GoogleFonts.inter(fontSize: 20, color: Colors.yellow[50]),
|
|
fillColor: Colors.black38,
|
|
filled: true,
|
|
border: OutlineInputBorder(
|
|
gapPadding: 8.0,
|
|
borderRadius: BorderRadius.circular(12.0),
|
|
borderSide: BorderSide(color: Colors.white12, width: 0.4),
|
|
),
|
|
),
|
|
initialValue: "30",
|
|
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
|
textInputAction: TextInputAction.done,
|
|
style: GoogleFonts.archivoBlack(fontSize: 80, color: Colors.yellow[300]),
|
|
onChanged: (value) => {bloc.add(ExerciseNewQuantityUnitChange(quantity: double.parse(value)))}),
|
|
//] ),
|
|
]);
|
|
}
|
|
return row;
|
|
}
|
|
|
|
Column columnQuantity(ExerciseNewBloc bloc) {
|
|
Column row = Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
|
|
TextFormField(
|
|
focusNode: _nodeText2,
|
|
decoration: InputDecoration(
|
|
contentPadding: EdgeInsets.only(left: 25, top: 5, bottom: 5),
|
|
labelText: t(bloc.exerciseRepository.exerciseType.unit),
|
|
labelStyle: GoogleFonts.inter(fontSize: 20, color: Colors.orange[50], decorationColor: Colors.black12),
|
|
fillColor: Colors.black38,
|
|
filled: true,
|
|
border: OutlineInputBorder(
|
|
gapPadding: 8.0,
|
|
borderRadius: BorderRadius.circular(12.0),
|
|
borderSide: BorderSide(color: Colors.black26, width: 0.4),
|
|
),
|
|
),
|
|
initialValue: bloc.quantity.toStringAsFixed(0),
|
|
keyboardType: TextInputType.number,
|
|
textInputAction: TextInputAction.next,
|
|
style: GoogleFonts.archivoBlack(fontSize: 80, color: Colors.orange[200]),
|
|
onChanged: (value) => {bloc.add(ExerciseNewQuantityChange(quantity: double.parse(value)))},
|
|
),
|
|
]);
|
|
|
|
return row;
|
|
}
|
|
|
|
void confirmationDialog(ExerciseNewBloc bloc) {
|
|
LinkedHashMap args = LinkedHashMap();
|
|
print("exercise validated " + bloc.exerciseRepository.exercise.quantity.toString());
|
|
if (bloc.exerciseRepository.exercise.quantity == null) {
|
|
return;
|
|
}
|
|
|
|
String quantity = bloc.exerciseRepository.exercise.quantity % 1 == 0
|
|
? bloc.exerciseRepository.exercise.quantity.round().toString()
|
|
: bloc.exerciseRepository.exercise.quantity.toString();
|
|
|
|
String unitQuantity = "";
|
|
if (bloc.exerciseRepository.exercise.unitQuantity != null) {
|
|
unitQuantity = bloc.exerciseRepository.exercise.unitQuantity % 1 == 0
|
|
? bloc.exerciseRepository.exercise.unitQuantity.round().toString()
|
|
: bloc.exerciseRepository.exercise.unitQuantity.toString();
|
|
}
|
|
|
|
showCupertinoDialog(
|
|
useRootNavigator: true,
|
|
context: context,
|
|
//barrierDismissible: false,
|
|
builder: (_) => CupertinoAlertDialog(
|
|
title: Text(t("Do you save this exercise with these parameters?")),
|
|
content: Column(children: [
|
|
Divider(),
|
|
Text(
|
|
t("Exercise") + ": " + bloc.exerciseRepository.exerciseType.name,
|
|
style: (TextStyle(color: Colors.blue)),
|
|
),
|
|
Text(
|
|
quantity + " " + t(bloc.exerciseRepository.exerciseType.unit),
|
|
style: (TextStyle(color: Colors.deepOrange)),
|
|
),
|
|
Text(
|
|
bloc.exerciseRepository.exerciseType.unitQuantity == "1"
|
|
? t("with") + " " + unitQuantity + " " + t(bloc.exerciseRepository.exerciseType.unitQuantityUnit)
|
|
: "",
|
|
style: (TextStyle(color: Colors.deepOrange)),
|
|
),
|
|
]),
|
|
actions: [
|
|
FlatButton(
|
|
child: Text(t("No")),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
FlatButton(
|
|
child: Text(t("Yes")),
|
|
onPressed: () => {
|
|
bloc.exerciseRepository.setCustomer(Cache().userLoggedIn),
|
|
bloc.exerciseRepository.addExercise(),
|
|
Navigator.pop(context),
|
|
Navigator.pop(context),
|
|
if (bloc.exerciseRepository.exerciseType.is1RM)
|
|
{
|
|
args['exerciseRepository'] = bloc.exerciseRepository,
|
|
args['percent'] = 0.75,
|
|
args['readonly'] = false,
|
|
Navigator.of(context).pushNamed('exerciseControlPage', arguments: args)
|
|
}
|
|
else if (bloc.exerciseRepository.exerciseType.isEndurance)
|
|
{
|
|
args['exerciseRepository'] = bloc.exerciseRepository,
|
|
args['percent'] = 0.50,
|
|
args['readonly'] = false,
|
|
Navigator.of(context).pushNamed('exerciseControlPage', arguments: args)
|
|
}
|
|
},
|
|
)
|
|
],
|
|
));
|
|
}
|
|
}
|