Commit f4d784dc authored by Adam Barth's avatar Adam Barth

Add an example of creating a custom RenderBox

parent 75bc0277
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class _CustomRenderBox extends RenderConstrainedBox {
_CustomRenderBox() : super(additionalConstraints: const BoxConstraints.expand());
// Makes this render box hittable so that we'll get pointer events.
bool hitTestSelf(Point position) => true;
final Map<int, Point> _dots = <int, Point>{};
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
if (event is PointerDownEvent || event is PointerMoveEvent) {
_dots[event.pointer] = event.position;
markNeedsPaint();
} else if (event is PointerUpEvent || event is PointerCancelEvent) {
_dots.remove(event.pointer);
markNeedsPaint();
}
}
void paint(PaintingContext context, Offset offset) {
final Canvas canvas = context.canvas;
canvas.drawRect(offset & size, new Paint()..color = new Color(0xFF00FF00));
Paint paint = new Paint()..color = new Color(0xFF0000FF);
for (Point point in _dots.values)
canvas.drawCircle(point, 50.0, paint);
}
}
class _CustomRenderBoxWidget extends OneChildRenderObjectWidget {
_CustomRenderBox createRenderObject() => new _CustomRenderBox();
}
void main() {
runApp(new _CustomRenderBoxWidget());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment