workouttest_app/lib/widgets/app_bar.dart
2020-10-29 15:21:24 +01:00

158 lines
4.8 KiB
Dart

import 'dart:async';
import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
import 'package:aitrainer_app/localization/app_localization.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import 'package:rainbow_color/rainbow_color.dart';
class AppBarNav extends StatefulWidget implements PreferredSizeWidget {
final MenuBloc menuBloc;
final bool isMenu;
final int depth;
const AppBarNav({this.menuBloc, this.isMenu, this.depth});
@override
_AppBarNav createState() => _AppBarNav();
@override
Size get preferredSize => const Size.fromHeight(50);
}
class _AppBarNav extends State<AppBarNav> with SingleTickerProviderStateMixin, Common {
Animation<Color> colorAnim;
AnimationController colorController;
MenuBloc menuBloc;
@override
void initState() {
colorController =
AnimationController(duration: Duration(seconds: 4), vsync:this);
colorAnim = RainbowColorTween([Colors.white70,
Colors.blueGrey,
Colors.blueAccent,
Colors.lightBlue,
Colors.lightBlueAccent,
Colors.yellowAccent,
Colors.orange,
Colors.orangeAccent,
Colors.yellowAccent,
Color(0xffcce6ff)])
.animate(colorController)
..addListener(() { setState(() {}); })
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
Timer(Duration(seconds: 10), () {
if ( mounted ) {
colorController.forward();
}
});
} else if (status == AnimationStatus.dismissed) {
colorController.forward();
}
});
colorController.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
menuBloc = BlocProvider.of<MenuBloc>(context);
return AppBar(
backgroundColor: Colors.black,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
getAnimatedWidget(),
Image.asset(
'asset/image/WT_long_logo.png',
//fit: BoxFit.cover,
height: 45.0,
),
],
),
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () =>
{
if ( widget.isMenu != null ) {
if ( menuBloc.workoutItem != null ) {
menuBloc.add(MenuTreeUp(parent: menuBloc.workoutItem.parent)),
}
} else if ( widget.depth != null ) {
if ( widget.depth == 0 ) {
Navigator.of(context).pushNamed('home')
} else {
Navigator.of(context).pop()
}
}
},
)
);
}
@override
void dispose() {
colorController.dispose();
super.dispose();
}
Widget getAnimatedWidget() {
double cWidth = mediaSizeWidth(context);
double percent = Cache().getPercentExercises();
if ( percent == -1) {
menuBloc.menuTreeRepository.createTree();
ExerciseRepository exerciseRepository = ExerciseRepository();
exerciseRepository.getBaseExerciseFinishedPercent();
percent = Cache().getPercentExercises();
}
int sizeExerciseList = Cache().getExercises() == null? 0 : Cache().getExercises().length;
if ( sizeExerciseList == 0 ) {
String text = AppLocalizations.of(context).translate("Make your first test");
double fontSize = text.length > 24 ? 10 : 16;
return Stack(
alignment: Alignment.topLeft,
overflow: Overflow.clip,
children: [
Text(text,
style: TextStyle(fontSize: fontSize, color: colorAnim.value, shadows: [Shadow(color: Colors.purple , blurRadius: 15)]),
),
//TestProgress(animation: sizeAnim),
]
);
} else {
return Stack(
alignment: Alignment.topLeft,
children: [
LinearPercentIndicator(
width: cWidth / 4,
lineHeight: 14.0,
percent: percent,
center: Text(
(percent * 100).toStringAsFixed(0) + "% " + AppLocalizations.of(context).translate("finished"),
style: new TextStyle(fontSize: 10.0),
),
trailing: Icon(percent > 0.6 ? Icons.mood : Icons.mood_bad, color: colorAnim.value,),
linearStrokeCap: LinearStrokeCap.roundAll,
backgroundColor: colorAnim.value,
progressColor: Color(0xff73e600),
animation: true,
),
],
);
}
}
}