flutter - 在 Flutter 中使嵌套的小部件具有响应性
问题描述
为简单起见,我有 a Container
,它的孩子是 a Column
。该Column
小部件还有两个Container
. 现在根Container
是响应式的,它会根据屏幕调整大小,但它Container
内部的两个子节点Column
不会调整大小,因此会给我渲染溢出。手动设置MediaQuery.of(context).size
所有子小部件似乎不是最佳选择。
编码,
Scaffold(
body: Container(
height: size.height * 0.3, //size = MediaQuery.of(context).size
width: size.width * 0.3,
color: Colors.pinkAccent,
child: Column(
children: [
Container(
color: Colors.greenAccent,
height: 100,
width: 100,
),
Container(
height: 100,
width: 100,
color: Colors.orangeAccent,
),
],
),
),
);
我想Container
与父母一起调整孩子的大小Container
。
解决方案
如果我正确理解了您的问题,您可以使用Flexible
Widget
围绕您Container
的 s 来实现您的目标:
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height * 0.3, //size = MediaQuery.of(context).size
width: size.width * 0.3,
color: Colors.pinkAccent,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
flex: 1,
child: Container(
color: Colors.greenAccent,
width: 100,
child: Text('This is a text. This is a text. This is a text. This is a text. '),
),
),
Flexible(
flex: 1,
child: Container(
width: 100,
color: Colors.orangeAccent,
child: Text('This is a text. This is a text. This is a text. This is a text. '),
),
),
],
),
),
);
}
}
您可以通过调整每个内部的属性来调整每个Container
内部Flexible
Widget
占用的空间。flex
推荐阅读
- javascript - Javascript HTML:如何获取元素相对于其父元素的位置?
- sql - 从多个输入中获取多个时钟输入和输出时间,并在没有时钟的情况下忽略
- javascript - 分组描述中的柏树钩子
- r - 如何在我的数据框上使用完整连接并重命名具有相同名称 R 的列
- javascript - 如何在后端 js 代码中使用 emscripten 生成的 cpp 类?
- shopware - 如何在 Shopware 6 中访问自定义 cms 元素配置值?
- android - 扫描特定 BLE 设备的问题
- python - 二分搜索算法 - 项目思路
- python - 无法使用云运行在 Firebase 中托管烧瓶
- python - AttributeError:“成员”对象没有属性“通道”