Flutter - how to refresh different kind of widgets in a dashboard

Viewed 35

I'm developing a application that shows a dashboard: this dashboard is dynamically built and it is composed by some different kind of widget.

By a timed procedure it generates an async (REST) request and receive data (bloc-based). Parsing the data I need to trigger the refresh of some the widget's content.

I wrote an abstract class that extends StatefulWidget class to force a setValue() function and some other common features: all the widgets in the dashboard extends this class.

abstract class DashboardWidget extends StatefulWidget {

  final String? type;
  final dynamic settings;

  DashboardWidget({Key? key, this.settings, this.type}) : super(key: key);

  void setValue(dynamic newValue);
}

By the following code I'm able to set the dashboard and show the widget:

  for (var element in jsonPanes) {
    List<dynamic> w = element['widgets'];
    DashboardPane pane = DashboardPane( element['title'] );
    for (var owidget in w) {
      print("[dashboard] WIDGET: $owidget");

      // 
      DashboardWidget? widget;
      var oSettings = owidget['settings'];

      // 
      switch(owidget['type']){

        case 'value_wis_widget':
          widget = SigmaValueWisWidget(
            settings: SigmaValueWisWidgetSettings( oSettings['title'], oSettings['param'], oSettings['value'], oSettings['units'])
          );
          break;
... other cases ...

But in this way I must define the function on the widget's class, not in the relative state class, so I'm able to call the setValue() function but here I cannot call the setState() function to force the redraw procedure.

How can I set up the dashboard?

I read about the GlobalKey values: should I define them for every widget? How can I pass to the widgets?

0 Answers
Related