有什么方法可以在不进行任何硬编码的情况下将切换按钮动态扩展到父容器宽度。我找到了一个使用 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) {},
),
);
Run Code Online (Sandbox Code Playgroud)