null safety
This commit is contained in:
@@ -11,13 +11,13 @@ typedef OnRemove(CargoType cargoType);
|
||||
typedef OnUpdate(CargoType cargoType);
|
||||
|
||||
class CargoTable extends StatefulWidget {
|
||||
final List<CargoType> cargoTypes;
|
||||
final bool isNew;
|
||||
final OnRemove onRemove;
|
||||
final OnUpdate onUpdate;
|
||||
final List<CargoType>? cargoTypes;
|
||||
final bool? isNew;
|
||||
final OnRemove? onRemove;
|
||||
final OnUpdate? onUpdate;
|
||||
|
||||
const CargoTable(
|
||||
{Key key, this.cargoTypes, this.isNew, this.onRemove, this.onUpdate})
|
||||
{Key? key, this.cargoTypes, this.isNew, this.onRemove, this.onUpdate})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
@@ -26,14 +26,14 @@ class CargoTable extends StatefulWidget {
|
||||
|
||||
class _CargoTableState extends State<CargoTable> {
|
||||
double totalWeight = 0;
|
||||
List<CargoType> cargoTypes;
|
||||
List<CargoType>? cargoTypes;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
cargoTypes = widget.cargoTypes;
|
||||
if (!widget.isNew) {
|
||||
if (!widget.isNew!) {
|
||||
totalWeight =
|
||||
cargoTypes.fold(0, (previous, current) => previous + current.weight);
|
||||
cargoTypes!.fold(0, (previous, current) => previous + current.weight!);
|
||||
}
|
||||
|
||||
super.initState();
|
||||
@@ -41,7 +41,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print("Cargotypes:${cargoTypes.length}");
|
||||
print("Cargotypes:${cargoTypes!.length}");
|
||||
|
||||
return SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
@@ -80,18 +80,18 @@ class _CargoTableState extends State<CargoTable> {
|
||||
if (cargoTypes == null) {
|
||||
return [];
|
||||
}
|
||||
var rows = cargoTypes.map((c) {
|
||||
var rows = cargoTypes!.map((c) {
|
||||
return MyDataRow(
|
||||
onSelectChanged: (bool selected) async {},
|
||||
cells: [
|
||||
MyDataCell(
|
||||
new Text(
|
||||
c.name == null ? "" : c.name,
|
||||
c.name ??'',
|
||||
style: textStyle,
|
||||
),
|
||||
),
|
||||
MyDataCell(
|
||||
c.isCutomDuty
|
||||
c.isCutomDuty!
|
||||
? GestureDetector(
|
||||
onTap: () async {
|
||||
String _t = await showDialog(
|
||||
@@ -103,7 +103,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
setState(() {
|
||||
c.qty = int.tryParse(_t) ?? 0;
|
||||
});
|
||||
if (widget.onUpdate != null) widget.onUpdate(c);
|
||||
if (widget.onUpdate != null) widget.onUpdate!(c);
|
||||
},
|
||||
child: Center(
|
||||
child: Container(
|
||||
@@ -144,7 +144,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
context: context,
|
||||
builder: (_) => DialogInput(
|
||||
label: "cargo.weight",
|
||||
value: c.weight.toStringAsFixed(2)));
|
||||
value: c.weight!.toStringAsFixed(2)));
|
||||
|
||||
if (_t == null) return;
|
||||
setState(() {
|
||||
@@ -153,7 +153,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
if (c.weight != 0) {
|
||||
_cal();
|
||||
}
|
||||
if (widget.onUpdate != null) widget.onUpdate(c);
|
||||
if (widget.onUpdate != null) widget.onUpdate!(c);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(7.0),
|
||||
@@ -162,7 +162,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
borderRadius: BorderRadius.all(Radius.circular(5.0)),
|
||||
),
|
||||
child: Text(
|
||||
c.weight == null ? "0.00" : c.weight.toStringAsFixed(2),
|
||||
c.weight == null ? "0.00" : c.weight!.toStringAsFixed(2),
|
||||
style: textStyle),
|
||||
),
|
||||
),
|
||||
@@ -176,7 +176,7 @@ class _CargoTableState extends State<CargoTable> {
|
||||
color: primaryColor,
|
||||
),
|
||||
onPressed: () {
|
||||
if (widget.onRemove != null) widget.onRemove(c);
|
||||
if (widget.onRemove != null) widget.onRemove!(c);
|
||||
})
|
||||
],
|
||||
),
|
||||
@@ -237,21 +237,21 @@ class _CargoTableState extends State<CargoTable> {
|
||||
}
|
||||
|
||||
_cal() {
|
||||
var cargoType = autoCalWeight(cargoTypes, totalWeight);
|
||||
var cargoType = autoCalWeight(cargoTypes!, totalWeight);
|
||||
if (cargoType == null) return;
|
||||
|
||||
setState(() {
|
||||
cargoTypes.remove(cargoType);
|
||||
cargoTypes.add(cargoType);
|
||||
cargoTypes!.remove(cargoType);
|
||||
cargoTypes!.add(cargoType);
|
||||
});
|
||||
|
||||
if (widget.onUpdate != null) {
|
||||
widget.onUpdate(cargoType);
|
||||
widget.onUpdate!(cargoType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CargoType autoCalWeight(List<CargoType> cargoTypes, double total) {
|
||||
CargoType? autoCalWeight(List<CargoType> cargoTypes, double total) {
|
||||
if ((cargoTypes?.length ?? 0) == 0 || total == 0) return null;
|
||||
List<CargoType> noWeight = cargoTypes.where((c) => c.weight == 0).toList();
|
||||
if (noWeight.length != 1) return null;
|
||||
|
||||
Reference in New Issue
Block a user