flutter - 如何获取父布局的高度 - Flutter
问题描述
我想要容器的高度
Container( // how to get height of the container
child: Text('hello,\nhow to get\nheight of the parent layout'),
),
我试试这个
return LayoutBuilder(
builder:
(BuildContext context, BoxConstraints constraints) {
print(constraints.maxHeight);
return Container( // how to get height of the container
child: Text('hello,\nhow to get\nheight of the parent layout'),
);
},
);
高度打印始终为 0 值
解决方案
关于您的代码段
return LayoutBuilder(
builder:
(BuildContext context, BoxConstraints constraints) {
print(constraints.maxHeight);
return Container( // how to get height of the container
child: Text('hello,\nhow to get\nheight of the parent layout'),
);
},
);
您正在尝试获得孩子的大小。要获取父级大小,您需要LayoutBuidler
在该小部件上用作子级。
像这样,
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(body: LayoutBuilder(
builder: (context, bodyConstraints) {
print("body height : ${bodyConstraints.maxHeight}");
return Container(
height: 200,
color: Colors.deepOrangeAccent,
child: LayoutBuilder(
builder: (context, containerConstraints) {
print("Container height ${containerConstraints.maxHeight}");
return Text(
" body Height: ${bodyConstraints.maxHeight} \n container height: ${containerConstraints.maxHeight}",
);
},
),
);
},
));
}
}
它解决了你的问题吗?
推荐阅读
- .net - 构建一个 .net 和 sql server 数据 mngmt 应用程序,其中应用程序 sql 存储在表中是一种合理的方法吗?
- android - 在 Kotlin 中创建 BaseView 类
- extjs - 如何在 Web 应用程序页面结束呈现时设置覆盖面板 extjs
- multithreading - ThreadPoolExecutor - 如果池已满,我可以抛出异常吗
- javascript - Vue router-view 加载父组件而不是它自己的
- c# - 了解 Reduce() 函数异常:输入和输出数组格式的不支持组合
- qt - 运行windeployqt后找不到平台插件DLL
- java - 如何在多个线程之间共享事务
- eclipse - Eclipse 不再编译
- java - 休眠删除只是不会触发