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 cartElements, required this.context}) { dataGridRows = cartElements .map( (dataGridRow) => DataGridRow( cells: [ DataGridCell( columnName: 'name', value: dataGridRow.name, ), DataGridCell( columnName: 'amount', value: dataGridRow.amount, ), DataGridCell>( columnName: 'provider', value: dataGridRow.provider, ), DataGridCell( columnName: 'price', value: dataGridRow.price.toString(), ), ], ), ) .toList(); } final BuildContext context; List dataGridRows = []; @override List 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( (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()); } }