fl_chart用小部件包装PieChartSectionData

时间:2020-10-19 10:33:39

标签: flutter dart fl-chart

我正在尝试使用fl_chart包创建一个饼图,并且想知道是否可以使用flutter小部件将元素包装在sections中。

...    
PieChart(
                            PieChartData(
                              pieTouchData: pieTouchData,
                              borderData: FlBorderData(
                                show: false,
                              ),
                              centerSpaceRadius: 0,
                              sections: sections,
                              sectionsSpace: sections[index].value == 0 ? 0 : 1,
                            ),
                          )
...

这是各节的代码段:

...
    sections = List.generate(
                                categoryList.length,
                                (int sectionIndex) {
                                  
                                  return PieChartSectionData(
                                    titlePositionPercentageOffset: .98,
                                    titleStyle: TextSetting.title24.copyWith(
                                        fontFamily: "Parisienne",
                                        color: Colors.white,
                                        fontSize: fontSize),
                                    radius: radius,
                                    color: widget.transactionType ==
                                            TransactionType.expense
                                        ? AppColour.expensesColor[sectionIndex]
                                        : AppColour.incomesColor[sectionIndex],
                                    title: categoryList[sectionIndex],
                                    showTitle: value == 0 ? false : true,
                                    value: value,
                                  );
                                },
                              );
...

其他:Im not sure this will be useful but just to clarify more

    sections = [PieChartSectionData(........), 
PieChartSectionData(......),
]

预期结果应为:

sections = [FlutterWidget(child: PieChartSectionData(...), FlutterWidget(child: PieChartSectionData(...)];

我无法按预期执行的错误是:

error: The argument type 'PieChartSectionData' can't be assigned to the parameter type 'Widget'. (argument_type_not_assignable at [expense_tracker] lib\screens\analysis_screen_component\monthly_piechart.dart:121)

据我了解,这是因为PieChartSectionData不是小部件,但是我可以做些什么使其成为小部件的一部分吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

也许我不明白你,但我用这个代码来表示“部分”

List<PieChartSectionData> _listShow(
List<YourData> yourData,
  ) {
var all = 0;
var oneProcent;
for (int i = 0; i < yourData.length; i++) {
  all = all + yourData[i].count;
}
oneProcent = all / 100;

return yourData.asMap().entries.map((entry) {
  int index = entry.key;
  final _isTouched = index == _touchedIndex;
  final double _fontSize = _isTouched ? 20 : 16;
  final double _radius = _isTouched ? 170 : 150;
  var _countdouble = entry.value.count.toDouble();
  return PieChartSectionData(
      color: colorsList[index],
      value: _countdouble,
      title: '',
      radius: _radius,
      badgeWidget: Text(
        '${(entry.value.count / oneProcent).toStringAsFixed(2)}%',
        style: TextStyle(
          fontSize: _fontSize,
          fontWeight: FontWeight.bold,
          color: _isTouched ? Colors.black : Color(0xffffffff),
        ),
      ),
      badgePositionPercentageOffset: .80);
}).toList();

}