Chart Gallery
Loading preview
Starting the Flutter app
A showcase of Flutter's chart gallery functionality
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
const _barData = [0.6, 0.8, 0.5, 0.9, 0.7, 0.85];
const _labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
class ChartGallery extends StatelessWidget {
const ChartGallery({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Chart Gallery')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Bar Chart', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 16),
SizedBox(
height: 120,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: List.generate(_barData.length, (i) => Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: 32,
height: 80 * _barData[i],
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(4),
),
),
const SizedBox(height: 4),
Text(_labels[i],
style: Theme.of(context).textTheme.bodySmall),
],
)),
),
),
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Line Chart', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 16),
SizedBox(
height: 120,
child: CustomPaint(
painter: _LineChartPainter(data: _barData),
size: const Size(double.infinity, 120),
),
),
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Donut Chart', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 16),
Center(
child: SizedBox(
width: 120,
height: 120,
child: CustomPaint(
painter: _DonutChartPainter(
segments: [0.4, 0.3, 0.2, 0.1],
colors: [Colors.blue, Colors.green, Colors.orange, Colors.purple],
),
size: const Size(120, 120),
),
),
),
],
),
),
),
],
),
);
}
}
class _LineChartPainter extends CustomPainter {
final List<double> data;
_LineChartPainter({required this.data});
@override
void paint(Canvas canvas, Size size) {
if (data.isEmpty) return;
final paint = Paint()
..color = Colors.blue
..strokeWidth = 2
..style = PaintingStyle.stroke;
final w = size.width / (data.length - 1);
final points = data.asMap().entries.map((e) => Offset(
e.key * w,
size.height - e.value * size.height,
)).toList();
for (var i = 0; i < points.length - 1; i++) {
canvas.drawLine(points[i], points[i + 1], paint);
}
}
@override
bool shouldRepaint(_LineChartPainter oldDelegate) =>
!listEquals(oldDelegate.data, data);
}
class _DonutChartPainter extends CustomPainter {
final List<double> segments;
final List<Color> colors;
_DonutChartPainter({required this.segments, required this.colors});
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2 - 8;
var startAngle = -1.5708;
for (var i = 0; i < segments.length; i++) {
final sweep = 2 * 3.14159 * segments[i];
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius),
startAngle,
sweep,
false,
Paint()
..color = colors[i % colors.length]
..style = PaintingStyle.stroke
..strokeWidth = 16,
);
startAngle += sweep;
}
}
@override
bool shouldRepaint(_DonutChartPainter oldDelegate) =>
!listEquals(oldDelegate.segments, segments) ||
!listEquals(oldDelegate.colors, colors);
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
No chart package is involved; all three charts are hand-built. The bar chart is a Row of Containers with a height of 80 * _barData[i], while _LineChartPainter and _DonutChartPainter draw through CustomPaint. The line chart reuses _barData, and there are no axes, tooltips or animations.
Adapting it
- Feed
_barDataand_labelsfrom your own series, normalized first, because values are expected between 0 and 1. - Both painters compare old and new data with
listEqualsinshouldRepaint, so live values redraw from a stateful parent. _LineChartPainterhardcodesColors.blue; pass a color in so it follows the theme like the bars do. The donut’ssegmentsshould sum to 1.