Created
September 27, 2020 20:53
-
-
Save slightfoot/a79cd1edcf02dce45c80193a027093c6 to your computer and use it in GitHub Desktop.
Custom Debug Paint - Paint custom graphics only in when show debug paint is enabled in the inspector - by Simon Lightfoot - 27/09/2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2020 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
typedef CustomDebugPaintFn = void Function(Canvas canvas, Size size); | |
class CustomDebugPaint extends SingleChildRenderObjectWidget { | |
const CustomDebugPaint({ | |
Key key, | |
this.paint, | |
Widget child, | |
}) : super(key: key, child: child); | |
final CustomDebugPaintFn paint; | |
@override | |
RenderObject createRenderObject(BuildContext context) { | |
return RenderCustomDebugPaint(painter: paint); | |
} | |
@override | |
void updateRenderObject(BuildContext context, covariant RenderCustomDebugPaint renderObject) { | |
super.updateRenderObject(context, renderObject); | |
renderObject.painter = paint; | |
} | |
} | |
class RenderCustomDebugPaint extends RenderProxyBox { | |
RenderCustomDebugPaint({CustomDebugPaintFn painter, RenderBox child}) | |
: _painter = painter, | |
super(child); | |
CustomDebugPaintFn _painter; | |
CustomDebugPaintFn get painter => _painter; | |
set painter(CustomDebugPaintFn value) { | |
if (_painter != value) { | |
_painter = value; | |
markNeedsPaint(); | |
} | |
} | |
@override | |
void debugPaint(PaintingContext context, Offset offset) { | |
super.debugPaint(context, offset); | |
if (debugPaintSizeEnabled) { | |
_painter?.call(context.canvas, size); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2020 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter/material.dart'; | |
import 'custom_debug_paint.dart'; | |
void main() => runApp(TestApp()); | |
class TestApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Material( | |
child: CustomDebugPaint( | |
paint: (Canvas canvas, Size size) { | |
canvas.drawCircle( | |
(Offset.zero & size).center, | |
size.shortestSide / 3, | |
Paint()..color = Colors.red.withOpacity(0.2), | |
); | |
}, | |
child: Center( | |
child: Text('hello world'), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment