workouttest_app/lib/view/exercise_plan_custom_page.dart
2020-09-16 15:41:39 +02:00

191 lines
7.1 KiB
Dart

import 'dart:collection';
import 'package:aitrainer_app/bloc/exercise_plan/exercise_plan_bloc.dart';
import 'package:aitrainer_app/localization/app_language.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/workout_tree.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/widgets/app_bar_common.dart';
import 'package:aitrainer_app/widgets/splash.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_treeview/tree_view.dart';
class ExercisePlanCustomPage extends StatefulWidget {
@override
_ExercisePlanCustomPage createState() => _ExercisePlanCustomPage();
}
class _ExercisePlanCustomPage extends State<ExercisePlanCustomPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
// ignore: close_sinks
ExercisePlanBloc bloc;
@override
void initState() {
super.initState();
/// We require the initializers to run after the loading screen is rendered
SchedulerBinding.instance.addPostFrameCallback((_) {
BlocProvider.of<ExercisePlanBloc>(context).add(ExercisePlanLoad());
});
}
@override
Widget build(BuildContext context) {
LinkedHashMap arguments = ModalRoute.of(context).settings.arguments;
final ExerciseRepository exerciseRepository = arguments['exerciseRepository'];
final int customerId = arguments['customerId'];
bloc = BlocProvider.of<ExercisePlanBloc>(context);
bloc.customerId = customerId;
return Scaffold(
key: _scaffoldKey,
appBar: AppBarCommonNav(),
body: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
image: DecorationImage(
image: customerId == Cache().userLoggedIn.customerId ? AssetImage('asset/image/WT_light_background.png'):
AssetImage('asset/image/WT_menu_dark.png'),
fit: BoxFit.cover,
alignment: Alignment.center,
),
),
child: BlocConsumer<ExercisePlanBloc, ExercisePlanState>(listener: (context, state) {
if (state is ExercisePlanError) {
//showInSnackBar(state.message);
//return exerciseWidget(bloc);
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(
state.message,
),
backgroundColor: Colors.orange,
));
} else if (state is ExercisePlanLoading) {
LoadingDialog();
}
},
// ignore: missing_return
builder: (context, state) {
if (state is ExercisePlanInitial) {
return Container();
} else if (state is ExercisePlanReady) {
return exerciseWidget(bloc);
} else if (state is ExercisePlanError) {
return exerciseWidget(bloc);
} else if (state is ExercisePlanLoading) {
return Container();
}
})));
}
Widget exerciseWidget(ExercisePlanBloc bloc) {
TreeViewController _treeViewController = TreeViewController(children: nodeExercisePlan(bloc));
TreeViewTheme _treeViewTheme = TreeViewTheme(
expanderTheme: ExpanderThemeData(
type: ExpanderType.plusMinus,
modifier: ExpanderModifier.circleOutlined,
position: ExpanderPosition.start,
color: Colors.black26,
size: 10,
),
labelStyle: TextStyle(fontSize: 14, letterSpacing: 0, color: Colors.blue.shade800),
parentLabelStyle: TextStyle(
fontSize: 14,
letterSpacing: 0.3,
fontWeight: FontWeight.w800,
color: Colors.orange.shade600,
),
iconTheme: IconThemeData(
size: 20,
color: Colors.blue.shade800,
),
colorScheme: bloc.customerId == Cache().userLoggedIn.customerId ? ColorScheme.light(background: Colors.transparent) : ColorScheme.dark(background: Colors.transparent),
);
return Scaffold(
backgroundColor: Colors.transparent,
body: TreeView(
controller: _treeViewController,
allowParentSelect: true,
supportParentDoubleTap: false,
//onExpansionChanged: _expandNodeHandler,
onNodeTap: (key) {
Node<dynamic> node = _treeViewController.getNode(key);
WorkoutTree workoutTree = node.data as WorkoutTree;
bloc.exercisePlanRepository.setActualPlanDetail(workoutTree.exerciseType);
print("change node " + node.label + " key " + key);
bloc.add(ExercisePlanUpdate(workoutTree: workoutTree));
Navigator.of(context).pushNamed("exercisePlanDetailAdd", arguments: bloc);
},
theme: _treeViewTheme,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.blueAccent,
child: Icon(Icons.save_alt),
onPressed: () => {
bloc.add(ExercisePlanSave()),
if (bloc.exercisePlanRepository.getExercisePlanDetailSize() > 0) {
Navigator.of(context).pop()
}
}
),
//bottomNavigationBar: BottomNavigator(bottomNavIndex: 2),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
);
}
List<Node> nodeExercisePlan(ExercisePlanBloc bloc) {
List<Node> nodes = List<Node>();
Node actualNode;
bool isEnglish = AppLanguage().appLocal == Locale("en");
bloc.menuTreeRepository.sortedTree.forEach((name, list) {
List<WorkoutTree> listWorkoutItem = list;
List<Node> listExerciseTypePerMuscle = List<Node>();
NodeIcon icon;
listWorkoutItem.forEach((element) {
WorkoutTree treeItem = element;
icon =
treeItem.selected == true ? NodeIcon(codePoint: Icons.bubble_chart.codePoint, color: "blueAccent") : null;
String exerciseLabel = isEnglish
? treeItem.name
: treeItem.exerciseType == null ? treeItem.name : treeItem.exerciseType.nameTranslation;
List<Node<dynamic>> planDetailList = List<Node<dynamic>>();
String planDetail = bloc.exercisePlanRepository.getPlanDetail(treeItem.exerciseTypeId);
if (planDetail.length > 0) {
exerciseLabel += " (" + planDetail +")";
}
actualNode = Node(
label: exerciseLabel,
key: treeItem.id.toString(),
data: treeItem,
expanded: planDetailList.length > 0 ? true : false,
children: [],
icon: icon);
listExerciseTypePerMuscle.add(actualNode);
});
//print ("Node name " + name);
if (name != null) {
actualNode = Node(
label: name, // AppLocalizations.of(context).translate(name),
key: name,
expanded: true,
children: listExerciseTypePerMuscle,
icon: NodeIcon(codePoint: Icons.perm_identity.codePoint, color: "orange"));
nodes.add(actualNode);
}
});
return nodes;
}
}