95 lines
3.1 KiB
Dart
95 lines
3.1 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/collections/beneficiary.dart';
|
|
import 'package:okane/database/collections/budget.dart';
|
|
import 'package:okane/database/collections/loan.dart';
|
|
import 'package:okane/database/database.dart';
|
|
import 'package:okane/i18n/strings.g.dart';
|
|
import 'package:okane/ui/state/core.dart';
|
|
import 'package:searchfield/searchfield.dart';
|
|
|
|
class AddLoanPopup extends StatefulWidget {
|
|
final VoidCallback onDone;
|
|
|
|
const AddLoanPopup({super.key, required this.onDone});
|
|
|
|
@override
|
|
AddBudgetState createState() => AddBudgetState();
|
|
}
|
|
|
|
class AddBudgetState extends State<AddLoanPopup> {
|
|
final TextEditingController _beneficiaryTextController =
|
|
TextEditingController();
|
|
SearchFieldListItem<Beneficiary>? _selectedBeneficiary;
|
|
|
|
String getBeneficiaryName(Beneficiary item) {
|
|
return switch (item.type) {
|
|
BeneficiaryType.account => t.common.beneficiary.nameWithAccount(
|
|
name: item.name,
|
|
),
|
|
BeneficiaryType.other => item.name,
|
|
};
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: BlocBuilder<CoreCubit, CoreState>(
|
|
builder:
|
|
(context, state) => SearchField<Beneficiary>(
|
|
suggestions:
|
|
state.beneficiaries
|
|
.where((el) {
|
|
final bloc = GetIt.I.get<CoreCubit>();
|
|
if (el.type == BeneficiaryType.account) {
|
|
return el.account.value?.id.toInt() ==
|
|
bloc.activeAccount?.id.toInt();
|
|
}
|
|
return true;
|
|
})
|
|
.map((el) {
|
|
return SearchFieldListItem(
|
|
getBeneficiaryName(el),
|
|
item: el,
|
|
);
|
|
})
|
|
.toList(),
|
|
hint: "Beneficiary",
|
|
controller: _beneficiaryTextController,
|
|
selectedValue: _selectedBeneficiary,
|
|
onSuggestionTap: (beneficiary) {
|
|
setState(() => _selectedBeneficiary = beneficiary);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
OutlinedButton(
|
|
onPressed: () async {
|
|
if (_beneficiaryTextController.text.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
final bloc = GetIt.I.get<CoreCubit>();
|
|
final loan =
|
|
Loan()..beneficiary.value = _selectedBeneficiary!.item;
|
|
await upsertLoan(loan);
|
|
widget.onDone();
|
|
},
|
|
child: Text(t.modals.add),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|