ambito/lib/src/entity/cart/cart_datasource.dart

158 lines
4.9 KiB
Dart
Raw Normal View History

2024-12-13 09:41:26 +01:00
import 'package:ambito/src/entity/cart/cart_element.dart';
import 'package:ambito/src/entity/entities.dart';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import '../../packages/ambito_theme/ambito_theme.dart';
class CartDataSource extends DataGridSource {
CartDataSource(
{required List<CartElement> cartElements, required this.context}) {
dataGridRows = cartElements
.map<DataGridRow>(
(dataGridRow) => DataGridRow(
cells: [
DataGridCell<String>(
columnName: 'name',
value: dataGridRow.name,
),
DataGridCell<String>(
columnName: 'amount',
value: dataGridRow.amount,
),
DataGridCell<List<IdValue>>(
columnName: 'provider',
value: dataGridRow.provider,
),
DataGridCell<String>(
columnName: 'price',
value: dataGridRow.price.toString(),
),
],
),
)
.toList();
}
final BuildContext context;
List<DataGridRow> dataGridRows = [];
@override
List<DataGridRow> get rows => dataGridRows;
@override
DataGridRowAdapter? buildRow(DataGridRow row) {
final AmbitoTheme theme = getTheme(context);
double completePrice = double.tryParse(row
.getCells()
.where((cell) => cell.columnName == 'price')
.first
.value
.toString() ??
'0.0') ??
0;
int amount = int.tryParse(row
.getCells()
.where((cell) => cell.columnName == 'amount')
.first
.value
.toString() ??
'0') ??
0;
if (amount > 1) {
completePrice = completePrice * amount;
}
return DataGridRowAdapter(
cells: row.getCells().map<Widget>(
(dataGridCell) {
if (dataGridCell.columnName == 'provider') {
final providerString =
dataGridCell.value.map((ele) => ele.value).toList().join(', ');
return Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
providerString,
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
);
}
if (dataGridCell.columnName == 'price') {
final priceString = amount == 1
? '${dataGridCell.value ?? '0'}'
: '${completePrice.toStringAsFixed(2)}€ (${dataGridCell.value ?? '0'}€ pro Stk.)';
return Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
priceString,
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
);
}
if (dataGridCell.columnName == 'amount') {
return Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
icon: Icon(
Icons.remove,
color: Theme.of(context).colorScheme.secondary,
),
padding:
EdgeInsets.symmetric(vertical: 4.0, horizontal: 18.0),
iconSize: 16.0,
color: Theme.of(context).primaryColor,
onPressed: () {},
),
Text(
dataGridCell.value,
textAlign: TextAlign.center,
style: theme.bodyMedium,
),
IconButton(
icon: Icon(
Icons.add,
color: Theme.of(context).colorScheme.secondary,
),
padding:
EdgeInsets.symmetric(vertical: 4.0, horizontal: 18.0),
iconSize: 16.0,
color: Theme.of(context).primaryColor,
onPressed: () {},
),
],
),
);
}
return Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
dataGridCell.value.toString(),
overflow: TextOverflow.ellipsis,
style: theme.bodyMedium.copyWith(
color: theme.currentColorScheme.primary,
),
),
);
},
).toList());
}
}