我想获取 a 的偏移量StatefulWidget,因为我需要它来设置小部件的位置OverlayEntry。我尝试使用以下片段来获取偏移量:
final renderBox = context.findRenderObject();\n final offset = renderBox.localToGlobal(Offset.zero);\nRun Code Online (Sandbox Code Playgroud)\n final _key = GlobalKey();\n /* ----------Some code------------- */\n final renderBox = _key.currentContext.findRenderObject();\n final offset = renderBox.localToGlobal(Offset.zero);\nRun Code Online (Sandbox Code Playgroud)\n但每次我执行其中任一操作时,我都会从 Dart 分析器收到此错误消息:
\nThe method 'localToGlobal' isn't defined for the type 'RenderObject'.\nTry correcting the name to the name of an existing method, or defining a method named 'localToGlobal'.\nRun Code Online (Sandbox Code Playgroud)\n这是我目前使用的 Flutter 版本:
\n[\xe2\x88\x9a] Flutter (Channel stable, v1.17.5, on Microsoft Windows [Version 10.0.18363.900], locale en-NG)\nFlutter 1.17.5 \xe2\x80\xa2 channel stable \xe2\x80\xa2 https://github.com/flutter/flutter.git\nFramework \xe2\x80\xa2 revision 8af6b2f038 (8 days ago) \xe2\x80\xa2 2020-06-30 12:53:55 -0700\nEngine \xe2\x80\xa2 revision ee76268252\nTools \xe2\x80\xa2 Dart 2.8.4\nRun Code Online (Sandbox Code Playgroud)\n
Gio*_*tto 10
嗨,我遇到了同样的问题。就像他们在这里讨论的那样,您应该指定变量类型。所以你必须这样做:
final _key = GlobalKey();
/* ----------Some code------------- */
final RenderBox renderBox = _key.currentContext.findRenderObject();
final offset = renderBox.localToGlobal(Offset.zero);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3846 次 |
| 最近记录: |