Flutter,如何在 BarChart 小部件中创建可点击的栏?

RAM*_*RAM 2 charts bar-chart flutter

我有一个flutter应用程序,需要一个条形图,我可以在其中单击条形图来打开包含相关信息的新页面。我该怎么做呢?

图像

/// Bar chart example
charts.BarChart(
    createSampleData(),
    animate: true,
    barGroupingType: charts.BarGroupingType.stacked,
    barRendererDecorator:
        new charts.BarLabelDecorator<String>(),
    domainAxis: new charts.OrdinalAxisSpec(),
  ),
Run Code Online (Sandbox Code Playgroud)

小智 5

使用WidgetselectionModels的数组属性BarChart

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);
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)