更改饼图颤动中的值时出现问题

问题描述 投票:0回答:1

我第一次尝试了此代码,并且可以正常工作。

double a = 2, b = 3, c = 5;
var color;
Map<String, double> dataMap = Map();
List<Color> colorList = [
  Colors.red,
  Colors.green,
  Colors.yellow,
];

void changeGraph() {
  dataMap.putIfAbsent("Fat", () => c);
  dataMap.putIfAbsent("Protein", () => b);
  dataMap.putIfAbsent("Carbs", () => a);
}

void initState() {
  super.initState();
  changeGraph();
}

PieChart(
  dataMap: dataMap,
  animationDuration: Duration(milliseconds: 800),
  chartLegendSpacing: 32.0,
  chartRadius: MediaQuery.of(context).size.width / 2.7,
  showChartValuesInPercentage: true,
  showChartValues: true,
  showChartValuesOutside: false,
  chartValueBackgroundColor: Colors.grey[200],
  colorList: colorList,
  showLegends: true,
  legendPosition: LegendPosition.right,
  decimalPlaces: 1,
  showChartValueLabel: true,
  initialAngle: 0,
  chartValueStyle: defaultChartValueStyle.copyWith(
    color: Colors.blueGrey[900].withOpacity(0.9),
  ),
  chartType: ChartType.disc,
)

然后从用户那里获取值后,我尝试了这种方法来更改图形

setState(() {
  a = newA;
  b = newB;
  c = newC;
});

我也尝试调用changeGraph()方法,但是图形没有变化,并且显示了它第一次显示的值。有什么方法可以更改值吗?

flutter dart flutter-layout flutter-animation
1个回答
0
投票

您在这里所做的是仅更改变量的值,而不更改映射内的值。映射具有a = 2的值,而不是对a的引用。

意思是当您说dataMap.putIfAbsent("Carbs", () => a);时,“ Carbs”的值不是a,但实际上是2,因为此处的值是int而不是引用。

为了更改地图中Carb的值,您需要通过执行datamap["Carbs"] = newA从地图本身直接更改它的值。 b和c也一样

让我知道是否行不通

© www.soinside.com 2019 - 2024. All rights reserved.