okane/lib/ui/widgets/piechart.dart

85 lines
2.3 KiB
Dart

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:okane/ui/pages/account/breakdown_card.dart';
typedef OkanePieChartSection = ({String title, double value, Color color});
typedef OkanePieChartValueConverter = String Function(double);
String numToString(double input) {
return input.toString();
}
class OkanePieChart extends StatelessWidget {
// Width of the pie chart
final double width;
// Height of the pie chart
final double height;
final List<OkanePieChartSection> items;
final OkanePieChartValueConverter valueConverter;
const OkanePieChart({
required this.items,
this.valueConverter = numToString,
this.width = 150,
this.height = 150,
super.key,
});
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.all(16),
child: SizedBox(
width: width,
height: height,
child: AspectRatio(
aspectRatio: 1,
child: PieChart(
PieChartData(
borderData: FlBorderData(show: false),
sectionsSpace: 5,
centerSpaceRadius: 35,
sections:
items
.map(
(i) => PieChartSectionData(
value: i.value,
title: valueConverter(i.value),
titleStyle: TextStyle(
fontWeight: FontWeight.bold,
),
radius: 40,
color: i.color,
),
)
.toList(),
),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children:
items
.map((i) => LegendItem(text: i.title, color: i.color))
.toList(),
),
),
],
);
}
}