简体   繁体   中英

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

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(),
  ),

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);
      });
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM