Draggable: childWhenDragging, maxSimultaneousDrags

This commit is contained in:
Hixie
2016-02-09 14:47:09 -08:00
parent 1f0d292079
commit c56d378868
2 changed files with 166 additions and 36 deletions

View File

@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
@@ -22,7 +25,7 @@ class ExampleDragTargetState extends State<ExampleDragTarget> {
Widget build(BuildContext context) {
return new DragTarget<Color>(
onAccept: _handleAccept,
builder: (BuildContext context, List<Color> data, _) {
builder: (BuildContext context, List<Color> data, List<Color> rejectedData) {
return new Container(
height: 100.0,
margin: new EdgeDims.all(10.0),
@@ -129,7 +132,83 @@ class ExampleDragSource extends StatelessComponent {
}
}
class DragAndDropApp extends StatelessComponent {
class DashOutlineCirclePainter extends CustomPainter {
const DashOutlineCirclePainter();
static const int segments = 17;
static const double deltaTheta = math.PI * 2 / segments; // radians
static const double segmentArc = deltaTheta / 2.0; // radians
static const double startOffset = 1.0; // radians
void paint(Canvas canvas, Size size) {
final double radius = size.shortestSide / 2.0;
final Paint paint = new Paint()
..color = const Color(0xFF000000)
..style = ui.PaintingStyle.stroke
..strokeWidth = radius / 10.0;
final Path path = new Path();
final Rect box = Point.origin & size;
for (double theta = 0.0; theta < math.PI * 2.0; theta += deltaTheta)
path.addArc(box, theta + startOffset, segmentArc);
canvas.drawPath(path, paint);
}
bool shouldRepaint(DashOutlineCirclePainter oldPainter) => false;
}
class MovableBall extends StatelessComponent {
MovableBall(this.position, this.ballPosition, this.callback);
final int position;
final int ballPosition;
final ValueChanged<int> callback;
static const double kBallSize = 50.0;
Widget build(BuildContext context) {
Widget ball = new DefaultTextStyle(
style: Theme.of(context).text.body1.copyWith(
textAlign: TextAlign.center,
color: Colors.white
),
child: new Dot(
color: Colors.blue[700],
size: kBallSize,
child: new Center(child: new Text('BALL'))
)
);
Widget dashedBall = new Container(
width: kBallSize,
height: kBallSize,
child: new CustomPaint(
painter: const DashOutlineCirclePainter()
)
);
if (position == ballPosition) {
return new Draggable<bool>(
data: true,
child: ball,
childWhenDragging: dashedBall,
feedback: ball,
maxSimultaneousDrags: 1
);
} else {
return new DragTarget<bool>(
onAccept: (bool data) { callback(position); },
builder: (BuildContext context, List<bool> accepted, List<bool> rejected) {
return dashedBall;
}
);
}
}
}
class DragAndDropApp extends StatefulComponent {
DragAndDropAppState createState() => new DragAndDropAppState();
}
class DragAndDropAppState extends State<DragAndDropApp> {
int position = 1;
void moveBall(int newPosition) {
setState(() { position = newPosition; });
}
Widget build(BuildContext context) {
return new Scaffold(
toolBar: new ToolBar(
@@ -137,30 +216,32 @@ class DragAndDropApp extends StatelessComponent {
),
body: new Column(
children: <Widget>[
new Flexible(child: new Row(
children: <Widget>[
new ExampleDragSource(
color: const Color(0xFFFFF000),
under: true,
heavy: false,
child: new Text('under')
),
new ExampleDragSource(
color: const Color(0xFF0FFF00),
under: false,
heavy: true,
child: new Text('long-press above')
),
new ExampleDragSource(
color: const Color(0xFF00FFF0),
under: false,
heavy: false,
child: new Text('above')
),
],
alignItems: FlexAlignItems.center,
justifyContent: FlexJustifyContent.spaceAround
)),
new Flexible(
child: new Row(
children: <Widget>[
new ExampleDragSource(
color: Colors.yellow[300],
under: true,
heavy: false,
child: new Text('under')
),
new ExampleDragSource(
color: Colors.green[300],
under: false,
heavy: true,
child: new Text('long-press above')
),
new ExampleDragSource(
color: Colors.indigo[300],
under: false,
heavy: false,
child: new Text('above')
),
],
alignItems: FlexAlignItems.center,
justifyContent: FlexJustifyContent.spaceAround
)
),
new Flexible(
child: new Row(
children: <Widget>[
@@ -171,6 +252,16 @@ class DragAndDropApp extends StatelessComponent {
]
)
),
new Flexible(
child: new Row(
children: <Widget>[
new MovableBall(1, position, moveBall),
new MovableBall(2, position, moveBall),
new MovableBall(3, position, moveBall),
],
justifyContent: FlexJustifyContent.spaceAround
)
),
]
)
);