KonectarEvents/lib/widgets/piechart.dart

231 lines
8.2 KiB
Dart

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:konectar_events/model/specialtymodel.dart';
import 'package:konectar_events/utils/appcolors.dart';
import 'package:konectar_events/widgets/indicators.dart';
class CustomPieChart extends StatefulWidget {
List<Specialty> specialtyList;
CustomPieChart({super.key, required this.specialtyList});
@override
State<StatefulWidget> createState() => CustomPieChartState();
}
class CustomPieChartState extends State<CustomPieChart> {
int touchedIndex = -1;
@override
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 1.3,
child: Column(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1.9,
child: PieChart(
PieChartData(
pieTouchData: PieTouchData(
touchCallback: (FlTouchEvent event, pieTouchResponse) {
setState(() {
if (!event.isInterestedForInteractions ||
pieTouchResponse == null ||
pieTouchResponse.touchedSection == null) {
touchedIndex = -1;
return;
}
touchedIndex = pieTouchResponse
.touchedSection!.touchedSectionIndex;
});
},
),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 0,
centerSpaceRadius: 60,
sections: showingSections(widget.specialtyList),
),
),
),
),
// SizedBox(
// height: 5,
// ),
Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: List.generate(widget.specialtyList.length, (i) {
return Indicator(
size: 10,
color: AppColors().appcolors[i],
text: widget.specialtyList[i].specialtyName,
isSquare: true,
);
})
// <Widget>[
// Indicator(
// size: 8,
// color: AppColors.contentColorBlue,
// text: 'Medical Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorYellow,
// text: 'Hematology/Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorPurple,
// text: 'Internal Medicine',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorGreen,
// text: 'Radiation Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 2,
// ),
// Indicator(
// size: 8,
// color: AppColors.contentColorOrange,
// text: 'Pediatric Hematology/Oncology',
// isSquare: true,
// ),
// SizedBox(
// height: 5,
// ),
// ],
),
),
const SizedBox(
width: 9,
),
],
),
);
}
List<PieChartSectionData> showingSections(List<Specialty> specialtyList) {
double total = 0.0;
for (var obj in specialtyList) {
total += double.parse(obj.specialtyCount);
}
return List.generate(specialtyList.length, (i) {
final isTouched = i == touchedIndex;
final fontSize = isTouched ? 14.0 : 12.0;
final radius = isTouched ? 60.0 : 50.0;
const shadows = [Shadow(color: Colors.black, blurRadius: 2)];
return PieChartSectionData(
color: AppColors().appcolors[i],
value: ((double.parse(specialtyList[i].specialtyCount) / total) * 100)
.roundToDouble(),
title:
'${((double.parse(specialtyList[i].specialtyCount) / total) * 100).roundToDouble()}%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
// switch (i) {
// case 0:
// return PieChartSectionData(
// color: AppColors.contentColorBlue,
// value:
// (double.parse(specialtyList[i].specialtyCount) * total) / 100,
// title:
// '${(double.parse(specialtyList[i].specialtyCount) * total) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 1:
// return PieChartSectionData(
// color: AppColors.contentColorYellow,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 2:
// return PieChartSectionData(
// color: AppColors.contentColorPurple,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 3:
// return PieChartSectionData(
// color: AppColors.contentColorGreen,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount) / 100}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// case 4:
// return PieChartSectionData(
// color: AppColors.contentColorOrange,
// value: double.parse(specialtyList[i].specialtyCount) / 100,
// title: '${double.parse(specialtyList[i].specialtyCount)}%',
// radius: radius,
// titleStyle: TextStyle(
// fontSize: fontSize,
// fontWeight: FontWeight.bold,
// color: AppColors.mainTextColor1,
// shadows: shadows,
// ),
// );
// default:
// throw Error();
// }
});
}
}