要确保Draggable可以在父部件内拖动,需要将其包裹在一个Container或类似的可滚动父部件中。然后,将Draggable中的feedback widget设置为Stack,以使其可以被拖动。
以下是一个示例代码:
Container(
height: 300,
child: ListView(
children: [
Draggable(
child: Container(
height: 100,
width: 100,
color: Colors.blue,
),
feedback: Stack(
children: [
Container(
height: 100,
width: 100,
color: Colors.blue.withOpacity(0.5),
),
Text(
'Drag me!',
style: TextStyle(fontSize: 20),
),
],
),
childWhenDragging: Container(),
onDragEnd: (details) {},
),
],
),
);
在这个例子中,我们将Draggable包裹在容器和一个ListView中。feedback widget以不透明度为0.5的蓝色矩形和一个“Drag me!”文本构成的Stack的形式提供给了Draggable。子Widget是一个100x100的蓝色矩形。
在拖动feedback widget时,如果没有使用Stack,那么feedback widget会被限制在ListView的边界内。使用Stack会使得Draggable可以在父部件内拖动。