Flutter 小知識,Key的使用(獲取當前點選Widget位置/獲取當前Widget大小)
初始化key:
GlobalKey _keyGreen = GlobalKey();
引用Key:
InkWell(
child: Container(
margin: EdgeInsets.only(left: 30),
child: Text(
"Key測試",
textAlign: TextAlign.center,
),
),
key: _keyGreen,
onTap: () {
//獲取當前Widget大小
final RenderBox renderBox =
_keyGreen.currentContext.findRenderObject();
final sizeGreen = renderBox.size;
print("SIZEofgreen: $sizeGreen");
//獲取當前螢幕位置
final positionGreen = renderBox.localToGlobal(Offset.zero);
print("POSITIONofgreen: $positionGreen");
},
)
獲取螢幕位置:
final RenderBox renderBox = _keyGreen.currentContext.findRenderObject();
final positionGreen = renderBox.localToGlobal(Offset.zero);
print("POSITIONofgreen: $positionGreen");
獲取當前Widget大小:
final RenderBox renderBox = _keyGreen.currentContext.findRenderObject();
final sizeGreen = renderBox.size;
print("SIZEofgreen: $sizeGreen");
注意:
一定要標識key
相關文章
- Flutter中http請求抓包解決方案
- Flutter 外掛的建立及使用
- Mybatis中updateByPrimaryKeySelective和updateByPrimaryKey的用法區別
- 開源認證和訪問控制的利器keycloak使用簡介
- pc 有麼有什麼工具活框架 可以像 monkeyrunner 的 可以自動遍歷執行的
- DUMP-CX_SY_OPEN_SQL_DB-DBSQL_DUPLICATE_KEY_ERROR
- flutter-banner
- 用struct做unordered_map的key
- Padding - flutter
- flutter doctor提示安裝flutter dart外掛,實際已經安裝了的解決辦法
- laravel出現The cipher and / or key length are invalid 的Bug除錯
- PyQt5刪除QListWidget中的item
- 從ERROR 1062 (23000) at line Duplicate entry 'R01' for key 'PRIMARY' 能看出什麼
- Flutter迴圈滑動的PageView
- Monkey 執行的時候,音樂播放..
- 工作193:vue.runtime.esm.js?2b0e:619 [Vue warn]: <transition-group> children must be keyed: <ElTag>
- 工作195:解決key值不唯一的報錯
- 一種更優雅的Flutter Dialog解決方案