flutter_circular_chart/example/animated_radial_chart_example.dart

125 lines
3.3 KiB
Dart
Raw Normal View History

2017-11-03 01:57:39 +00:00
import 'package:flutter/material.dart';
2021-03-08 17:37:23 +00:00
import 'package:flutter_circular_chart_two/flutter_circular_chart.dart';
2017-11-03 01:57:39 +00:00
void main() {
2021-03-08 17:37:23 +00:00
runApp(MaterialApp(
home: AnimatedRadialChartExample(),
2017-11-03 01:57:39 +00:00
));
}
class AnimatedRadialChartExample extends StatefulWidget {
@override
2021-03-08 17:37:23 +00:00
_AnimatedRadialChartExampleState createState() => _AnimatedRadialChartExampleState();
2017-11-03 01:57:39 +00:00
}
2021-03-08 17:37:23 +00:00
class _AnimatedRadialChartExampleState extends State<AnimatedRadialChartExample> {
final GlobalKey<AnimatedCircularChartState> _chartKey = GlobalKey<AnimatedCircularChartState>();
2017-11-03 01:57:39 +00:00
final _chartSize = const Size(200.0, 200.0);
double value = 50.0;
2018-03-21 00:36:48 +00:00
Color labelColor = Colors.blue[200];
2017-11-03 01:57:39 +00:00
void _increment() {
setState(() {
value += 10;
List<CircularStackEntry> data = _generateChartData(value);
_chartKey.currentState.updateData(data);
});
}
void _decrement() {
setState(() {
value -= 10;
List<CircularStackEntry> data = _generateChartData(value);
_chartKey.currentState.updateData(data);
});
}
List<CircularStackEntry> _generateChartData(double value) {
Color dialColor = Colors.blue[200];
if (value < 0) {
dialColor = Colors.red[200];
} else if (value < 50) {
dialColor = Colors.yellow[200];
}
2018-03-21 00:36:48 +00:00
labelColor = dialColor;
2017-11-03 01:57:39 +00:00
List<CircularStackEntry> data = <CircularStackEntry>[
2021-03-08 17:37:23 +00:00
CircularStackEntry(
2017-11-03 01:57:39 +00:00
<CircularSegmentEntry>[
2021-03-08 17:37:23 +00:00
CircularSegmentEntry(
2017-11-03 01:57:39 +00:00
value,
dialColor,
rankKey: 'percentage',
)
],
rankKey: 'percentage',
),
];
if (value > 100) {
2018-03-21 00:36:48 +00:00
labelColor = Colors.green[200];
2021-03-08 17:37:23 +00:00
data.add(CircularStackEntry(
2017-11-03 01:57:39 +00:00
<CircularSegmentEntry>[
2021-03-08 17:37:23 +00:00
CircularSegmentEntry(
2017-11-03 01:57:39 +00:00
value - 100,
Colors.green[200],
rankKey: 'percentage',
),
],
rankKey: 'percentage2',
));
}
return data;
}
@override
Widget build(BuildContext context) {
2021-03-08 17:37:23 +00:00
TextStyle _labelStyle =
Theme.of(context).textTheme.headline6.merge(TextStyle(color: labelColor));
2018-03-21 00:36:48 +00:00
2021-03-08 17:37:23 +00:00
return Scaffold(
appBar: AppBar(
2017-11-03 01:57:39 +00:00
title: const Text('Percentage Dial'),
),
2021-03-08 17:37:23 +00:00
body: Column(
2017-11-03 01:57:39 +00:00
children: <Widget>[
2021-03-08 17:37:23 +00:00
Container(
child: AnimatedCircularChart(
2017-11-03 01:57:39 +00:00
key: _chartKey,
size: _chartSize,
initialChartData: _generateChartData(value),
chartType: CircularChartType.Radial,
2018-05-09 20:15:34 +00:00
edgeStyle: SegmentEdgeStyle.round,
2017-11-03 01:57:39 +00:00
percentageValues: true,
2018-03-21 00:36:48 +00:00
holeLabel: '$value%',
labelStyle: _labelStyle,
2017-11-03 01:57:39 +00:00
),
),
2021-03-08 17:37:23 +00:00
Row(
2017-11-03 01:57:39 +00:00
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
2021-03-08 17:37:23 +00:00
RaisedButton(
2018-03-21 00:36:48 +00:00
onPressed: _decrement,
child: const Icon(Icons.remove),
shape: const CircleBorder(),
color: Colors.red[200],
textColor: Colors.white,
),
2021-03-08 17:37:23 +00:00
RaisedButton(
2018-03-21 00:36:48 +00:00
onPressed: _increment,
child: const Icon(Icons.add),
shape: const CircleBorder(),
color: Colors.blue[200],
textColor: Colors.white,
),
2017-11-03 01:57:39 +00:00
],
),
],
),
);
}
}