flutter开发|Flutter--获取父组件约束大小

【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}"); }, ), ); }


    推荐阅读