prasule/lib/views/debts/debt_view.dart

142 lines
4.4 KiB
Dart
Raw Permalink Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:prasule/api/debt_scenario.dart';
import 'package:prasule/api/wallet.dart';
import 'package:prasule/api/wallet_manager.dart';
import 'package:prasule/main.dart';
import 'package:prasule/pw/platformroute.dart';
import 'package:prasule/util/drawer.dart';
import 'package:prasule/views/debts/setup_debt_scenario.dart';
import 'package:prasule/views/setup.dart';
/// Shows the selected [DebtScenario]
class DebtView extends StatefulWidget {
/// Shows the selected [DebtScenario]
const DebtView({super.key});
@override
State<DebtView> createState() => _DebtViewState();
}
class _DebtViewState extends State<DebtView> {
List<Wallet> wallets = [];
void loadWallet() {
wallets = WalletManager.listWallets();
if (wallets.isEmpty && mounted) {
unawaited(
Navigator.of(context)
.pushReplacement(platformRoute((c) => const SetupView())),
);
return;
}
setState(() {});
}
@override
void initState() {
super.initState();
loadWallet();
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: WalletManager.selectedWallet.debts.isEmpty
? 1
: WalletManager.selectedWallet.debts.length,
child: Scaffold(
floatingActionButton: FloatingActionButton(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
child: const Icon(Icons.add),
onPressed: () {
Navigator.of(context)
.push(platformRoute((c) => const SetupDebtScenario()))
.then((v) {
setState(() {});
});
},
),
appBar: AppBar(
title: DropdownButton<int>(
value: wallets.indexOf(
wallets
.where((w) => w.name == WalletManager.selectedWallet.name)
.first,
),
items: [
...wallets.map(
(e) => DropdownMenuItem(
value: wallets.indexOf(
e,
),
child: Text(e.name),
),
),
DropdownMenuItem(
value: -1,
child: Text(AppLocalizations.of(context).newWallet),
),
],
onChanged: (v) async {
if (v == null || v == -1) {
await Navigator.of(context).push(
platformRoute(
(c) => const SetupView(
newWallet: true,
),
),
);
wallets = WalletManager.listWallets();
logger.i(wallets.length);
WalletManager.selectedWallet = wallets.last;
setState(() {});
return;
}
WalletManager.selectedWallet = wallets[v];
setState(() {});
},
),
bottom: TabBar(
tabs: (WalletManager.selectedWallet.debts.isEmpty)
? [Text(AppLocalizations.of(context).welcome)]
: List.generate(
WalletManager.selectedWallet.debts.length,
(index) => Tab(
text: WalletManager.selectedWallet.debts[index].name,
),
),
),
),
drawer: makeDrawer(context, Pages.debts),
body: TabBarView(
children: (WalletManager.selectedWallet.debts.isEmpty)
? [
Column(
children: [
const SizedBox(
height: 20,
),
Text(
AppLocalizations.of(context).noDebtScenarios,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
Text(AppLocalizations.of(context).noDebtScenariosSub),
],
),
]
: List.generate(
WalletManager.selectedWallet.debts.length,
(c) => const Placeholder(),
),
),
),
);
}
}