Flutter 颤振-如何制作卡片列表,显示每年每月的项目?

Flutter 颤振-如何制作卡片列表,显示每年每月的项目?,flutter,dart,Flutter,Dart,费用追踪者 class Transactions { final String id; final double amount; final String date; const Transactions({ @required this.id, @required this.amount, @required this.date, }); Transactions.fromMap( Map map, ) : th

费用追踪者

 class Transactions {
  final String id;

  final double amount;
  final String date;


  const Transactions({
    @required this.id,
    @required this.amount,
    @required this.date,
   
  });

  Transactions.fromMap(
    Map map,
  )   : this.id = map['id'],
        this.amount = map['amount'],
        this.date = map['date'],
       

  Map toMap() {
    return {
      // 'color': this.color,
      'id': this.id,
      'amount': this.amount,
      'date': this.date,
     
    };
  }
}
我试图制作一张卡片列表,告诉用户在本年度的几个月内已经发生的费用,如下所示:

我试过这个:

    return List.generate(2, (index) {
    
      final weekDay = DateTime.now().subtract(
        Duration(days: index),
      );
      var totalSum = 0.0;

      for (var i = 0; i < widget.transactions.length; i++) {
        if (DateTime.parse(widget.transactions[i].date).year == weekDay.year &&
            DateTime.parse(widget.transactions[i].date).month ==
                weekDay.month) {
          totalSum += widget.transactions[i].amount;
        }
      }

      // for (var i = 0; i < widget.transactions.length; i++) {
      //   totalSum = widget.transactions[index].amount + totalSum;
      // }

      return {
        'day': DateFormat.E().format(weekDay).substring(0, 1),
        'amount': totalSum,
      };
    }).reversed.toList();
  }

  double get totalSpending {
    return groupedTransactionValues.fold(0.0, (sum, item) {
      return sum + item['amount'];
    });
  }
返回列表。生成(2,(索引){
最后一个工作日=DateTime.now()。减去(
持续时间(天数:指数),
);
var totalSum=0.0;
对于(var i=0;i
但它并不是在做我想让它做的事。
嗯,最简单的方法之一是将您的数据组织在一个列表中,其中每个元素都是一个月的相关细节(即金额)

例如:

class Month{
 int month; //between 1 and 12
 double amount;
} 
因此,您只需要在build函数中实现列表,如下所示:

build(context){
 return(
 //...
 child: List.builder(
 itemCount: yourlist.length
 itemBuilder: (context, index){
  return YourTileWidget(month: yourlist[index].month, amount: yourlist[index].amount)
    }
   ),
  //...
  );
}

希望这是你想要实现的

你应该澄清你遇到的错误,而不仅仅是说它没有做你想做的事情

这就是我通常对卡片列表所做的:

ListView.builder(
  itemCount: listTransaction.length, // your List
  itemBuilder: (context, index) {
    return Card(                           
      child: Column(
        children: [
          Text(listTransaction[index].date),
          Text(listTransaction[index].amount)
        ]
      )
    );
  },
)

你试过调试你的generate方法吗?没有,这不是我想要的。我想生成一张卡片列表,这些卡片必须在这几个月内花费总计。例如,在一月份,用户总共花费了200美元。因此,这张卡将有200美元,1月1日给了你一般的方法。例如,您可以更改类月份并放置所需的任何数据