赞
踩
MediaQuery.of(context).size // Size(414.0, 896.0)
MediaQuery.of(context).devicePixelRatio; // 2.0
使用window
需要引入dart:ui
库。
window.physicalSize; // Size(828.0, 1792.0)
window.devicePixelRatio; // 2.0
使用widget中的key可以获取widget的尺寸及位置信息。
class GetWidgetSize extends StatelessWidget { GlobalKey _key1 = GlobalKey(); GlobalKey _key2 = GlobalKey(); GlobalKey _key3 = GlobalKey(); @override Widget build(BuildContext context) { // 监听widget渲染完成 WidgetsBinding.instance.addPostFrameCallback((duration){ RenderBox box = _key1.currentContext.findRenderObject(); // _key1.currentContext.size; Size(200.0, 100.0) print(box.size); // Size(200.0, 100.0) print(box.localToGlobal(Offset.zero)); // Offset(107.0, 100.0) }); // TODO: implement build return Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ Container( key: _key1, constraints: BoxConstraints.tight(Size(200, 100)), color: Colors.red, ), Container( key: _key2, margin: EdgeInsets.symmetric(vertical: 30.0), constraints: BoxConstraints.tight(Size(200, 100)), color: Colors.yellow, ), Container( key: _key3, constraints: BoxConstraints.tight(Size(200, 100)), color: Colors.blue, ), ], ), ); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。