【问题标题】:Flutter row element remain center even using alignment or column element could not change it row align left?即使使用对齐或列元素无法更改行左对齐,颤动的行元素仍保持居中?
【发布时间】:2020-04-30 08:44:10
【问题描述】:

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

  1. alignment: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,
            ),
          ),
        ],
      ),
    ),
  );
}

这是屏幕截图。

【问题讨论】:

  • 你试过Row Widget的crossAxisAligment属性吗?
  • @JoãoSoares 是的,我也尝试过,但还是一样。奇怪的是我尝试了所有选项,但它似乎根本不动。
  • @newbie 如果可能,请分享屏幕截图
  • @CrazyLazyCat 我已根据您的要求添加了屏幕截图?
  • @newbie 是我的回答,你想要达到什么目的?如果是这样,请记住将答案标记为正确,以便其他用户将来可以看到。

标签: flutter alignment containers


【解决方案1】:

我使时钟图标左对齐,日期居中,时间右对齐。这是你想要达到的目标吗?

Container(
  color: Colors.grey,
  alignment:Alignment.topRight,
  padding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
  child: buildDateTime("datetime")
);

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

我的结果:

【讨论】:

  • 不,还是一样,我怀疑上面的其他容器或行或列是一个问题?
  • @newbie 就像我在上面的评论中所说的那样。你复制了我所有的代码吗?您在我的屏幕截图中看到的内容是您想要实现的吗?
  • 你周围的代码肯定有问题。我在屏幕截图上显示的正是我的代码所做的。应用程序中没有其他内容。尝试在没有其他内容的视图中加载此代码,看看它的外观。
  • @Joaa 是的,我怀疑是什么导致了这个问题。让我一一调试。
【解决方案2】:

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

试试这个,

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,
          ),
        ),
      ],
    ),
  );
}

【讨论】:

  • 它可能是父列或行也需要交叉对齐设置吗?
  • 是的。它可能来自外部的问题,其中包含Container
  • 是的,它实际上是通过其他几个容器、列、行嵌入的,所以我想我需要检查所有这些。我应该添加所有父元素交叉对齐功能吗?
  • 不确定。不过你可以试试。
  • 最终目标是什么。只显示日期列表?
猜你喜欢
  • 2015-04-26
  • 2014-09-18
  • 2014-08-10
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 2014-12-20
  • 2020-08-18
相关资源
最近更新 更多