完整代码
import 'dart:math';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
class BarChartScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return BarChartState();
}
}
class BarChartState extends State<BarChartScreen> {
late List data = [];
@override
void initState() {
super.initState();
var rand = Random();
for (int i = 0; i < 5; i++) {
data.add({
"yAxis": rand.nextDouble() * 10, "xAxis": i});
}
}
@override
Widget build(BuildContext context) {
return Container(
height: 300,
child: BarChart(
BarChartData(
alignment: BarChartAlignment.center,
maxY: 10,
groupsSpace: 16,
barGroups: data.map((e) {
return BarChartGroupData(
x: e["xAxis"],
barsSpace: 3,
showingTooltipIndicators: [0],
barRods: [
BarChartRodData(
y: e[