Flutter, How to create a clickable bar in a BarChart widget?

╄→尐↘猪︶ㄣ 提交于 2020-11-29 10:17:07

问题


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?

image

/// 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!