import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; class PieChartWidget extends StatelessWidget { const PieChartWidget({ required this.sections, this.title, this.centerText, this.showLegend = true, this.legendItems, super.key, }); final List sections; final String? title; final String? centerText; final bool showLegend; final List? legendItems; @override Widget build(BuildContext context) { final theme = Theme.of(context); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (title != null) Padding( padding: const EdgeInsets.only(bottom: 16), child: Text( title!, style: theme.textTheme.titleMedium?.copyWith( fontWeight: FontWeight.bold, ), ), ), Expanded( child: Row( children: [ Expanded( flex: 2, child: PieChart( PieChartData( sections: sections, centerSpaceRadius: 40, sectionsSpace: 2, ), ), ), if (showLegend && legendItems != null) Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: legendItems! .map( (item) => Padding( padding: const EdgeInsets.symmetric(vertical: 4), child: Row( children: [ Container( width: 12, height: 12, decoration: BoxDecoration( color: item.color, borderRadius: BorderRadius.circular(3), ), ), const SizedBox(width: 8), Expanded( child: Text( item.label, style: theme.textTheme.bodySmall, overflow: TextOverflow.ellipsis, ), ), ], ), ), ) .toList(), ), ), ], ), ), ], ); } } class LegendItem { const LegendItem({ required this.label, required this.color, }); final String label; final Color color; }