Files

137 lines
4.7 KiB
Dart
Raw Permalink Normal View History

2019-11-27 15:04:02 -08:00
// Copyright 2014 The Flutter Authors. All rights reserved.
2016-02-13 00:52:56 -08:00
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
2016-02-13 14:30:37 -08:00
// This example shows how to use process input events in the underlying render
// tree.
import 'package:flutter/material.dart'; // Imported just for its color palette.
2016-02-13 00:52:56 -08:00
import 'package:flutter/rendering.dart';
// Material design colors. :p
List<Color> _kColors = <Color>[
Colors.teal,
Colors.amber,
Colors.purple,
Colors.lightBlue,
Colors.deepPurple,
Colors.lime,
2016-02-13 00:52:56 -08:00
];
/// A simple model object for a dot that reacts to pointer pressure.
class Dot {
2018-09-12 08:29:29 +02:00
Dot({ Color color }) : _paint = Paint()..color = color;
2016-02-13 00:52:56 -08:00
final Paint _paint;
2017-04-12 15:06:12 -07:00
Offset position = Offset.zero;
2016-02-13 00:52:56 -08:00
double radius = 0.0;
void update(PointerEvent event) {
position = event.position;
radius = 5 + (95 * event.pressure);
}
void paint(Canvas canvas, Offset offset) {
canvas.drawCircle(position + offset, radius, _paint);
}
}
/// A render object that draws dots under each pointer.
2016-02-13 14:30:37 -08:00
class RenderDots extends RenderBox {
RenderDots();
2016-02-13 00:52:56 -08:00
/// State to remember which dots to paint.
final Map<int, Dot> _dots = <int, Dot>{};
2016-02-13 14:30:37 -08:00
/// Indicates that the size of this render object depends only on the
/// layout constraints provided by the parent.
@override
2016-02-13 14:30:37 -08:00
bool get sizedByParent => true;
/// By selecting the biggest value permitted by the incoming constraints
2016-02-13 14:30:37 -08:00
/// during layout, this function makes this render object as large as
/// possible (i.e., fills the entire screen).
@override
2016-02-13 14:30:37 -08:00
void performResize() {
size = constraints.biggest;
}
2016-02-13 00:52:56 -08:00
/// Makes this render object hittable so that it receives pointer events.
@override
2017-04-12 15:06:12 -07:00
bool hitTestSelf(Offset position) => true;
2016-02-13 00:52:56 -08:00
/// Processes pointer events by mutating state and invalidating its previous
/// painting commands.
@override
2016-02-13 00:52:56 -08:00
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
if (event is PointerDownEvent) {
2019-12-05 21:33:07 +01:00
final Color color = _kColors[event.pointer.remainder(_kColors.length) as int];
2018-09-12 08:29:29 +02:00
_dots[event.pointer] = Dot(color: color)..update(event);
2016-02-13 14:30:37 -08:00
// We call markNeedsPaint to indicate that our painting commands have
// changed and that paint needs to be called before displaying a new frame
// to the user. It's harmless to call markNeedsPaint multiple times
// because the render tree will ignore redundant calls.
2016-02-13 00:52:56 -08:00
markNeedsPaint();
} else if (event is PointerUpEvent || event is PointerCancelEvent) {
_dots.remove(event.pointer);
markNeedsPaint();
} else if (event is PointerMoveEvent) {
_dots[event.pointer].update(event);
markNeedsPaint();
}
}
/// Issues new painting commands.
@override
2016-02-13 00:52:56 -08:00
void paint(PaintingContext context, Offset offset) {
final Canvas canvas = context.canvas;
2016-02-13 14:30:37 -08:00
// The "size" property indicates the size of that this render box was
2018-03-07 00:36:03 -05:00
// allotted during layout. Here we paint our bounds white. Notice that we're
2016-02-13 14:30:37 -08:00
// located at "offset" from the origin of the canvas' coordinate system.
// Passing offset during the render tree's paint walk is an optimization to
// avoid having to change the origin of the canvas's coordinate system too
// often.
2018-09-12 08:29:29 +02:00
canvas.drawRect(offset & size, Paint()..color = const Color(0xFFFFFFFF));
2016-02-13 14:30:37 -08:00
// We iterate through our model and paint each dot.
for (final Dot dot in _dots.values)
2016-02-13 00:52:56 -08:00
dot.paint(canvas, offset);
}
}
void main() {
2016-02-13 14:30:37 -08:00
// Create some styled text to tell the user to interact with the app.
2018-09-12 08:29:29 +02:00
final RenderParagraph paragraph = RenderParagraph(
2017-04-07 12:24:32 -07:00
const TextSpan(
style: TextStyle(color: Colors.black87),
2017-10-22 18:11:36 +02:00
text: 'Touch me!',
2017-09-07 16:57:38 -07:00
),
textDirection: TextDirection.ltr,
2016-02-13 00:52:56 -08:00
);
2016-02-13 14:30:37 -08:00
// A stack is a render object that layers its children on top of each other.
// The bottom later is our RenderDots object, and on top of that we show the
// text.
2018-09-12 08:29:29 +02:00
final RenderStack stack = RenderStack(
2017-09-07 22:52:36 -07:00
textDirection: TextDirection.ltr,
2016-02-13 00:52:56 -08:00
children: <RenderBox>[
2018-09-12 08:29:29 +02:00
RenderDots(),
2016-02-13 00:52:56 -08:00
paragraph,
2017-09-07 16:57:38 -07:00
],
2016-02-13 00:52:56 -08:00
);
2016-02-13 14:30:37 -08:00
// The "parentData" field of a render object is controlled by the render
// object's parent render object. Now that we've added the paragraph as a
// child of the RenderStack, the paragraph's parentData field has been
// populated with a StackParentData, which we can use to provide input to the
// stack's layout algorithm.
//
// We use the StackParentData of the paragraph to position the text in the top
// left corner of the screen.
2019-12-05 21:33:07 +01:00
final StackParentData paragraphParentData = paragraph.parentData as StackParentData;
2016-02-13 00:52:56 -08:00
paragraphParentData
..top = 40.0
..left = 20.0;
2016-02-13 14:30:37 -08:00
// Finally, we attach the render tree we've built to the screen.
2018-09-12 08:29:29 +02:00
RenderingFlutterBinding(root: stack);
2016-02-13 00:52:56 -08:00
}