您好,我正在使用以下功能按键分组并计算总计:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class MyCount {
String count;
MyCount({this.count});
}
class MyCountLocale extends LocalizationsDelegate<MyCount> {
MyCount data;
MyCountLocale(this.data);
@override
bool isSupported(Locale locale) {
return true;
}
@override
Future<MyCount> load(Locale locale) async {
return data;
}
@override
bool shouldReload(MyCountLocale old) {
return old.data != data;
}
}
Future<void> main() async {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ValueNotifier<int> count = ValueNotifier<int>(0);
LocalizationsDelegate myLocale;
@override
void initState() {
count.addListener(() {
setState(() {
myLocale = MyCountLocale(MyCount(count: count.value.toString()));
});
});
myLocale = MyCountLocale(MyCount(count: count.value.toString()));
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
myLocale,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: MyHomePage(count: count),
);
}
}
class MyHomePage extends StatefulWidget {
final ValueNotifier<int> count;
MyHomePage({this.count});
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
primary: true,
appBar: AppBar(),
body: Column(
children: <Widget>[
FloatingActionButton(
onPressed: () => widget.count.value++,
child: Icon(Icons.plus_one),
),
ListTile(
title: Text(Localizations.of<MyCount>(context, MyCount).count),
),
],
),
);
}
}
除了第一个项目是一个空对象之外,这工作得很好,我在这里做错什么了?
calcEquipmentTotals(){
var groups = this.inspection.equipments.reduce(function(obj,item){
obj[item.equipment_type_id] = obj[item.equipment_type_id] || { equipment_type_id: item.equipment_type_id, total: 0};
// Calculate total based on action
var total = obj[item.equipment_type_id].total;
if(item.action === 'added' || item.action === 'stocktake'){
total = total + item.quantity;
}else{
total = total - item.quantity;
};
obj[item.equipment_type_id].total = total;
return obj;
},[]);
return groups;
}
答案 0 :(得分:0)
这会做你想要的。
calcEquipmentTotals() {
var groups = this.inspection.equipments.reduce(function (map, item) {
const obj = map.get(item.equipment_type_id) || {
equipment_type_id: item.equipment_type_id, total: 0
};
if (item.action === 'added' || item.action === 'stocktake') {
obj.total += item.quantity;
} else {
obj.total -= item.quantity;
};
map.set(obj);
return map;
}, new Map());
return Array.from(groups.values());
}
答案 1 :(得分:0)
这些评论为我指明了正确的方向,这是最终完成的事情:
calcEquipmentTotals(){
var groups = this.inspection.equipments.reduce(function(obj,item){
obj[item.equipment_type_id] = obj[item.equipment_type_id] || { equipment_type_id: item.equipment_type_id, total: 0};
// Calculate total based on action
var total = obj[item.equipment_type_id].total;
if(item.action === 'added' || item.action === 'stocktake'){
total = total + item.quantity;
}else{
total = total - item.quantity;
};
obj[item.equipment_type_id].total = total;
return obj;
},{});
return Object.keys(groups).map((key) => groups[key]);
}