有什么办法可以动态地将切换按钮扩展到父容器宽度,无需硬编码任何内容。我找到了一个使用上下文 MediaQuery 的答案,该答案仅适用于全屏宽度。我还尝试将按钮包装在扩展小部件中,但这会引发错误
Container(
width: 150.0, // hardcoded for testing purpose
child: ToggleButtons(
constraints:
BoxConstraints.expand(width: MediaQuery.of(context).size.width), // this doesn't work once inside container unless hard coding it
borderRadius: BorderRadius.circular(5),
children: [
ShapeToggleButton(
text: 'Option1',
),
ShapeToggleButton(
text: 'Option2',
),
],
isSelected: [true, false],
onPressed: (index) {},
),
);
正如上面评论中 psink 所建议的,答案是将其包装在 LayoutBuilder 中
Container(
width: 150.0, // hardcoded for testing purpose
child: LayoutBuilder(builder: (context, constraints) {
return ToggleButtons(
renderBorder: false,
constraints:
BoxConstraints.expand(width: constraints.maxWidth / 2), //number 2 is number of toggle buttons
borderRadius: BorderRadius.circular(5),
children: [
Text(
'Option1',
),
Text(
'Option2',
),
],
isSelected: [true, false],
onPressed: (index) {},
);
})))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)