ambito/lib/src/pages/cart/cart_page.dart

283 lines
9.2 KiB
Dart
Raw Normal View History

2024-12-13 09:41:26 +01:00
import 'dart:typed_data';
import 'package:ambito/src/entity/cart/cart_datasource.dart';
import 'package:ambito/src/entity/cart/cart_element.dart';
import 'package:ambito/src/entity/cart/cart_repository.dart';
import 'package:ambito/src/entity/measure/complete/measure_complete.dart';
import 'package:ambito/src/entity/measure/general/measure_general.dart';
2024-11-09 22:03:03 +01:00
import 'package:ambito/src/packages/ambito_theme/ambito_theme.dart';
import 'package:flutter/material.dart';
2024-12-13 09:41:26 +01:00
import 'package:flutter_to_pdf/export_delegate.dart';
import 'package:flutter_to_pdf/export_frame.dart';
2024-11-18 09:15:00 +01:00
import 'package:screen_breakpoints/screen_breakpoints.dart';
2024-12-13 09:41:26 +01:00
import 'package:screenshot/screenshot.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import '../../entity/cart/cart.dart';
import '../../entity/measure/general/measure_general_repository.dart';
import '../../widgets/buttons/print_all_button.dart';
import '../../widgets/buttons/print_button.dart';
import '../../widgets/page/base_page.dart';
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
class CartPage extends StatefulWidget {
const CartPage({super.key, required this.measure});
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
final MeasureComplete? measure;
2024-11-09 22:03:03 +01:00
@override
2024-12-13 09:41:26 +01:00
State<StatefulWidget> createState() => CartPageState();
2024-11-09 22:03:03 +01:00
}
2024-12-13 09:41:26 +01:00
class CartPageState extends State<CartPage> {
List<Cart>? carts;
Widget cartCards = const SizedBox();
final ExportDelegate exportDelegate = ExportDelegate(
ttfFonts: {
'OpenSans': 'fonts/OpenSans-Regular.ttf',
'OpenSans_regular': 'fonts/OpenSans-Regular.ttf',
'OpenSans_500': 'fonts/OpenSans-Bold.ttf',
},
);
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
final ScreenshotController screenshotController = ScreenshotController();
Uint8List? _imageFile;
2024-11-09 22:03:03 +01:00
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final AmbitoTheme theme = getTheme(context);
2024-12-13 09:41:26 +01:00
carts = CartRepository().getMyCarts();
return BasePage().getPage(
context,
BreakpointBuilder(
builder: (
context,
breakpoint,
configuration,
) {
return SingleChildScrollView(
child: Center(
child: Screenshot(
controller: screenshotController,
child: SizedBox(
width: Breakpoint.fromContext(context).width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
theme.verticalSpacer,
Text(
'Warenkorb',
style: theme.headlineLarge,
textAlign: TextAlign.start,
),
theme.verticalSpacerSmall,
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Spacer(),
PrintAllButton(
onPressed: () async {
screenshotController.capture();
},
),
],
),
theme.verticalSpacerSmall,
buildCards(theme, carts),
],
),
),
),
2024-11-09 22:03:03 +01:00
),
2024-12-13 09:41:26 +01:00
);
},
2024-11-09 22:03:03 +01:00
),
);
}
2024-12-13 09:41:26 +01:00
Future<void> saveFile(document, String name) async {}
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
Widget buildCards(AmbitoTheme theme, List<Cart>? carts) {
List<Widget> children = [];
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
if (carts != null) {
int counter = 0;
for (final cart in carts) {
final measure = MeasureGeneralRepository().getByName(cart.name!);
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
final elements = CartRepository().getElementsByCartId(cart.id);
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
double sum = 0;
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
for (final element in elements) {
sum = sum +
((int.tryParse(element.amount ?? '1') ?? 1) *
(double.tryParse(element.price ?? '0') ?? 0));
}
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
final card = ExportFrame(
exportDelegate: exportDelegate,
frameId: 'cart${cart.id}',
child: SizedBox(
width: 1152,
child: Card(
elevation: 0,
color: theme.currentColorScheme.tertiary,
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
cart.name!,
style: theme.labelLarge.copyWith(
color: theme.currentColorScheme.primary),
),
const Spacer(),
SizedBox(
height: 40,
width: 200,
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(10),
),
child: measure?.getFullImage() ??
Image.asset('assets/images/logo_trans.png'),
),
),
],
),
theme.verticalSpacerSmall,
gridWidget(elements ?? []),
theme.verticalSpacer,
Row(
children: [
Text(
'Richtpreis ca. ${sum.toStringAsFixed(2)}',
style: theme.labelMedium
.copyWith(fontWeight: FontWeight.bold),
),
Spacer(),
PrintButton(
onPressed: () async {
final pdf = await exportDelegate
.exportToPdfDocument('cart${cart.id}');
await saveFile(pdf, '${cart.id}.pdf');
},
),
],
)
],
),
),
),
),
);
children.add(card);
children.add(theme.verticalSpacerMax);
counter++;
}
}
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
return ExportFrame(
frameId: 'completeCart',
exportDelegate: exportDelegate,
child: Column(
children: children,
),
);
2024-11-09 22:03:03 +01:00
}
2024-12-13 09:41:26 +01:00
Widget gridWidget(List<CartElement> cartElements) {
final AmbitoTheme theme = getTheme(context);
2024-11-09 22:03:03 +01:00
2024-12-13 09:41:26 +01:00
return SfDataGridTheme(
data: SfDataGridThemeData(
headerColor: theme.currentColorScheme.primaryContainer,
sortIcon: Icon(
Icons.keyboard_arrow_down,
color: theme.currentColorScheme.primary,
),
),
child: SfDataGrid(
allowSorting: true,
source: CartDataSource(cartElements: cartElements, context: context),
columnWidthMode: ColumnWidthMode.fill,
columns: [
GridColumn(
columnName: 'name',
label: Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
alignment: Alignment.centerLeft,
child: Text(
'Material',
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
),
),
GridColumn(
columnName: 'amount',
maximumWidth: 150,
label: Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
alignment: Alignment.centerLeft,
child: Text(
'Menge',
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
),
),
GridColumn(
columnName: 'provider',
maximumWidth: 200,
allowSorting: false,
label: Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
alignment: Alignment.centerLeft,
child: Text(
'Anbieter',
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
),
),
GridColumn(
columnName: 'price',
allowSorting: true,
maximumWidth: 250,
label: Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
alignment: Alignment.centerLeft,
child: Text(
'Richtpreis',
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
),
),
],
),
);
2024-11-09 22:03:03 +01:00
}
}