首页 > 解决方案 > 即使使用对齐方式或列元素无法更改行左对齐,颤动的行元素仍保持居中?

问题描述

我有以下容器,并在其中调用一个函数来创建子小部件。子行始终保持居中对齐的问题。我试过两种方法

  1. 对齐:Alignment.topLeft,
  2. 我尝试将其进一步嵌入列中并尝试使用 crossAxisAlignment:CrossAxisAlignment.start。

两种方法都无法将元素左对齐。它将残留物保留在容器的中心部分。下面可能有问题的是我的代码。

Container(
  alignment:Alignment.topRight,
  margin: const EdgeInsets.fromLTRB(10, 10, 0, 0),
  padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
  child: Column(
    mainAxisSize: MainAxisSize.max,
    mainAxisAlignment: MainAxisAlignment.start,
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          buildDateTime("datetime"),
        ]
      )
    ],
  )
),

下面是我调用来构建行的小部件。

Widget buildDateTime(String plate) {
  return new Container(
    child: Container(
      color: Colors.transparent,
      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Icon(Icons.access_time, size: 15.0, color: lightGrey),
          Text(
            ' 23/01/20202',
            style: new TextStyle(
              color: lightGrey,
              fontFamily: "Bpreplay",
              fontWeight: FontWeight.w400,
              fontSize: 14.0,
            ),
          ),
          Text(
            ' 09:00',
            style: new TextStyle(
              color: lightGrey,
              fontFamily: "Bpreplay",
              fontWeight: FontWeight.w400,
              fontSize: 14.0,
            ),
          ),
        ],
      ),
    ),
  );
}

这是屏幕截图。

在此处输入图像描述

标签: flutteralignmentcontainers

解决方案


我无法重现您提到的问题。对我来说,它占据了整个宽度。

尝试这个,

Widget buildDateTime(String plate) {
  return Align(
    alignment: Alignment.centerLeft,
    child: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Icon(Icons.access_time, size: 15.0, color: Colors.grey),
        Text(
          ' 23/01/20202',
          style: const TextStyle(
            color: Colors.grey,
            fontFamily: "Bpreplay",
            fontWeight: FontWeight.w400,
            fontSize: 14.0,
          ),
        ),
        Text(
          ' 09:00',
          style: const TextStyle(
            color: Colors.grey,
            fontFamily: "Bpreplay",
            fontWeight: FontWeight.w400,
            fontSize: 14.0,
          ),
        ),
      ],
    ),
  );
}

推荐阅读