【flutter开发|Flutter--获取父组件约束大小】context可以获取当前组件的大小,但是有时候我们想要渲染子组件时需要依赖当前组件的尺寸,build第一次运行时渲染树还没生成,调用context.size会报错,此时就需要使用LayoutBuilder组件,它可以获取父组件的约束,用它包裹子组件,变相获得还未渲染的父组件尺寸。
@override
Widget build(BuildContext context) {
return UIContainer(
width: double.infinity,
height: double.infinity,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Text("maxWidth: ${constraints.maxWidth}, maxHeight: ${constraints
.maxHeight} \n minWidth: ${constraints
.minWidth}, minHeight: ${constraints.minHeight}");
},
),
);
}
推荐阅读
- Flutter|flutter 学习笔记-1
- dart|Flutter 架构
- Android开发|移动端系统生物认证技术详解
- Flutter Provider状态管理框架
- 在 Flutter 使用 GetX 对话框
- Flutter开发|zsh: command not found: flutter
- IOS|Flutter运行Ios真机时报错(Failed to register observatory port with mDNS with error)
- dio拦截器|dio拦截器 flutter_Flutter 中 Dio 拦截器
- flutter|Flutter最酷炫瀑布流实现