forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_render_box.dart
59 lines (48 loc) · 1.69 KB
/
custom_render_box.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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 RenderDots extends RenderConstrainedBox {
RenderDots() : super(additionalConstraints: const BoxConstraints.expand());
// Makes this render box hittable so that we'll get pointer events.
@override
bool hitTestSelf(Offset position) => true;
final Map<int, Offset> _dots = <int, Offset>{};
@override
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();
}
}
@override
void paint(PaintingContext context, Offset offset) {
final Canvas canvas = context.canvas;
canvas.drawRect(offset & size, new Paint()..color = const Color(0xFF0000FF));
final Paint paint = new Paint()..color = const Color(0xFF00FF00);
for (Offset point in _dots.values)
canvas.drawCircle(point, 50.0, paint);
super.paint(context, offset);
}
}
class Dots extends SingleChildRenderObjectWidget {
const Dots({ Key key, Widget child }) : super(key: key, child: child);
@override
RenderDots createRenderObject(BuildContext context) => new RenderDots();
}
void main() {
runApp(
const Directionality(
textDirection: TextDirection.ltr,
child: const Dots(
child: const Center(
child: const Text('Touch me!'),
),
),
),
);
}