paint_event.dart 1.3 KB
Newer Older
Kevin's avatar
Kevin committed
1 2 3 4 5 6 7
part of cool_ui;

typedef PaintCallback = void Function(PaintingContext context, Offset offset,Size size);

class PaintEvent extends SingleChildRenderObjectWidget{


Kevin's avatar
Kevin committed
8 9
  final PaintCallback? paintBefore;
  final PaintCallback? paintAfter;
Kevin's avatar
Kevin committed
10 11 12


  const PaintEvent({
Kevin's avatar
Kevin committed
13
    Key? key,
Kevin's avatar
Kevin committed
14 15
    this.paintBefore,
    this.paintAfter,
Kevin's avatar
Kevin committed
16
    Widget? child
Kevin's avatar
Kevin committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  }) :
        super(key: key, child: child);
  @override
  PaintEventProxyBox createRenderObject(BuildContext context) {
    return PaintEventProxyBox(
      paintAfter: paintAfter,
      paintBefore: paintBefore
    );
  }

  @override
  void updateRenderObject(BuildContext context, PaintEventProxyBox renderObject) {
    renderObject..paintAfter = paintBefore
          ..paintAfter = paintAfter;
  }

}


class PaintEventProxyBox extends RenderProxyBox{
Kevin's avatar
Kevin committed
37 38
  PaintCallback? paintBefore;
  PaintCallback? paintAfter;
Kevin's avatar
Kevin committed
39 40

  PaintEventProxyBox({
Kevin's avatar
Kevin committed
41
    RenderBox? child,
Kevin's avatar
Kevin committed
42 43 44 45 46 47 48 49 50 51 52 53 54
    this.paintBefore,
    this.paintAfter
  }):super(child);

  @override
  void detach() {
    super.detach();
    markNeedsPaint();
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    if(this.paintBefore != null){
Kevin's avatar
Kevin committed
55
      this.paintBefore!(context,offset,size);
Kevin's avatar
Kevin committed
56 57 58 59 60 61 62
    }

    super.paint(context, offset);



    if(this.paintAfter != null){
Kevin's avatar
Kevin committed
63
      this.paintAfter!(context,offset,size);
Kevin's avatar
Kevin committed
64 65 66 67
    }

  }
}