gpt4 book ai didi

flutter - CustomPainter 使用 Listenable 重绘

转载 作者:IT王子 更新时间:2023-10-29 07:15:04 45 4
gpt4 key购买 nike

CustomPainter 类似乎有几种触发重绘的方法。

我已经让我的 painter 使用 shouldRepaint 方法,但是,我希望我的 painter 对 listenable 中的变化使用react,而不是轮询更改。

Flutter 文档指出

The most efficient way to trigger a repaint is to either:

Extend this class and supply a repaint argument to the constructor of > the CustomPainter, where that object notifies its listeners when it is time to repaint. Extend Listenable (e.g. via ChangeNotifier) and implement CustomPainter, so that the object itself provides the notifications directly.

我已经尝试将可听对象传递给自定义画家,但是当可听对象更新时,没有按照文档中的说明调用绘画方法

In either case, the CustomPaint widget or RenderCustomPaint render object will listen to the Listenable and repaint whenever the animation ticks,

class CursorPainter extends CustomPainter {
Listenable _repaint;
Player player;
BuildContext context;

// Pass in repaint (listenable)
CursorPainter({repaint, this.context}) {
_repaint = repaint;
player = Provider.of<Player>(context, listen: false);
}

@override
void paint(Canvas canvas, Size size) {
// Paint logic...
}

@override
bool shouldRepaint(CursorPainter lastTrackPainter) {
// Tried returning both true and false here to no avail. Method is continually called.
}
}


我希望每次可监听发生变化并调用 notifyListeners() 时,CustomPainter 都会按照文档中的说明重新绘制自身。

最佳答案

在你的构造函数中,调用super,...

  CursorPainter({Listenable repaint, this.context}) : super(repaint: repaint) {
_repaint = repaint;
player = Provider.of<Player>(context, listen: false);
}

关于flutter - CustomPainter 使用 Listenable 重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57633554/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com