问题
I have a flutter App and need a Bar chart in which I can click bars to open new page with related information. How should I do it?
/// Bar chart example
charts.BarChart(
createSampleData(),
animate: true,
barGroupingType: charts.BarGroupingType.stacked,
barRendererDecorator:
new charts.BarLabelDecorator<String>(),
domainAxis: new charts.OrdinalAxisSpec(),
),
回答1:
Use selectionModels
array property of BarChart
Widget:
new charts.BarChart(
createSampleData(),
animate: true,
barGroupingType: charts.BarGroupingType.stacked,
selectionModels: [
new charts.SelectionModelConfig(
type: charts.SelectionModelType.info,
changedListener: _onSelectionChanged,
)
],
barRendererDecorator:
new charts.BarLabelDecorator<String>(),
domainAxis: new charts.OrdinalAxisSpec(),
),
_onSelectionChanged(charts.SelectionModel model) {
final selectedDatum = model.selectedDatum;
if (selectedDatum.isNotEmpty) {
setState(() {
print(selectedDatum.first.datum.sales);
});
}
}
来源:https://stackoverflow.com/questions/63937085/flutter-how-to-create-a-clickable-bar-in-a-barchart-widget