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

761

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);
      });
    }
  }
Share:
761
Ramin Bateni
Author by

Ramin Bateni

I'm a idea maker &amp; full stack developer who like using both of art &amp; logic beside together to create innovations!😎 Since 2011, I have played in various roles (cto, team lead, full stack developer, product manager, founder, consultant &amp;...), while I started designing applications &amp; programming since 2004 and I was doing art design for a long time before it. - Web Developing ASP.NET MVC Core / C#.NET / Rest API / EF / Code First HTML5 / CSS3 / JavaScript / jQuery / Typescript Next.js / Vue.js / Vuex / Nuxt.js Nest.js / Node.js / Chat Bot SQL Server / Redis / MongoDB and more..... - Desktop App: C# WPF Electron - Creativity, Idea making &amp; growing Startups - Graphic - Branding, Digital Marketing

Updated on December 24, 2022

Comments

  • Ramin Bateni
    Ramin Bateni over 1 year

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