280 lines
10 KiB
Dart
280 lines
10 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/exercise_repository.dart';
|
|
import 'package:aitrainer_app/util/trans.dart';
|
|
import 'package:aitrainer_app/widgets/app_bar.dart';
|
|
import 'package:aitrainer_app/widgets/splash.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:aitrainer_app/library/numberpicker.dart';
|
|
|
|
class ExerciseNewPage extends StatefulWidget{
|
|
_ExerciseNewPageState createState() => _ExerciseNewPageState();
|
|
}
|
|
|
|
class _ExerciseNewPageState extends State<ExerciseNewPage> with Trans{
|
|
|
|
@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, exerciseType: exerciseType)..
|
|
add(ExerciseNewLoad()),
|
|
child: BlocConsumer<ExerciseNewBloc, ExerciseNewState>(
|
|
listener: (context, state) {
|
|
if ( state is ExerciseNewLoading ) {
|
|
return LoadingDialog();
|
|
} else if ( state is ExerciseNewError ) {
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
backgroundColor: Colors.orange,
|
|
content:
|
|
Text(state.message, style: TextStyle(color: Colors.white))));
|
|
}
|
|
},
|
|
builder: (context, state) {
|
|
final exerciseBloc = BlocProvider.of<ExerciseNewBloc>(context);
|
|
if ( state is ExerciseNewReady ) {
|
|
return getExerciseWidget(exerciseBloc, exerciseType);
|
|
} else {
|
|
return getExerciseWidget(exerciseBloc, exerciseType);
|
|
}
|
|
},
|
|
)
|
|
);
|
|
}
|
|
|
|
Widget getExerciseWidget(ExerciseNewBloc exerciseBloc, ExerciseType exerciseType) {
|
|
|
|
exerciseBloc.exerciseRepository.setExerciseType(exerciseType);
|
|
String exerciseName = AppLanguage().appLocal == Locale("en") ?
|
|
exerciseBloc.exerciseRepository.exerciseType.name :
|
|
exerciseBloc.exerciseRepository.exerciseType.nameTranslation;
|
|
|
|
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_light_background.png'),
|
|
fit: BoxFit.fill,
|
|
alignment: Alignment.center,
|
|
),
|
|
),
|
|
child: Container(
|
|
padding: const EdgeInsets.only (top: 25, left: 25, right: 25),
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.vertical,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: <Widget>[
|
|
/*Divider(color: Colors.transparent,),
|
|
Divider(color: Colors.transparent,),*/
|
|
Text(t('Save Exercise'),
|
|
style: TextStyle(fontSize: 14, color: Colors.blueAccent)),
|
|
Text(exerciseName,
|
|
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.deepOrange),
|
|
overflow: TextOverflow.fade,
|
|
maxLines: 1,
|
|
softWrap: true,
|
|
),
|
|
|
|
|
|
//Divider(color: Colors.transparent,),
|
|
FlatButton(
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Icon(Icons.description),
|
|
Text(t("Description"),
|
|
style: TextStyle(
|
|
color: Colors.blueAccent, fontWeight: FontWeight.normal, fontSize: 14)),
|
|
Icon(Icons.arrow_forward_ios),
|
|
]),
|
|
textColor: Colors.blueAccent,
|
|
color: Colors.transparent,
|
|
onPressed: () =>
|
|
{
|
|
Navigator.of(context).pushNamed(
|
|
'exerciseTypeDescription', arguments: exerciseBloc.exerciseRepository),
|
|
},
|
|
),
|
|
//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,),
|
|
RaisedButton(
|
|
textColor: Colors.white,
|
|
color: Colors.deepOrange,
|
|
focusColor: Colors.white,
|
|
onPressed: () =>
|
|
{
|
|
confirmationDialog(exerciseBloc),
|
|
},
|
|
child: Text(t("Save"), style: TextStyle(fontSize: 16),)
|
|
),
|
|
]),
|
|
)
|
|
)
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Row columnQuantityUnit( ExerciseNewBloc bloc ) {
|
|
Row row = Row();
|
|
if ( bloc.exerciseRepository.exerciseType != null &&
|
|
bloc.exerciseRepository.exerciseType.unitQuantity == "1") {
|
|
row = Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
NumberPicker.horizontal(
|
|
highlightSelectedValue: true,
|
|
initialValue: bloc.unitQuantity.toInt(),
|
|
minValue: 0,
|
|
maxValue: 200,
|
|
step: 1,
|
|
onChanged: (value) => {
|
|
bloc.add(ExerciseNewQuantityUnitChange(quantity: value.toDouble()))
|
|
},
|
|
listViewHeight: 80,
|
|
textStyle: TextStyle(fontSize: 24),
|
|
textStyleHighlighted: TextStyle(fontSize: 38, color: Colors.orange, fontWeight: FontWeight.bold),
|
|
//decoration: _decoration,
|
|
),
|
|
|
|
new InkWell(
|
|
child: new Text(t(bloc.exerciseRepository.exerciseType.unitQuantityUnit),
|
|
style: TextStyle(fontSize: 16)),
|
|
),
|
|
|
|
]);
|
|
}
|
|
return row;
|
|
}
|
|
|
|
Row columnQuantity( ExerciseNewBloc bloc ) {
|
|
Row row = Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
NumberPicker.horizontal(
|
|
highlightSelectedValue: true,
|
|
initialValue: bloc.quantity.toInt(),
|
|
minValue: 0,
|
|
maxValue: 200,
|
|
step: 1,
|
|
onChanged: (value) => {
|
|
bloc.add(ExerciseNewQuantityChange(quantity: value.toDouble()))
|
|
},
|
|
listViewHeight: 80,
|
|
textStyle: TextStyle(fontSize: 24),
|
|
textStyleHighlighted: TextStyle(fontSize: 38, color: Colors.orange, fontWeight: FontWeight.bold),
|
|
//decoration: _decoration,
|
|
),
|
|
|
|
Text(t(bloc.exerciseRepository.exerciseType.unit),
|
|
style: TextStyle(fontSize: 16)),
|
|
]);
|
|
|
|
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 )
|
|
}
|
|
},
|
|
)
|
|
],
|
|
)
|
|
);
|
|
}
|
|
}
|