flutter - 如何在颤动的容器内制作固定大小的容器
问题描述
我正在尝试制作应用程序的布局,其中主容器将具有 45% 的设备高度,并且主容器内的容器应该是固定大小。
我编写了以下代码,但内部容器将全高作为其父(主)容器。
class TestContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 50,
height:MediaQuery.of(context).size.height * 0.45,
color: Colors.red
child:Container(
width: 50,
height: 100,
color: Colors.green
)
);
}
}
任何线索我做错了什么?
解决方案
通过用Center
小部件包装它来修复它:
class TestContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 50,
height:MediaQuery.of(context).size.height * 0.45,
color: Colors.red,
// wrap with a center widget
child:Center(
child: Container(
width: 50,
height: 100,
color: Colors.green
),
)
);
}
}
将内容器放置在底部中心
class TestContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 50,
height: MediaQuery.of(context).size.height * 0.45,
color: Colors.red,
// wrap with a align widget
child: Align(
// set the alignment property
alignment: Alignment.bottomCenter,
child: Container(
width: 50,
height: 100,
color: Colors.green,
),
),
);
}
}
在此处阅读有关 Flutter 中布局约束的更多信息:https ://flutter.dev/docs/development/ui/layout/constraints
推荐阅读
- git - 如何访问在 GitHub Actions 中提交的文件?
- python - Pandas Lookup 将被弃用 - 优雅而高效的替代方案
- laravel - 如何在 laravel 中链接 Bus::chain()
- css - font-family suggestion in Chrome devtools is commercial-free or not?
- aws-sdk - 纱线添加@aws-sdk/client-dynamodb
- android - 如何实现具有不同类型项目(TextView、Button、Spinner)的通用 RecyclerView?
- python - 带有 VIEW 语句的 Django SUM 查询
- android - 如何在约束布局内同时应用左右约束时将布局左对齐?
- javascript - Mongoose Express.js 从多个查询中获取结果
- swift - 如何使按钮每次按下时显示不同的标签