shouldRepaint method

  1. @override
  2. @mustCallSuper
bool shouldRepaint(
  1. covariant MorphingText oldDelegate
)
inherited

Called whenever a new instance of the custom painter delegate class is provided to the RenderCustomPaint object, or any time that a new CustomPaint object is created with a new instance of the custom painter delegate class (which amounts to the same thing, because the latter is implemented in terms of the former).

If the new instance represents different information than the old instance, then the method should return true, otherwise it should return false.

If the method returns false, then the paint call might be optimized away.

It's possible that the paint method will get called even if shouldRepaint returns false (e.g. if an ancestor or descendant needed to be repainted). It's also possible that the paint method will get called without shouldRepaint being called at all (e.g. if the box changes size).

If a custom delegate has a particularly expensive paint function such that repaints should be avoided as much as possible, a RepaintBoundary or RenderRepaintBoundary (or other render object with RenderObject.isRepaintBoundary set to true) might be helpful.

The oldDelegate argument will never be null.

Implementation

@override
@mustCallSuper
bool shouldRepaint(MorphingText oldDelegate) {
  String oldFrameText = oldDelegate.text;
  if (oldFrameText == text) {
    this._oldText = oldDelegate._oldText;
    this._oldTextProperties = oldDelegate._oldTextProperties;
    this._textProperties = oldDelegate._textProperties;
    // shouldn't repaint if there is not change in progress
    if (this.progress == oldDelegate.progress) {
      return false;
    }
  } else {
    this._oldText = oldDelegate.text;
    // calculate text info for prev and current text
    _calculateTextProperties(text, _textProperties);
    _calculateTextProperties(_oldText!, _oldTextProperties);
    // calculate which text will move to which position
    _calculateMove();
  }
  return true;
}