Robo-advisory-dart/lib/screens/home/local_widgets/expenses_chart.dart

108 lines
3.4 KiB
Dart

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:robo_advisory/providers/transaction_provider.dart';
import 'package:robo_advisory/providers/user_provider.dart';
class ExpensesChart extends StatefulWidget {
@override
State<StatefulWidget> createState() => ExpensesChartState();
}
class ExpensesChartState extends State {
int touchedIndex;
@override
Widget build(BuildContext context) {
List<String> categories =
Provider.of<UserProvider>(context, listen: true).categories;
return Row(
children: <Widget>[
Expanded(
child: PieChart(
PieChartData(
pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
if (pieTouchResponse.touchInput is FlLongPressEnd ||
pieTouchResponse.touchInput is FlPanEnd) {
touchedIndex = -1;
} else {
touchedIndex = pieTouchResponse.touchedSectionIndex;
if (touchedIndex != null) {
Provider.of<UserProvider>(context, listen: false)
.setSelectedTabIndex(3);
Provider.of<TransactionProvider>(context, listen: false)
.setSelectedCategory(categories[touchedIndex + 1]);
}
}
});
}),
borderData: FlBorderData(
show: false,
),
sectionsSpace: 1,
centerSpaceRadius: 70,
sections: showingSections()),
),
),
],
);
}
List<PieChartSectionData> showingSections() {
return List.generate(4, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
switch (i) {
case 0:
return PieChartSectionData(
color: const Color(0xff0293ee),
value: 50,
title: '50%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
case 1:
return PieChartSectionData(
color: const Color(0xfff8b250),
value: 12,
title: '12%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
case 2:
return PieChartSectionData(
color: const Color(0xff13d38e),
value: 13,
title: '13%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
case 3:
return PieChartSectionData(
color: const Color(0xff845bef),
value: 25,
title: '25%',
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
default:
return null;
}
});
}
}