【问题标题】:How to customizing alignment to components inside a Row on Flutter?如何在 Flutter 的 Row 中自定义对齐组件?
【发布时间】:2021-04-05 01:05:03
【问题描述】:

我正在构建一个Card 来显示图像和一些文本信息。我希望 RED Container 内容与顶部对齐,而不是像当前一样居中。我使用Row 来划分ImageTexts 内容。除此之外我想改变Texts在图片右侧整个空间的位置,我该如何管理呢?

卡片小部件

  Widget buildExerciseCard(BuildContext context, int index) {
    return Container(
      color: Colors.green,
      margin: EdgeInsets.fromLTRB(5, 2, 5, 0),
      child: Card(
          color: Colors.blue,
          child: Row(
            children: [
              Image.asset(
                exerciseList[index].imagePath,
                width: 75,
                height: 75,
                fit: BoxFit.contain,
              ),
              Container(
                color: Colors.red,
                child: Column(
                  children: [
                    Text(exerciseList[index].name),
                    Text(exerciseList[index].bodyPart),
                    Text(exerciseList[index].muscle)
                  ],
                ),
              ),
            ],
          )),
    );
  }

结果:

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    试试这个代码:

    
      Widget buildExerciseCard(BuildContext context, int index) {
        return Container(
          color: Colors.green,
          alignment: Alignment.topLeft,
          margin: EdgeInsets.fromLTRB(5, 2, 5, 0),
          child: Card(
              color: Colors.blue,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Image.asset(
                    exerciseList[index].imagePath,
                    width: 75,
                    height: 75,
                    fit: BoxFit.contain,
                  ),
                  Container(
                    color: Colors.red,
                    child: Column(
                      children: [
                        Text(exerciseList[index].name),
                        Text(exerciseList[index].bodyPart),
                        Text(exerciseList[index].muscle)
                      ],
                    ),
                  ),
                ],
              )),
        );
      }
    

    【讨论】:

    • 这只是将容器/列扩展到所有水平空间,而不是与顶部对齐。
    • 立即查看。我已经更新了代码。它正在按您的预期工作
    猜你喜欢
    • 2020-03-05
    • 2020-11-16
    • 1970-01-01
    • 2019-06-21
    • 2021-01-11
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多