119 lines
3.7 KiB
Dart
119 lines
3.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:okane/database/collections/beneficiary.dart';
|
|
import 'package:okane/database/database.dart';
|
|
import 'package:okane/ui/state/core.dart';
|
|
import 'package:okane/ui/utils.dart';
|
|
import 'package:okane/ui/widgets/image_wrapper.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
class TransactionDetailsPage extends StatelessWidget {
|
|
final bool isPage;
|
|
|
|
const TransactionDetailsPage({this.isPage = false, super.key});
|
|
|
|
static MaterialPageRoute<void> get mobileRoute =>
|
|
MaterialPageRoute(builder: (_) => TransactionDetailsPage(isPage: true));
|
|
|
|
Future<void> _updateBeneficiaryIcon(Beneficiary beneficiary) async {
|
|
final pickedFile = await FilePicker.platform.pickFiles(
|
|
type: FileType.image,
|
|
);
|
|
if (pickedFile == null) {
|
|
return;
|
|
}
|
|
|
|
final file = pickedFile.files.first;
|
|
final suppPath = await getApplicationSupportDirectory();
|
|
final imageDir = p.join(suppPath.path, "beneficiaries");
|
|
final imagePath = p.join(imageDir, file.name);
|
|
print("Copying ${file.path!} to $imagePath");
|
|
|
|
await Directory(imageDir).create(recursive: true);
|
|
if (beneficiary.imagePath != null) {
|
|
await File(beneficiary.imagePath!).delete();
|
|
}
|
|
|
|
await File(file.path!).copy(imagePath);
|
|
|
|
print("Updating DB");
|
|
beneficiary.imagePath = imagePath;
|
|
await upsertBeneficiary(beneficiary);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final widget = BlocBuilder<CoreCubit, CoreState>(
|
|
builder: (context, state) {
|
|
if (state.activeTransaction == null) {
|
|
return Text("No transaction selected");
|
|
}
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: ListView(
|
|
children: [
|
|
Row(
|
|
children: [
|
|
StreamBuilder(
|
|
stream: watchBeneficiaryObject(
|
|
state.activeTransaction!.beneficiary.value!.id,
|
|
),
|
|
builder: (context, snapshot) {
|
|
final obj =
|
|
snapshot.data ??
|
|
state.activeTransaction!.beneficiary.value!;
|
|
return ImageWrapper(
|
|
title: obj.name,
|
|
path: obj.imagePath,
|
|
onTap: () => _updateBeneficiaryIcon(obj),
|
|
);
|
|
},
|
|
),
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Text(
|
|
state.activeTransaction!.beneficiary.value!.name,
|
|
),
|
|
),
|
|
Spacer(),
|
|
IconButton(
|
|
icon: Icon(Icons.edit),
|
|
onPressed: () {
|
|
// TODO: Implement
|
|
},
|
|
),
|
|
],
|
|
),
|
|
Wrap(
|
|
spacing: 8,
|
|
children:
|
|
state.activeTransaction!.tags
|
|
.map((tag) => Chip(label: Text(tag)))
|
|
.toList(),
|
|
),
|
|
Row(
|
|
children: [
|
|
state.activeTransaction!.amount > 0
|
|
? Icon(Icons.add)
|
|
: Icon(Icons.remove),
|
|
Text(formatCurrency(state.activeTransaction!.amount)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
if (isPage) {
|
|
return Scaffold(body: widget);
|
|
}
|
|
return widget;
|
|
}
|
|
}
|