Files
fl_chart/example/lib/presentation/samples/pie/pie_chart_sample2.dart
zypherift c7e3f36b06
Some checks failed
Code Coverage / upload (push) Has been cancelled
Gh-Pages / build (push) Has been cancelled
Code Verification / verify (push) Has been cancelled
1.0.0
2025-08-09 18:17:34 +02:00

165 lines
4.8 KiB
Dart

import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart_app/presentation/widgets/indicator.dart';
import 'package:flutter/material.dart';
class PieChartSample2 extends StatefulWidget {
const PieChartSample2({super.key});
@override
State<StatefulWidget> createState() => PieChart2State();
}
class PieChart2State extends State {
int touchedIndex = -1;
@override
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 1.3,
child: Row(
children: <Widget>[
const SizedBox(
height: 18,
),
Expanded(
child: AspectRatio(
aspectRatio: 1,
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: 40,
sections: showingSections(),
),
),
),
),
const Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Indicator(
color: AppColors.contentColorBlue,
text: 'First',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: AppColors.contentColorYellow,
text: 'Second',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: AppColors.contentColorPurple,
text: 'Third',
isSquare: true,
),
SizedBox(
height: 4,
),
Indicator(
color: AppColors.contentColorGreen,
text: 'Fourth',
isSquare: true,
),
SizedBox(
height: 18,
),
],
),
const SizedBox(
width: 28,
),
],
),
);
}
List<PieChartSectionData> showingSections() {
return List.generate(4, (i) {
final isTouched = i == touchedIndex;
final fontSize = isTouched ? 25.0 : 16.0;
final radius = isTouched ? 60.0 : 50.0;
const shadows = [Shadow(color: Colors.black, blurRadius: 2)];
switch (i) {
case 0:
return PieChartSectionData(
color: AppColors.contentColorBlue,
value: 40,
title: '40%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
case 1:
return PieChartSectionData(
color: AppColors.contentColorYellow,
value: 30,
title: '30%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
case 2:
return PieChartSectionData(
color: AppColors.contentColorPurple,
value: 15,
title: '15%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
case 3:
return PieChartSectionData(
color: AppColors.contentColorGreen,
value: 15,
title: '15%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: AppColors.mainTextColor1,
shadows: shadows,
),
);
default:
throw Error();
}
});
}
}