@@ -4,6 +4,8 @@ import 'package:appetizer/domain/models/transaction/paginated_yearly_rebate.dart
44import 'package:appetizer/presentation/leaves_and_rebate/components/custom_divider.dart' ;
55import 'package:appetizer/presentation/components/shadow_container.dart' ;
66import 'package:flutter/material.dart' ;
7+ import 'package:flutter_bloc/flutter_bloc.dart' ;
8+ import 'package:appetizer/domain/repositories/transaction_repositroy.dart' ;
79import 'package:month_picker_dialog/month_picker_dialog.dart' ;
810
911class MonthlyRebates extends StatefulWidget {
@@ -39,6 +41,26 @@ class _MonthlyRebatesState extends State<MonthlyRebates> {
3941 // super.initState();
4042 // }
4143
44+ @override
45+ void initState () {
46+ super .initState ();
47+ year = DateTime .now ().year;
48+ _currMonthIndex = widget.currMonthIndex;
49+ paginatedYearlyRebate = widget.paginatedYearlyRebate;
50+ }
51+
52+ void _rebuildMonthlyMap () {
53+ _totalRebate = 0 ;
54+ _monthlyRebateMap.clear ();
55+ if (paginatedYearlyRebate != null ) {
56+ for (YearlyRebate yr in paginatedYearlyRebate! .results) {
57+ _monthlyRebateMap[_monthList[yr.monthId]] = yr.rebate;
58+ _totalRebate += yr.rebate;
59+ }
60+ }
61+ _monthlyRebateMap["All" ] = _totalRebate;
62+ }
63+
4264 final _monthList = [
4365 'All' ,
4466 'January' ,
@@ -56,16 +78,10 @@ class _MonthlyRebatesState extends State<MonthlyRebates> {
5678 ];
5779 @override
5880 Widget build (BuildContext context) {
59- _totalRebate = 0 ;
6081 _currMonthIndex ?? = widget.currMonthIndex;
6182 _currMonthName = _monthList[_currMonthIndex! ];
6283 paginatedYearlyRebate ?? = widget.paginatedYearlyRebate;
63- for (YearlyRebate yr in paginatedYearlyRebate! .results) {
64- _monthlyRebateMap[_monthList[yr.monthId]] = yr.rebate;
65- _totalRebate += yr.rebate;
66- }
67- _monthlyRebateMap["All" ] = _totalRebate;
68- year = DateTime .now ().year;
84+ _rebuildMonthlyMap ();
6985 return ShadowContainer (
7086 width: 312. toAutoScaledWidth,
7187 offset: 2 ,
@@ -112,10 +128,17 @@ class _MonthlyRebatesState extends State<MonthlyRebates> {
112128 initialDate: DateTime (year, _currMonthIndex! ),
113129 lastDate: DateTime .now (),
114130 );
115- // TODO: rebates for prev year are blocked
116- if (newDateTime != null &&
117- newDateTime.year == year &&
118- newDateTime.month != _currMonthIndex) {
131+ if (newDateTime == null ) return ;
132+ if (newDateTime.year != year) {
133+ final repo = context.read <TransactionRepository >();
134+ final data = await repo.getYearlyRebates (newDateTime.year);
135+ setState (() {
136+ year = newDateTime.year;
137+ paginatedYearlyRebate = data;
138+ _currMonthIndex = newDateTime.month;
139+ _rebuildMonthlyMap ();
140+ });
141+ } else if (newDateTime.month != _currMonthIndex) {
119142 setState (() {
120143 _currMonthIndex = newDateTime.month;
121144 });
0 commit comments