|
| 1 | +import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_data.dart'; |
| 2 | +import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_renderer.dart'; |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | + |
| 5 | +class GaugeChart extends ImplicitlyAnimatedWidget { |
| 6 | + /// [data] determines how the [GaugeChart] should be look like, |
| 7 | + /// when you make any change in the [GaugeChartData], it updates |
| 8 | + /// new values with animation, and duration is [swapAnimationDuration]. |
| 9 | + /// also you can change the [swapAnimationCurve] |
| 10 | + /// which default is [Curves.linear]. |
| 11 | + const GaugeChart( |
| 12 | + this.data, { |
| 13 | + this.chartRendererKey, |
| 14 | + super.key, |
| 15 | + super.duration = const Duration(milliseconds: 150), |
| 16 | + super.curve = Curves.linear, |
| 17 | + }); |
| 18 | + |
| 19 | + /// Determines how the [GaugeChartData] should be look like. |
| 20 | + final GaugeChartData data; |
| 21 | + |
| 22 | + /// We pass this key to our renderers which are supposed to |
| 23 | + /// render the chart itself (without anything around the chart). |
| 24 | + final Key? chartRendererKey; |
| 25 | + |
| 26 | + /// Creates a [_GaugeChartState] |
| 27 | + @override |
| 28 | + _GaugeChartState createState() => _GaugeChartState(); |
| 29 | +} |
| 30 | + |
| 31 | +class _GaugeChartState extends AnimatedWidgetBaseState<GaugeChart> { |
| 32 | + /// we handle under the hood animations (implicit animations) via this tween, |
| 33 | + /// it lerps between the old [GaugeChartData] to the new one. |
| 34 | + GaugeChartDataTween? _gaugeChartDataTween; |
| 35 | + |
| 36 | + @override |
| 37 | + Widget build(BuildContext context) { |
| 38 | + final showingData = _getData(); |
| 39 | + |
| 40 | + return GaugeChartLeaf( |
| 41 | + data: _gaugeChartDataTween!.evaluate(animation), |
| 42 | + targetData: showingData, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + void forEachTween(TweenVisitor<dynamic> visitor) { |
| 48 | + _gaugeChartDataTween = visitor( |
| 49 | + _gaugeChartDataTween, |
| 50 | + widget.data, |
| 51 | + (dynamic value) => |
| 52 | + GaugeChartDataTween(begin: value as GaugeChartData, end: widget.data), |
| 53 | + ) as GaugeChartDataTween?; |
| 54 | + } |
| 55 | + |
| 56 | + GaugeChartData _getData() { |
| 57 | + return widget.data; |
| 58 | + } |
| 59 | +} |
0 commit comments