Skip to content

Instantly share code, notes, and snippets.

@e200
Last active May 9, 2022 22:32
Show Gist options
  • Save e200/e71ca676f9bbe424a3bdc2a2fc5b2fa9 to your computer and use it in GitHub Desktop.
Save e200/e71ca676f9bbe424a3bdc2a2fc5b2fa9 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return
Container(
color: Colors.white,
padding: const EdgeInsets.all(15),
child: const Center(
child: SizedBox(
width: 150,
height: 150,
child: TestObject(color: Colors.blue),
),
),
);
}
}
class TestObject extends LeafRenderObjectWidget {
final Color color;
const TestObject({
Key? key,
required this.color,
}) : super(key: key);
@override
RenderObject createRenderObject(BuildContext context) {
return _RenderTestObject(color: color);
}
@override
void updateRenderObject(BuildContext context, _RenderTestObject renderObject) {
renderObject.color = color;
}
}
class _RenderTestObject extends RenderBox {
Color _color;
_RenderTestObject({
required Color color,
}) : _color = color;
Color get color => _color;
set color(Color color) {
_color = color;
markNeedsPaint();
}
@override
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
assert(debugHandleEvent(event, entry));
print(event);
}
@override
void performLayout() {
size = computeDryLayout(constraints);
}
@override
bool hitTestSelf(Offset position) => true;
@override
Size computeDryLayout(BoxConstraints constraints) {
final _size = Size(
constraints.maxWidth,
constraints.maxHeight,
);
return constraints.constrain(_size);
}
@override
void paint(PaintingContext context, Offset offset) {
final canvas = context.canvas;
canvas.translate(offset.dx, offset.dy);
canvas.save();
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(
0,
0,
constraints.maxWidth,
constraints.maxHeight,
),
Radius.circular(
constraints.maxHeight * .3,
),
),
Paint()..color = color.withOpacity(.3),
);
canvas.drawCircle(
Offset(
constraints.maxWidth / 2,
constraints.maxHeight / 2,
),
constraints.maxHeight / 3,
Paint()..color = color,
);
canvas.restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment