custom_render_box.dart 1.54 KB
Newer Older
1 2 3 4 5 6 7
// 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';

8 9
class RenderDots extends RenderConstrainedBox {
  RenderDots() : super(additionalConstraints: const BoxConstraints.expand());
10 11

  // Makes this render box hittable so that we'll get pointer events.
12
  @override
13 14 15 16
  bool hitTestSelf(Point position) => true;

  final Map<int, Point> _dots = <int, Point>{};

17
  @override
18 19 20 21 22 23 24 25 26 27
  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();
    }
  }

28
  @override
29 30
  void paint(PaintingContext context, Offset offset) {
    final Canvas canvas = context.canvas;
31
    canvas.drawRect(offset & size, new Paint()..color = new Color(0xFF0000FF));
32

33
    Paint paint = new Paint()..color = new Color(0xFF00FF00);
34 35
    for (Point point in _dots.values)
      canvas.drawCircle(point, 50.0, paint);
36 37

    super.paint(context, offset);
38 39 40
  }
}

41
class Dots extends SingleChildRenderObjectWidget {
42
  Dots({ Key key, Widget child }) : super(key: key, child: child);
43 44

  @override
45
  RenderDots createRenderObject(BuildContext context) => new RenderDots();
46 47 48
}

void main() {
49
  runApp(new Dots(child: new Center(child: new Text('Touch me!'))));
50
}