didUpdateWidget method
Called whenever the widget configuration changes.
If the parent widget rebuilds and request that this location in the tree update to display a new widget with the same runtimeType and Widget.key, the framework will update the widget property of this State object to refer to the new widget and then call this method with the previous widget as an argument.
Override this method to respond when the widget changes.
The framework always calls build after calling didUpdateWidget, which means any calls to setState in didUpdateWidget are redundant.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
Here it called whenever the series collection gets updated in SfCartesianChart.
Implementation
@override
void didUpdateWidget(SfCartesianChart oldWidget) {
_stateProperties.isRedrawByZoomPan = false;
_stateProperties.isLoadMoreIndicator = false;
_stateProperties.zoomProgress = false;
final List<CartesianSeriesRenderer> oldWidgetSeriesRenderers =
//ignore: prefer_spread_collections
<CartesianSeriesRenderer>[]..addAll(_stateProperties.seriesRenderers);
final List<CartesianSeriesRenderer> oldWidgetOldSeriesRenderers =
<CartesianSeriesRenderer>[]
//ignore: prefer_spread_collections
..addAll(_stateProperties.oldSeriesRenderers);
//Update and maintain the series state, when we update the series in the series collection //
_stateProperties.renderingDetails.chartTheme =
_updateThemeData(context, Theme.of(context), SfChartTheme.of(context));
_stateProperties.renderingDetails.themeData = Theme.of(context);
_createAndUpdateSeriesRenderer(
oldWidget, oldWidgetSeriesRenderers, oldWidgetOldSeriesRenderers);
needsRepaintChart(
_stateProperties,
_stateProperties.chartAxis.axisRenderersCollection,
oldWidgetSeriesRenderers);
_stateProperties.renderingDetails.isLegendToggled = false;
// ignore: unnecessary_null_comparison
if (_stateProperties.renderingDetails.legendWidgetContext != null &&
_stateProperties.renderingDetails.legendWidgetContext.isNotEmpty) {
_stateProperties.renderingDetails.legendWidgetContext.clear();
}
final SeriesRendererDetails? seriesRendererDetails =
_stateProperties.seriesRenderers.isNotEmpty
? SeriesHelper.getSeriesRendererDetails(
_stateProperties.seriesRenderers[0])
: null;
if (seriesRendererDetails?.selectionBehaviorRenderer != null) {
final SelectionDetails? selectionDetails =
SelectionHelper.getRenderingDetails(
seriesRendererDetails!.selectionBehaviorRenderer!);
if (_stateProperties.seriesRenderers.isNotEmpty &&
selectionDetails?.selectionRenderer != null) {
selectionDetails?.selectionRenderer?.isInteraction = false;
}
}
if (_stateProperties.isNeedUpdate) {
_stateProperties.renderingDetails.widgetNeedUpdate = true;
_stateProperties.oldSeriesRenderers = oldWidgetSeriesRenderers;
_getOldSeriesKeys(_stateProperties.oldSeriesRenderers);
_stateProperties.oldAxisRenderers = <ChartAxisRenderer>[]
//ignore: prefer_spread_collections
..addAll(_stateProperties.chartAxis.axisRenderersCollection);
}
super.didUpdateWidget(oldWidget);
}