Skip to content

Commit

Permalink
feat: Custom Duration Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Emon526 committed Nov 16, 2024
1 parent 592658c commit e446aa3
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 119 deletions.
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ void main() {
}

//TODO:: Check Responsive

//TODO:: Custom chart duration
class MyApp extends StatelessWidget {
const MyApp({super.key});

Expand Down
4 changes: 2 additions & 2 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class HomePage extends StatelessWidget {
0.3,
child: Consumer<TransactionProvider>(
builder: (context, provider, child) {
return Chart(provider.recentTransactions);
return Chart();
},
),
),
Expand Down Expand Up @@ -208,7 +208,7 @@ class HomePage extends StatelessWidget {
appBar.preferredSize.height -
mediaQuery.padding.top) *
0.7,
child: Chart(provider.recentTransactions),
child: Chart(),
)
: txListWidget;
},
Expand Down
35 changes: 34 additions & 1 deletion lib/providers/chartprovider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:expense_planner/providers/transaction_provider.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

Expand All @@ -7,6 +6,7 @@ import '../models/transaction.dart';
class ChartProvider with ChangeNotifier {
int _selectedDays = 7;
int get selectedDays => _selectedDays;

void set selectedDays(int value) {
if (value != _selectedDays) {
_selectedDays = value;
Expand All @@ -21,4 +21,37 @@ class ChartProvider with ChangeNotifier {
];

List<int> get selectedDaysList => _selectedDaysList;

List<Map<String, Object>> groupTransactionValues(
List<TransactionModel> transactions) {
return List.generate(
_selectedDays,
(index) {
final weekDay = DateTime.now().subtract(
Duration(days: index),
);
var totalSum = 0.0;

for (var i = 0; i < transactions.length; i++) {
if (transactions[i].date.day == weekDay.day &&
transactions[i].date.month == weekDay.month &&
transactions[i].date.year == weekDay.year) {
totalSum += transactions[i].amount;
}
}

return {
'day': DateFormat.E().format(weekDay).substring(0, 1),
'amount': totalSum,
};
},
).reversed.toList();
}

// Calculate total spending using the groupTransactionValues
double totalSpending(List<TransactionModel> transactions) {
return groupTransactionValues(transactions).fold(0.0, (sum, item) {
return sum + (item['amount'] as double);
});
}
}
155 changes: 153 additions & 2 deletions lib/theme/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ThemeData lightTheme = ThemeData(
brightness: Brightness.light,
primaryColor: primaryColor,
// canvasColor: Colors.white,
// fontFamily: fontFamily,
fontFamily: fontFamily,
indicatorColor: primaryColor,
textSelectionTheme: TextSelectionThemeData(
cursorColor: primaryColor,
Expand All @@ -48,6 +48,82 @@ ThemeData lightTheme = ThemeData(
),
behavior: SnackBarBehavior.floating,
),
datePickerTheme: DatePickerThemeData(
// backgroundColor: Colors.white,
// dayOverlayColor: WidgetStatePropertyAll(Colors.amberAccent),
// inputDecorationTheme: InputDecorationTheme(fillColor: Colors.amber),
// shadowColor: Colors.amber,
todayBorder: BorderSide(
color: primaryColor,
),

// surfaceTintColor: Colors.red,
todayForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.black;
}),
todayBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return primaryColor;
}
return null;
}),
dayBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return primaryColor;
}
return null;
}),
dayForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.black;
}),
yearBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return primaryColor;
}
return null;
}),
yearForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.black;
}),
cancelButtonStyle:
ButtonStyle(foregroundColor: WidgetStatePropertyAll(primaryColor)),
confirmButtonStyle:
ButtonStyle(foregroundColor: WidgetStatePropertyAll(primaryColor)),
),

textTheme: TextTheme(
bodyLarge: TextStyle(
color: Colors.black87,
Expand Down Expand Up @@ -261,7 +337,7 @@ ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
primaryColor: secondaryColor,
// canvasColor: Colors.white,
// fontFamily: fontFamily,
fontFamily: fontFamily,
indicatorColor: secondaryColor,
textSelectionTheme: TextSelectionThemeData(
cursorColor: secondaryColor,
Expand All @@ -287,6 +363,81 @@ ThemeData darkTheme = ThemeData(
),
behavior: SnackBarBehavior.floating,
),
datePickerTheme: DatePickerThemeData(
// backgroundColor: Colors.white,
// dayOverlayColor: WidgetStatePropertyAll(Colors.amberAccent),
// inputDecorationTheme: InputDecorationTheme(fillColor: Colors.amber),
// shadowColor: Colors.amber,
todayBorder: BorderSide(
color: secondaryColor,
),

// surfaceTintColor: Colors.red,
todayForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.white;
}),
todayBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return secondaryColor;
}
return null;
}),
dayBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return secondaryColor;
}
return null;
}),
dayForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.white;
}),
yearBackgroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return secondaryColor;
}
return null;
}),
yearForegroundColor:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return null;
}
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return Colors.white;
}),
cancelButtonStyle:
ButtonStyle(foregroundColor: WidgetStatePropertyAll(secondaryColor)),
confirmButtonStyle:
ButtonStyle(foregroundColor: WidgetStatePropertyAll(secondaryColor)),
),

textTheme: TextTheme(
bodyLarge: TextStyle(
Expand Down
Loading

0 comments on commit e446aa3

Please sign in to comment.