【问题标题】:Having issue while changing values in pie chart flutter在饼图颤动中更改值时出现问题
【发布时间】:2020-09-13 01:01:46
【问题描述】:

我第一次尝试了这段代码,它工作正常。

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】:

    您在这里所做的只是更改变量的值,而不是地图内的值。地图的值是 a = 2,而不是对 a 的引用。

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

    要更改地图中碳水化合物的值,您需要直接从地图本身更改它,例如 datamap["Carbs"] = newA。 b和c也一样

    如果这不起作用,请告诉我

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2016-01-25
      • 1970-01-01
      • 2020-12-17
      • 2020-09-06
      相关资源
      最近更新 更多