I'm trying to make fill-in-the-blank UI by using DragTarget
and Draggable
.
I want to set offset between feedback
and pointer during dragging to prevent the feedback
from beeing hidden by a finger,
and I need to do hit-testing between the feedback
and the dragtarget
How can I implement this?
This is what I want to implement.
Solution
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _text = '';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: _buildDragTarget(),
),
),
Expanded(
child: Center(
child: _buildDraggable(),
),
),
],
),
);
}
Widget _buildDragTarget() {
return DragTarget<String>(onAccept: (data) {
setState(() {
_text = data;
});
}, builder: (BuildContext context, List candidateData, List rejectedData) {
return Container(
width: 150,
height: 50,
decoration: BoxDecoration(
border: Border.all(width: 3, color: Colors.blue),
),
child: Center(
child: Text(
_text,
style: TextStyle(fontSize: 24),
),
),
);
});
}
Widget _buildDraggable() {
var draggable = Chip(
label: Text(
'draggable',
style: TextStyle(fontSize: 24),
),
);
var feedback = Transform.scale(
scale: 2.0,
child: Opacity(
opacity: 0.5,
child: Container(
padding: EdgeInsets.only(bottom: 100),
child: Material(
color: Colors.transparent,
child: Transform.scale(
scale: 0.5,
child: draggable,
)),
),
),
);
return Draggable<String>(
dragAnchor: DragAnchor.child,
child: draggable,
feedback: feedback,
feedbackOffset: Offset(0, -50),
data: 'draggable',
);
}
}
Transform
andPadding
, but I had to make more complicated widget than I thought... – Stillman