flutter - 如何在颤动的环上约束可拖动对象?
问题描述
我的想法是首先创建一个Stack。这个Stack将有两个孩子。首先,创建一个Container并将其制成圆形,并且它有一个GestureDetetor作为其子项。第二个是另一个容器,它也是圆形,半径较小(我认为这可能会掩盖GestureDetector的区域,我不希望它对任何手势都有效)。
我的代码就像,
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
width: 200.0,
height: 200.0,
padding: EdgeInsets.all(1.0),
decoration: BoxDecoration(
color: Colors.yellow,
shape: BoxShape.circle,
),
child: Builder(
builder: (context) {
final handle = GestureDetector(
onPanUpdate: _panHandler,
child: Icon(
Icons.arrow_drop_down_circle,
size: 30.0,
),
);
return AnimatedBuilder(
animation: valueListener,
builder: (context, child) {
return Align(
alignment: Alignment(
(valueListener.value[0] * 2 - 1),
(valueListener.value[1] * 2 - 1),
),
child: child,
);
},
child: handle,
);
},
),
),
Container(
height: 180.0,
width: 180.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.green,
),
),
],
);
}
但是,这段代码根本无法约束位置圈……
解决方案
你可以使用Transform.rotate。它有一个origin属性,可以帮助您使用偏移量旋转小部件。您需要做的就是相应地更新 Panupdate 上的角度。下面是我用来创建这个的代码
Transform.rotate(
origin: Offset(125, 0),
angle: torad(angle), //function to convert degree to radians
child: GestureDetector(
onPanUpdate: (dragUpdateDetails) {
BlocProvider.of<ABloc>(context)
.add(Dragged()); //bloc event to update the position of draggable object as per your convenience
},
child: Container(
height: 30,
width: 30,
child: CircularWidget(),
color: Colors.transparent),
),
),
推荐阅读
- flutter - 如何将列表视图与其他小部件一起使用?
- javascript - 更改 Vite 中的输出结构
- python - Python的内存行为
- javascript - 如何保存对象的打开/关闭状态
- c++ - 如何使用迭代器和 string_view 将字符串拆分为 1024 块
- c# - 如何在另一个进程中挂钩托管库中的方法而不通过 dll 注入修改它们?
- django - 如何仅使用 django manage.py makemessages 创建 .pot 文件
- reverse - 如何修复 ida pro 7.5 中的错误“无法初始化 WinDbg 引擎”?
- unit-testing - .Net core Web API 5.0 的 Nunit 单元测试
- bpmn - Camunda:测试 JSON 数组是否包含元素