import 'package:fcs/domain/entities/cargo_type.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/widgets/local_text.dart'; import 'package:fcs/pages/widgets/my_data_table.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'total_weight_edit.dart'; typedef OnAdd(CargoType cargoType); typedef OnRemove(CargoType cargoType); class CargoTable extends StatefulWidget { final List? cargoTypes; final OnAdd? onAdd; final OnRemove? onRemove; const CargoTable({Key? key, this.cargoTypes, this.onAdd, this.onRemove}) : super(key: key); @override _CargoTableState createState() => _CargoTableState(); } class _CargoTableState extends State { double totalWeight = 0; List _cargos = []; double remainingWeight = 0; @override Widget build(BuildContext context) { return DataTable( headingRowHeight: 40, showCheckboxColumn: false, columns: [ DataColumn( label: LocalText( context, "cargo.type", color: Colors.grey, ), ), DataColumn( label: Row( children: [ Container( padding: EdgeInsets.only(left: 50), child: LocalText( context, "cargo.weight", color: Colors.grey, ), ), ], ), ), ], rows: getCargoRows(context), ); } List getCargoRows(BuildContext context) { if (widget.cargoTypes == null) { return []; } List _list = []; List _types = []; double _total = 0; var rows = widget.cargoTypes!.map((c) { _total += c.weight; return DataRow( onSelectChanged: (bool? selected) async {}, cells: [ DataCell(Row( children: [ new Text( c.name ?? "", style: textStyle, ), new Text( c.qty == null || c.qty == 0 ? "" : " x ${c.qty.toString()}", style: TextStyle(color: Colors.grey), ), ], )), DataCell( Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Text(c.weight.toStringAsFixed(2), style: textStyle), widget.onRemove == null ? SizedBox( width: 50, ) : IconButton( icon: Icon( Icons.remove_circle, color: primaryColor, ), onPressed: () { if (widget.onRemove != null) widget.onRemove!(c); }) ], ), ), ], ); }).toList(); var totalRow = DataRow( onSelectChanged: (bool? selected) {}, cells: [ DataCell(Align( alignment: Alignment.centerRight, child: LocalText( context, "shipment.cargo.total", color: Colors.black87, fontWeight: FontWeight.bold, ), )), DataCell( Padding( padding: const EdgeInsets.only(right: 40.0), child: Align( alignment: Alignment.centerRight, child: InkWell( onTap: () async { double? _t = await Navigator.of(context).push( CupertinoPageRoute( builder: (context) => TotalWeightEdit(totalWeight: totalWeight))); if (_t == null) return; setState(() { totalWeight = _t; this.remainingWeight = this.totalWeight - _total; widget.cargoTypes!.forEach((c) { if (c.qty == null) { this._cargos.add(c); } }); this._cargos.forEach((c) { _list.add(c.name!); }); widget.cargoTypes!.forEach((c) { _types.add(c.name!); }); if (this._cargos.length == widget.cargoTypes!.length - 1) { _types.forEach((t) { if (!_list.contains(t)) { widget.cargoTypes!.forEach((c) { if (c.name == t) { c.weight = this.remainingWeight; } }); } }); } }); }, child: Container( padding: const EdgeInsets.all(7.0), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.all(Radius.circular(5.0)), ), child: Text(totalWeight.toStringAsFixed(2), style: TextStyle(fontWeight: FontWeight.bold)), ), )), ), ), ], ); rows.add(totalRow); return rows; } double getRemainBalance(double total) { double _r = this.totalWeight < total ? 0 : this.totalWeight - total; return _r; } }