Untitled
Anonymous
Plain Text
10/27/2025 9:10 AM
64.9 KB
13
Indexable
Never
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:johntour_driver/common/common.dart';
import 'package:johntour_driver/core/model/user_detail_model.dart';
import 'package:johntour_driver/core/utils/custom_loader.dart';
import 'package:johntour_driver/core/utils/custom_snack_bar.dart';
import 'package:johntour_driver/core/utils/custom_text.dart';
import 'package:johntour_driver/core/utils/functions.dart';
import 'package:johntour_driver/features/account/application/acc_bloc.dart';
import 'package:johntour_driver/features/auth/presentation/pages/auth_page.dart';
import 'package:johntour_driver/l10n/app_localizations.dart';
class EarningsPage extends StatefulWidget {
static const String routeName = '/earningsPage';
final EarningArguments? args;
const EarningsPage({super.key, this.args});
@override
State<EarningsPage> createState() => _EarningsPageState();
}
List dragValue = [];
class _EarningsPageState extends State<EarningsPage> {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return BlocProvider(
create: (context) => AccBloc()..add(GetEarningsEvent()),
child:
BlocListener<AccBloc, AccState>(listener: (context, state) async {
if (state is EarningsLoadingStartState) {
CustomLoader.loader(context);
}
if (state is EarningsLoadingStopState) {
CustomLoader.dismiss(context);
}
if (state is UserUnauthenticatedState) {
final type = await AppSharedPreference.getUserType();
if (!context.mounted) return;
Navigator.pushNamedAndRemoveUntil(
context, AuthPage.routeName, (route) => false,
arguments: AuthPageArguments(type: type));
}
if (state is ShowErrorState) {
return showToast(message: state.message);
}
}, child: BlocBuilder<AccBloc, AccState>(builder: (context, state) {
return Scaffold(
backgroundColor: AppColors.primary.withOpacity(0.95),
body: Stack(
children: [
Container(
height: size.height,
width: size.width,
padding: EdgeInsets.fromLTRB(
size.width * 0.075,
MediaQuery.of(context).padding.top,
size.width * 0.075,
0),
child: Column(
children: [
SizedBox(
height: size.width * 0.03,
),
if (widget.args != null &&
widget.args!.from! == 'dashboard')
Column(
children: [
SizedBox(
width: size.width * 0.85,
child: Row(
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back,
size: size.width * 0.07,
color: AppColors.white,
))
],
),
),
SizedBox(
height: size.width * 0.02,
),
],
),
SizedBox(
width: size.width * 0.85,
child: MyText(
text: AppLocalizations.of(context)!.weeklyEarnings,
textStyle: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(
fontSize: size.width * 0.038,
color: AppColors.white.withOpacity(0.9)),
textAlign: TextAlign.center,
),
),
SizedBox(
height: size.width * 0.01,
),
SizedBox(
width: size.width * 0.85,
child: MyText(
text: (context
.read<AccBloc>()
.earningsList
.isNotEmpty)
? formatVND(
context
.read<AccBloc>()
.earningsList[context
.read<AccBloc>()
.choosenEarningsWeeks!]
.totalAmount,
symbol: context
.read<AccBloc>()
.earningCurrency ??
'₫')
: '',
textStyle: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(
fontSize: size.width * 0.0769,
color: AppColors.white),
textAlign: TextAlign.center,
),
),
SizedBox(
height: size.width * 0.015,
),
SizedBox(
width: size.width * 0.85,
child: _buildEarningsStats(context, size),
),
SizedBox(
height: size.width * 0.035,
),
if (context.read<AccBloc>().earningsList.isNotEmpty)
GestureDetector(
onHorizontalDragStart: (v) {
dragValue.clear();
},
onHorizontalDragUpdate: (v) {
dragValue.add(v.globalPosition.dx);
},
onHorizontalDragEnd: (v) {
if (dragValue[dragValue.length - 1] >
dragValue[dragValue.length - 2]) {
if (context
.read<AccBloc>()
.earningsList
.length -
1 >
context
.read<AccBloc>()
.choosenEarningsWeeks!) {
context.read<AccBloc>().add(
ChangeEarningsWeekEvent(
week: context
.read<AccBloc>()
.choosenEarningsWeeks! +
1));
}
}
if (dragValue[dragValue.length - 1] <
dragValue[dragValue.length - 2]) {
if (context
.read<AccBloc>()
.choosenEarningsWeeks! !=
0) {
context.read<AccBloc>().add(
ChangeEarningsWeekEvent(
week: context
.read<AccBloc>()
.choosenEarningsWeeks! -
1));
}
}
},
child: SizedBox(
width: size.width * 0.85,
height: size.width * 0.32,
child: PageView(
reverse: true,
controller:
context.read<AccBloc>().earningsController,
onPageChanged: (v) {
final accBloc = context.read<AccBloc>();
final earningsList = accBloc.earningsList;
context.read<AccBloc>().choosenEarningsWeeks =
v;
accBloc.add(ChangeEarningsWeekEvent(week: v));
final selectedWeekData = earningsList[v];
final firstDateKey =
selectedWeekData.dates.keys.first;
if (v == earningsList.length - 1) {
context.read<AccBloc>().add(
GetDailyEarningsEvent(
date: selectedWeekData.fromDate
.toString()
.replaceFirst(
selectedWeekData.fromDate
.toString()
.split('-')[0],
firstDateKey
.toString()
.split('-')[1],
),
),
);
} else {
context.read<AccBloc>().add(
GetDailyEarningsEvent(date: 'today'),
);
}
},
scrollDirection: Axis.horizontal,
children: context
.read<AccBloc>()
.earningsList
.asMap()
.map((k, v) {
return MapEntry(
k,
Container(
width: size.width * 0.85,
padding: EdgeInsets.all(
size.width * 0.04),
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(16),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Theme.of(context)
.scaffoldBackgroundColor,
Theme.of(context)
.scaffoldBackgroundColor
.withOpacity(0.8),
],
),
boxShadow: [
BoxShadow(
color: Colors.black
.withOpacity(0.08),
blurRadius: 12,
offset: Offset(0, 4),
),
]),
child: Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: context
.read<AccBloc>()
.earningsList[context
.read<AccBloc>()
.choosenEarningsWeeks!]
.dates
.map((k, v) {
bool display = false;
if (int.parse(context
.read<AccBloc>()
.earningsList[context
.read<
AccBloc>()
.choosenEarningsWeeks!]
.toDate
.toString()
.split(
'-')[0]) <
int.parse(k
.toString()
.split(
'-')[1])) {
if (DateTime.now()
.difference(DateFormat('dd-MMM-yy').parse(context
.read<
AccBloc>()
.earningsList[context
.read<
AccBloc>()
.choosenEarningsWeeks!]
.fromDate
.toString()
.replaceFirst(
context
.read<AccBloc>()
.earningsList[context.read<AccBloc>().choosenEarningsWeeks!]
.fromDate
.toString()
.split('-')[0],
k.toString().split('-')[1])))
.inHours >=
0) {
display = true;
}
} else {
if (DateTime.now()
.difference(DateFormat('dd-MMM-yy').parse(context
.read<
AccBloc>()
.earningsList[context
.read<
AccBloc>()
.choosenEarningsWeeks!]
.toDate
.toString()
.replaceFirst(
context
.read<AccBloc>()
.earningsList[context.read<AccBloc>().choosenEarningsWeeks!]
.toDate
.toString()
.split('-')[0],
k.toString().split('-')[1])))
.inHours >=
0) {
display = true;
}
}
return MapEntry(
k,
Column(
children: [
MyText(
text: k
.toString()
.split(
'-')[0],
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize: size.width *
0.035,
color:
Theme.of(context).primaryColorDark),
),
SizedBox(
height:
size.width *
0.015,
),
Material(
color: Colors
.transparent,
borderRadius:
BorderRadius.circular(
size.width *
0.045),
child:
InkWell(
borderRadius:
BorderRadius.circular(size.width *
0.045),
onTap: (display)
? () {
if (display) {
if (int.parse(context.read<AccBloc>().earningsList[context.read<AccBloc>().choosenEarningsWeeks!].toDate.toString().split('-')[0]) < int.parse(k.toString().split('-')[1])) {
context.read<AccBloc>().add(GetDailyEarningsEvent(date: context.read<AccBloc>().earningsList[context.read<AccBloc>().choosenEarningsWeeks!].fromDate.toString().replaceFirst(context.read<AccBloc>().earningsList[context.read<AccBloc>().choosenEarningsWeeks!].fromDate.toString().split('-')[0], k.toString().split('-')[1])));
} else {
context.read<AccBloc>().add(GetDailyEarningsEvent(date: context.read<AccBloc>().earningsList[context.read<AccBloc>().choosenEarningsWeeks!].toDate.toString().replaceFirst(context.read<AccBloc>().earningsList[context.read<AccBloc>().choosenEarningsWeeks!].toDate.toString().split('-')[0], k.toString().split('-')[1])));
}
}
}
: null,
child:
Container(
height: size
.width *
0.085,
width: size
.width *
0.085,
decoration: BoxDecoration(
shape: BoxShape
.circle,
border: Border.all(
color: (display) ? Theme.of(context).primaryColorDark.withOpacity(0.4) : Colors.transparent,
width: 1.5),
color: (context.read<AccBloc>().choosenEarningsDate == k) ? Theme.of(context).primaryColor : Colors.transparent),
alignment:
Alignment
.center,
child:
MyText(
text: k
.toString()
.split('-')[1],
textStyle: Theme.of(context).textTheme.bodySmall!.copyWith(
fontSize: size.width * 0.032,
fontWeight: (context.read<AccBloc>().choosenEarningsDate == k) ? FontWeight.w600 : FontWeight.normal,
color: (display)
? (context.read<AccBloc>().choosenEarningsDate != k)
? Theme.of(context).primaryColorDark
: Theme.of(context).scaffoldBackgroundColor
: Theme.of(context).disabledColor),
),
),
),
)
],
));
})
.values
.toList(),
),
if (context
.read<AccBloc>()
.earningsList
.length >
1)
SizedBox(
height: size.width * 0.02,
),
],
),
));
})
.values
.toList(),
),
),
),
],
),
),
if (context.read<AccBloc>().earningsList.isNotEmpty)
Positioned(
bottom: 0,
child: Container(
height: _calculateContainerHeight(size),
width: size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(size.width * 0.12),
topRight: Radius.circular(size.width * 0.12),
),
color: Theme.of(context).scaffoldBackgroundColor),
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: size.width * 0.12,
),
SizedBox(
height: size.width * 0.45,
width: size.width * 0.8,
// color: Colors.red,
child: Column(
children: [
Expanded(
child: SizedBox(
width: size.width * 0.7,
child: Row(
crossAxisAlignment:
CrossAxisAlignment.end,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: context
.read<AccBloc>()
.earningsList[context
.read<AccBloc>()
.choosenEarningsWeeks!]
.dates
.map((k, v) {
dynamic higher = 0;
context
.read<AccBloc>()
.earningsList[context
.read<AccBloc>()
.choosenEarningsWeeks!]
.dates
.forEach((k, v) {
if (v > higher) {
higher = v;
}
});
return MapEntry(
k,
Container(
width: size.width * 0.08,
height: context
.read<AccBloc>()
.earningsList[context
.read<
AccBloc>()
.choosenEarningsWeeks!]
.dates[k] >
0
? size.width *
0.4 *
(v / higher)
: 1,
color: Theme.of(context)
.primaryColor
.withOpacity(0.3),
),
);
})
.values
.toList(),
),
)),
SizedBox(
height: size.width * 0.015,
),
Container(
width: size.width * 0.7,
height: 1,
color: AppColors.black,
),
SizedBox(
height: size.width * 0.015,
),
SizedBox(
width: size.width * 0.75,
child: Row(
children: context
.read<AccBloc>()
.earningsList[context
.read<AccBloc>()
.choosenEarningsWeeks!]
.dates
.map((k, v) {
return MapEntry(
k,
SizedBox(
width: size.width * 0.1,
child: MyText(
text: k.toString()[0],
textAlign:
TextAlign.center,
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
size.width *
0.035,
color: Theme.of(
context)
.disabledColor),
)),
);
})
.values
.toList(),
),
)
],
),
),
if (context.read<AccBloc>().dailyEarningsList !=
null)
Column(
children: [
SizedBox(
height: size.width * 0.05,
),
SizedBox(
width: size.width * 0.9,
child: userData!.role != 'owner'
? Row(
children: [
MyText(
text:
'${AppLocalizations.of(context)!.login} : ',
textStyle: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontSize: size.width *
0.035,
// color: AppColors.darkGrey
),
),
Expanded(
child: MyText(
text: context
.read<AccBloc>()
.dailyEarningsList!
.totalHoursWorked
.replaceAll(
"Mins",
AppLocalizations.of(
context)!
.mins)
.replaceAll(
"Hr",
AppLocalizations.of(
context)!
.hours),
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
size.width *
0.035,
color: AppColors
.red),
),
)
],
)
: Row(
children: [
MyText(
text:
'${AppLocalizations.of(context)!.ridesTaken} : ',
textStyle: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontSize: size.width *
0.035,
),
),
Expanded(
child: MyText(
text: context
.read<AccBloc>()
.dailyEarningsList!
.totalTrips
.toString(),
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
size.width *
0.035,
color: AppColors
.red),
),
)
],
),
),
SizedBox(
height: size.width * 0.02,
),
SizedBox(
width: size.width * 0.9,
child: Row(
children: [
MyText(
text:
'${AppLocalizations.of(context)!.totalDistance} : ',
textStyle: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
size.width * 0.032,
color: Theme.of(context)
.primaryColorDark
.withOpacity(0.7),
),
),
Expanded(
child: MyText(
text:
'${context.read<AccBloc>().dailyEarningsList!.totalTripsKm.toStringAsFixed(2)} ${userData!.distanceUnit}',
textStyle: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
size.width * 0.035,
fontWeight:
FontWeight.w600,
color: AppColors.red),
),
)
],
),
),
SizedBox(
height: size.width * 0.05,
),
Container(
width: size.width * 0.9,
height: 1,
color: Theme.of(context).disabledColor,
),
SizedBox(
height: size.width * 0.05,
),
SizedBox(
width: size.width * 0.9,
child: MyText(
text: AppLocalizations.of(context)!
.summary,
textStyle: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontSize: 16,
color: Theme.of(context)
.disabledColor),
),
),
SizedBox(
height: size.width * 0.05,
),
(context
.read<AccBloc>()
.dailyEarningsList!
.data
.isNotEmpty)
? Column(children: [
for (var i = 0;
i <
context
.read<AccBloc>()
.dailyEarningsList!
.data
.length;
i++)
Container(
margin: EdgeInsets.only(
bottom:
size.width * 0.025),
width: size.width * 0.9,
padding: EdgeInsets.all(
size.width * 0.04),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end:
Alignment.bottomRight,
colors: [
Theme.of(context)
.primaryColor
.withOpacity(0.12),
Theme.of(context)
.primaryColor
.withOpacity(0.08),
],
),
borderRadius:
BorderRadius.circular(
12),
boxShadow: [
BoxShadow(
color: Colors.black
.withOpacity(0.06),
blurRadius: 8,
offset: Offset(0, 2),
),
],
border: Border.all(
color: Theme.of(context)
.primaryColor
.withOpacity(0.1),
width: 0.5,
),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment
.start,
children: [
MyText(
text: AppLocalizations
.of(context)!
.trips,
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
16,
color: Theme.of(
context)
.primaryColorDark),
),
MyText(
text: context
.read<
AccBloc>()
.dailyEarningsList!
.data[i]
.tripTime,
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize:
12,
color: Theme.of(
context)
.primaryColorDark),
),
],
),
),
SizedBox(
width: size.width *
0.02),
Icon(
Icons.add,
color: Colors.green
.withOpacity(0.7),
size: 14,
),
SizedBox(
width: size.width *
0.02),
MyText(
text: formatVND(
context
.read<AccBloc>()
.dailyEarningsList!
.data[i]
.tripCommission,
symbol: context
.read<AccBloc>()
.dailyEarningsList!
.currency),
textStyle: Theme.of(
context)
.textTheme
.bodySmall!
.copyWith(
fontSize: 15,
fontWeight:
FontWeight
.w600,
color: Theme.of(
context)
.primaryColor),
),
],
),
)
])
: Column(
children: [
Image.asset(
AppImages.earningsNoData,
width: size.width * 0.5,
height: size.width * 0.5,
fit: BoxFit.contain,
),
const SizedBox(height: 8),
MyText(
text: AppLocalizations.of(
context)!
.noSummaryText),
const SizedBox(height: 15),
],
)
],
),
SizedBox(
height: size.width * 0.25,
)
],
),
),
)),
// if (context.read<AccBloc>().earningsList.isNotEmpty)
// Positioned(
// top: ((widget.args != null &&
// widget.args!.from! == 'dashboard'))
// ? size.width * 0.9
// : size.width * 0.62,
// left: size.width * 0.075,
// child: Container(
// width: size.width * 0.85,
// decoration: BoxDecoration(
// boxShadow: [
// BoxShadow(
// offset: const Offset(0, 2),
// spreadRadius: 0,
// blurRadius: 8,
// color: Colors.black.withOpacity(0.08),
// )
// ],
// borderRadius: BorderRadius.circular(12),
// color: Theme.of(context).scaffoldBackgroundColor,
// ),
// child: Padding(
// padding: EdgeInsets.symmetric(
// horizontal: size.width * 0.04,
// vertical: size.width * 0.018,
// ),
// child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// children: [
// Expanded(
// child: _buildSummaryCard(
// context: context,
// icon: Icons.directions_car_rounded,
// value: context
// .read<AccBloc>()
// .earningsList[context
// .read<AccBloc>()
// .choosenEarningsWeeks!]
// .totalTrips
// .toString(),
// label: AppLocalizations.of(context)!.trips,
// size: size,
// ),
// ),
// Expanded(
// child: _buildSummaryCard(
// context: context,
// icon: Icons.account_balance_wallet_rounded,
// value: context
// .read<AccBloc>()
// .earningsList[context
// .read<AccBloc>()
// .choosenEarningsWeeks!]
// .totalWalletAmount
// .toString(),
// label: 'Wallet',
// size: size,
// ),
// ),
// Expanded(
// child: _buildSummaryCard(
// context: context,
// icon: Icons.payments_rounded,
// value: formatVND(
// context
// .read<AccBloc>()
// .earningsList[context
// .read<AccBloc>()
// .choosenEarningsWeeks!]
// .totalCashAmount,
// symbol: context
// .read<AccBloc>()
// .earningCurrency ??
// '₫',
// ),
// label: AppLocalizations.of(context)!.cash,
// size: size,
// ),
// )
// ],
// ),
// ),
// ))
],
));
})));
}
// Widget _buildSummaryCard({
// required BuildContext context,
// required IconData icon,
// required String value,
// required String label,
// required Size size,
// }) {
// return Container(
// padding: EdgeInsets.symmetric(
// horizontal: size.width * 0.02,
// vertical: size.width * 0.015,
// ),
// decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(10),
// color: Theme.of(context).primaryColor.withOpacity(0.05),
// ),
// child: Column(
// mainAxisSize: MainAxisSize.min,
// children: [
// Icon(
// icon,
// size: size.width * 0.045,
// color: Theme.of(context).primaryColor.withOpacity(0.8),
// ),
// SizedBox(height: size.width * 0.008),
// MyText(
// text: value,
// textStyle: Theme.of(context).textTheme.bodySmall!.copyWith(
// fontSize: size.width * 0.032,
// fontWeight: FontWeight.w600,
// color: Theme.of(context).primaryColorDark,
// ),
// textAlign: TextAlign.center,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// ),
// SizedBox(height: size.width * 0.003),
// MyText(
// text: label,
// textStyle: Theme.of(context).textTheme.bodySmall!.copyWith(
// fontSize: size.width * 0.02,
// color: Theme.of(context).primaryColorDark.withOpacity(0.55),
// ),
// textAlign: TextAlign.center,
// ),
// ],
// ),
// );
// }
Widget _buildEarningsStats(BuildContext context, Size size) {
final accBloc = context.read<AccBloc>();
final isNotEmpty = accBloc.earningsList.isNotEmpty;
final weekIndex = accBloc.choosenEarningsWeeks ?? 0;
final isOwner = userData?.role == 'owner';
if (!isNotEmpty) {
return const SizedBox.shrink();
}
final weekData = accBloc.earningsList[weekIndex];
final totalTrips = weekData.totalTrips.toString();
final totalWallet = formatVND(
weekData.totalWalletAmount,
symbol: accBloc.earningCurrency ?? '₫',
);
final statsText = isOwner
? '${AppLocalizations.of(context)!.trips} : $totalTrips • Wallet: $totalWallet'
: '${AppLocalizations.of(context)!.login} : ${weekData.totalLoggedInHours.replaceAll("Mins", AppLocalizations.of(context)!.mins).replaceAll("Hr", AppLocalizations.of(context)!.hours)} • ${AppLocalizations.of(context)!.trips} : $totalTrips';
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: MyText(
text: statsText,
textStyle: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontSize: size.width * 0.032,
color: AppColors.white.withOpacity(0.85),
),
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
],
);
}
/// Calculate container height for chart list
/// Leaves room for header + chart on top, scales responsively
double _calculateContainerHeight(Size size) {
final topPadding = MediaQuery.of(context).padding.top;
final isFromDashboard = widget.args?.from == 'dashboard';
// Estimate top section (safe area + header + chart)
// Header components total: ~0.75w to 0.80w (dashboard adds back button)
// Chart: 0.32w
// Plus spacing/padding buffer: ~0.08w
final topSectionHeight =
topPadding + (size.width * (isFromDashboard ? 0.83 : 0.78));
// Container bottom gets remaining height with small margin
final bottomMargin = size.width * 0.03;
final containerHeight = size.height - topSectionHeight - bottomMargin;
// Ensure reasonable range: 30-50% of screen height
// Prevents chart overlap on large screens and ensures list scrollability
return containerHeight.clamp(size.height * 0.50, size.height * 0.70);
}
}
Editor is loading...
Leave a Comment