You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/ui/leave_balance/leave_balance_screen.dart

261 lines
10 KiB
Dart

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/dashboard_api_client.dart';
import 'package:mohem_flutter_app/api/leave_balance_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/date_uitl.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/dashboard/get_accrual_balances_list_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/get_absence_transaction_list_model.dart';
import 'package:mohem_flutter_app/provider/dashboard_provider_model.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';
import 'package:pie_chart/pie_chart.dart';
import 'package:provider/provider.dart';
class LeaveBalance extends StatefulWidget {
LeaveBalance({Key? key}) : super(key: key);
@override
_LeaveBalanceState createState() {
return _LeaveBalanceState();
}
}
class _LeaveBalanceState extends State<LeaveBalance> {
List<GetAbsenceTransactionList>? absenceTransList;
List<PieChartModel> chartModelList = [];
GetAccrualBalancesList? leaveBalanceAccrual;
DateTime accrualDateTime = DateTime.now();
@override
void initState() {
super.initState();
getAbsenceTransactions();
}
@override
void dispose() {
super.dispose();
}
void getAbsenceTransactions() async {
try {
Utils.showLoading(context);
absenceTransList = await LeaveBalanceApiClient().getAbsenceTransactions(-999);
Utils.hideLoading(context);
setState(() {});
} catch (ex) {
Utils.hideLoading(context);
Utils.handleException(ex, context, null);
}
}
void changeAccrualDate() async {
try {
Utils.showLoading(context);
List<GetAccrualBalancesList> accrualList = await DashboardApiClient().getAccrualBalances(DateFormat("MM/dd/yyyy").format(accrualDateTime));
if (accrualList.isNotEmpty) {
leaveBalanceAccrual = accrualList[0];
chartModelList = [
PieChartModel("Current Balance", leaveBalanceAccrual?.accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
PieChartModel("Used", leaveBalanceAccrual?.accrualUsedEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
];
}
Utils.hideLoading(context);
setState(() {});
} catch (ex) {
Utils.hideLoading(context);
Utils.handleException(ex, context, null);
}
}
@override
Widget build(BuildContext context) {
if (leaveBalanceAccrual == null) {
leaveBalanceAccrual = Provider.of<DashboardProviderModel>(context, listen: false).leaveBalanceAccrual;
chartModelList = [
PieChartModel("Current Balance", leaveBalanceAccrual?.accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
PieChartModel("Used", leaveBalanceAccrual?.accrualUsedEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
];
}
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBarWidget(
context,
title: LocaleKeys.leaveBalance.tr(),
),
body: ListView(
physics: const BouncingScrollPhysics(),
padding: const EdgeInsets.all(21),
children: [
Column(
children: [
Row(
children: [
"Current Leave Balance".toText20().expanded,
Row(
children: [
const Icon(Icons.calendar_month_rounded, color: MyColors.darkIconColor, size: 16),
5.width,
DateUtil.formatDateToDate(accrualDateTime, AppState().isArabic(context)).toText13(isUnderLine: true),
8.width
],
).onPress(() async {
DateTime selectedDate = await Utils.selectDate(context, accrualDateTime);
if (selectedDate != accrualDateTime) {
accrualDateTime = selectedDate;
changeAccrualDate();
}
}),
],
),
8.height,
AspectRatio(
aspectRatio: 302 / 122,
child: Row(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
chartLegend(chartModelList[0], isFirst: true),
chartLegend(chartModelList[1]),
],
).paddingOnly(top: 8, bottom: 8).expanded,
getChart()
],
),
)
],
).paddingOnly(top: 19, bottom: 11, right: 6, left: 14).objectContainerView(disablePadding: true, radius: 10),
12.height,
absenceTransList == null
? const SizedBox()
: (absenceTransList!.isEmpty
? Utils.getNoDataWidget(context).paddingOnly(top: 50)
: ListView.separated(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.zero,
itemBuilder: (cxt, int index) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
ItemDetailView(LocaleKeys.startDateT.tr(), absenceTransList![index].sTARTDATE ?? ""),
ItemDetailView(LocaleKeys.endDateT.tr(), absenceTransList![index].eNDDATE ?? ""),
ItemDetailView(LocaleKeys.absenceType.tr(), absenceTransList![index].aBSENCETYPE ?? ""),
ItemDetailView(LocaleKeys.absenceCategory.tr(), absenceTransList![index].aBSENCECATEGORY ?? ""),
ItemDetailView(LocaleKeys.days.tr(), absenceTransList![index].aBSENCEDAYS?.toString() ?? ""),
ItemDetailView(LocaleKeys.hours.tr(), absenceTransList![index].aBSENCEHOURS?.toString() ?? ""),
ItemDetailView(LocaleKeys.approvalStatus.tr(), absenceTransList![index].aPPROVALSTATUS ?? ""),
ItemDetailView(LocaleKeys.absenceStatus.tr(), absenceTransList![index].aBSENCESTATUS ?? ""),
],
).objectContainerView(),
separatorBuilder: (cxt, index) => 12.height,
itemCount: absenceTransList!.length)),
],
),
floatingActionButton: Container(
height: 54,
width: 54,
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(transform: GradientRotation(.83), begin: Alignment.topRight, end: Alignment.bottomLeft, colors: [
MyColors.gradiantEndColor,
MyColors.gradiantStartColor,
]),
),
child: const Icon(Icons.add, color: Colors.white, size: 30),
).onPress(() {
Navigator.pushNamed(context, AppRoutes.addLeaveBalance);
}),
);
}
Widget getChart() {
List<Color> _colorList = chartModelList.map((e) => e.color).toList();
return PieChart(
dataMap: {for (var e in chartModelList) e.title: e.parsedValue},
animationDuration: const Duration(milliseconds: 800),
chartRadius: MediaQuery.of(context).size.width / 3.2,
colorList: _colorList,
emptyColor: MyColors.lightGreyEAColor,
initialAngleInDegree: 270,
legendOptions: const LegendOptions(
showLegendsInRow: false,
showLegends: false,
),
chartValuesOptions: const ChartValuesOptions(
showChartValueBackground: false,
showChartValues: true,
showChartValuesInPercentage: true,
showChartValuesOutside: false,
decimalPlaces: 0,
chartValueStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 10,
letterSpacing: -0.64,
color: MyColors.white,
),
),
);
}
Widget chartLegend(PieChartModel chartModel, {bool isFirst = false}) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 12,
height: 12,
decoration: BoxDecoration(color: chartModel.color, shape: BoxShape.circle),
).paddingOnly(top: 2),
9.width,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
RichText(
text: TextSpan(
text: chartModel.title,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: isFirst ? MyColors.textMixColor : MyColors.backgroundBlackColor),
children: [
TextSpan(
text: " " + chartModel.titleAppend,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: isFirst ? MyColors.textMixColor : MyColors.backgroundBlackColor),
),
],
),
),
chartModel.value.toString().toText14(),
],
).expanded,
],
);
}
}
class PieChartModel {
String title;
String titleAppend;
double value;
Color color;
PieChartModel(this.title, this.value, this.color, {this.titleAppend = ""});
Map<String, double> get chartMapValue => {title: value};
double get parsedValue => value > 0 ? value : value + 0.1;
}