首页 > 解决方案 > Flutter Repaint 边界重绘规则

问题描述

每次方法中的变量_lastIndex更改时,我都想重新绘制我的 RepaintBoundary 小部件的孩子setState()。基本上,我希望 RepaintBoundary 的行为与适用于任何普通 Container 的行为相同 - 但出于不相关的原因,我需要 RepaintBoundary 而不是 Container。我玩了一下,发现在第一个示例中发生更改时,子 ( MyPainter) 会重新渲染_lastIndex,而在第二个示例中则不会。它背后的规则是什么让这两个例子的行为如此不同?

1.例子

       RepaintBoundary(
          key: globalKey,
          child: Container(
          child: Container(
            child: new CustomPaint(
              child: Text(_lastIndex.toString()), // MyPainter is re-rendered because of this line
              painter: new MyPainter(index: _lastIndex), // while this line doesn't have any effect on re-rendering
              isComplex: true,
              willChange: false,
          ),
         )
        )
      ),

2.例子

 RepaintBoundary(
          key: globalKey,
          child: Container(
          child: Container(
            child: new CustomPaint(
              painter: new MyPainter(index: _lastIndex),
              isComplex: true,
              willChange: false,
          ),
         )
        )
      ),

标签: flutterdartflutter-canvas

解决方案


推荐阅读