53 lines
1.6 KiB
Dart
53 lines
1.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
import 'package:okane/database/sqlite.dart';
|
|
import 'package:okane/i18n/strings.g.dart';
|
|
import 'package:okane/ui/state/core.dart';
|
|
import 'package:okane/ui/utils.dart';
|
|
import 'package:okane/ui/widgets/piechart_card.dart';
|
|
|
|
class TotalBalanceCard extends StatelessWidget {
|
|
const TotalBalanceCard({super.key});
|
|
|
|
Future<double> _getTotalBalance(List<Account> accounts) async {
|
|
if (accounts.isEmpty) {
|
|
return 0;
|
|
}
|
|
|
|
final db = GetIt.I.get<OkaneDatabase>();
|
|
final totalBalance = await db.transactionsDao.getTotalBalance(
|
|
accounts.map((a) => a.id),
|
|
);
|
|
|
|
final loanSum = await db.loansDao.getTotalLoanSum();
|
|
return totalBalance + loanSum;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocBuilder<CoreCubit, CoreState>(
|
|
builder: (context, state) {
|
|
return ResponsiveCard(
|
|
titleText: t.pages.accounts.totalBalance.title,
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16),
|
|
child: FutureBuilder(
|
|
future: _getTotalBalance(state.accounts),
|
|
builder: (context, snapshot) {
|
|
print("SNAPSHOT: ${snapshot.data}");
|
|
return Text(
|
|
snapshot.hasData
|
|
? formatCurrency(snapshot.data!)
|
|
: t.pages.accounts.totalBalance.loading,
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|