使用gauge.dart
文件from the example here,我刚刚添加了一个changedListener
回调来获取用户单击了哪个段。
仅对4个分段中的3个按预期方式起作用,但是如果单击最后一个分段,则在其区域末尾触发时会触发异常(取决于示例中给出的随机值,如果相对小段,然后整个段会给出相同的错误)。
static List<charts.Series<GaugeSegment, String>> _createRandomData() {
final random = new Random();
final data = [
new GaugeSegment('Low', random.nextInt(100)),
new GaugeSegment('Acceptable', random.nextInt(100)),
new GaugeSegment('High', random.nextInt(100)),
new GaugeSegment('Highly Unusual', random.nextInt(100)),
];
return [
new charts.Series<GaugeSegment, String>(
id: 'Segments',
domainFn: (GaugeSegment segment, _) => segment.segment,
measureFn: (GaugeSegment segment, _) => segment.size,
data: data,
)
];
}
@override
Widget build(BuildContext context) {
return new charts.PieChart(seriesList,
animate: animate,
// added selectionModels to pass in _onSelectionChanged callback
selectionModels: [
new charts.SelectionModelConfig(
type: charts.SelectionModelType.info,
changedListener: _onSelectionChanged,
)
],
defaultRenderer: new charts.ArcRendererConfig(
arcWidth: 30, startAngle: 4 / 5 * pi, arcLength: 7 / 5 * pi));
}
_onSelectionChanged(charts.SelectionModel model) {
final selectedDatum = model.selectedDatum;
print("DATUM: ${selectedDatum.first.datum}");
if (selectedDatum.isNotEmpty) {
print('IS_NOT_EMPTY');
}
}
会给这个例外
The following StateError was thrown while handling a gesture:
Bad state: No element
When the exception was thrown, this was the stack
#0 List.first (dart:core-patch/array.dart:202:5)
#1 GaugeChart._onSelectionChanged
package:example/pie_chart/gauge.dart:89
#2 MutableSelectionModel.updateSelection.<anonymous closure>
package:charts_common/…/selection_model/selection_model.dart:178
#3 List.forEach (dart:core-patch/growable_array.dart:283:8)
#4 MutableSelectionModel.updateSelection
package:charts_common/…/selection_model/selection_model.dart:178
...
Handler: "onTapUp"
Recognizer: TapGestureRecognizer#1d1a0
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(286.5, 261.0)
finalLocalPosition: Offset(278.5, 153.0)
sent tap down