首页 > 解决方案 > 如何将小部件动态添加到手势检测器的偏移位置颤动

问题描述

[![Image Sample][1]][1] 下面这张图片是我添加到我的颤振应用程序中的。我希望它工作的方式是,一旦用户点击任何位置,我就会向它添加一些指示器小部件。我有我的逻辑,它返回手势检测器的点击坐标点。它返回一个偏移位置,例如 Offset(172.5, 298.5)

   Stack(
                children: [
                  GestureDetector(
                    onTapDown: _handleTapDown,
                    onTap: () {

                    },
                    child: Container(
                        child: Image.asset(
                      images[index],
                      height: 500,
                      width: double.infinity,
                    )),
                  ),
                  Positioned(
                    right: 20.0,
                    bottom: 70.0,
                    child: Container(
                      width: 100,
                      height: 48,
                      child: MainButton(
                        color: primaryColor,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            new Image.asset(
                              'images/rotate.png',
                              height: 16,
                              width: 16,
                            ),
                            SizedBox(
                              width: 14,
                            ),
                            Text(
                              "Turn",
                              textAlign: TextAlign.start,
                              style: TextStyle(
                                fontSize: 14,
                                fontFamily: 'Lato',
                                color: Colors.white,
                                fontWeight: FontWeight.w700,
                              ),
                            ),
                          ],
                        ),
                        onPressed: () {
                          setState(() {
                            if (index == 2) {
                              index = 0;
                            } else {
                              index++;
                            }
                          });
                        },
                      ),
                    ),
                  ),
                ],
              ),

 void _handleTapDown(TapDownDetails details) {
    final RenderBox referenceBox = context.findRenderObject();
    setState(() {
      _tapPosition = referenceBox.globalToLocal(details.globalPosition);
      print(_tapPosition);
    });
  }

现在,如何确保指示器小部件根据其点击位置出现在用户点击的任何点上?[1]:https ://i.stack.imgur.com/pHz2om.png

标签: flutterdartstack

解决方案


推荐阅读