flutter - 我想在 Flutter 中将我的行分成 1/4 的比例

标签 flutter flutter-layout

我想将 flutter 中的行分成 4 列,宽度相同。到目前为止,我想出的解决方案是这样的,

   child: Row(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.greenAccent),
          ),
          Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.yellow),
          ),
          Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.blue),
          ),
          Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.white),
          ),
        ],
      ),

有没有什么替代方法。在这种方法中,将行分成 1/3 的比例也不起作用。

最佳答案

您可以通过使用 Expanded Widget 来实现这一点。

检查下面的代码:

它工作得很好。

1) For 1/3 ratio


Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.greenAccent),
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.yellow),
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.blue),
          ),
        ),
      ],
    ),

1) For 1/4 ratio


Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.greenAccent),
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.yellow),
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.blue),
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.25,
            decoration: BoxDecoration(color: Colors.white),
          ),
        ),
      ],
    ),

请参阅下面的输出:

enter image description here

enter image description here

我希望这回答了你的问题。

关于flutter - 我想在 Flutter 中将我的行分成 1/4 的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61616336/

相关文章:

list - 如何在 Flutter 中创建网络图像列表

flutter - 在下拉菜单中显示带有下拉选项的 Bottom Sheet 单击 flutter

flutter - 下拉选框保持选择

flutter - 如何在 Flutter 中将我的应用程序设置为横向模式?

flutter - 如何防止 slider 弄乱我的布局?

flutter - 无法记录事件 : analytics library is missing

flutter - Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用

dart - Flutter:堆栈中底部中心小部件

database - hive 或 sqflite 是否适合在 flutter 中处理大量数据

datatable - 如何在 Flutter 中监听 DataTable 中的 DataRow