How to ensure my CustomPaint widget painting is stored in the raster cache?

后端 未结 1 1586
生来不讨喜
生来不讨喜 2020-12-04 18:51

I have an app that displays a black dot at the point where the user touches the screen like this:

The black dot can be moved by the user as he/she drags his

相关标签:
1条回答
  • 2020-12-04 19:16

    It's a specificity of Flutter. We are not in React, where "Components" are repainted only when their state/props change.

    In Flutter, every time a widget has to repaint the whole tree will too.

    Usually, this is not a problem and fairly fast. But in some cases (such as yours), you don't want that. And this is where a fairly undocumented but important widget appears! RepaintBoundary

    There's an excellent talk about how Flutter's rendering pipeline works, here: https://www.youtube.com/watch?v=UUfXWzp0-DU

    But in short, consider RepaintBoundary as what tells Flutter to split the painting operation into different parts.

    Anyway, the solution ? Wrap your Expensive widget in a RepaintBoundary. And suddenly you get 60 FPS.

          new RepaintBoundary(
            child: new CustomPaint(
              painter: new ExpensivePainter(),
              isComplex: true,
              willChange: false,
            ),
          ),
    

    0 讨论(0)
提交回复
热议问题