【问题标题】:Adding a new column to a card in Flutter在 Flutter 中向卡片添加新列
【发布时间】:2019-04-29 03:53:41
【问题描述】:

如何在颤动的卡片中添加新列?

目前,我有 2 列,但不知道如何在它们之间添加第三列,因为它们位于左侧和右侧。当我尝试添加“新列”时,它只会在第一列下创建一个新的文本行。

这是我目前的代码:

  Widget _buildListItem(BuildContext context, Record record) {
    return Card(
      key: ValueKey(record.activityName),
      elevation: 8.0,
      margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
      child: Container(
        decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .9)),
        child: ListTile(
          contentPadding:
          EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),

           title: Text(
            record.activityName,
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 23),
          ),
          subtitle: Row(
            children: <Widget>[
              new Flexible(
                  child: new Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        RichText(
                          text: TextSpan(
                            text: "Activations: "+record.activations+
                                  "\n"+record.dateCompleted,
                            style: TextStyle(color: Colors.white),
                          ),
                          maxLines: 2,
                          softWrap: true,
                        )
                      ],
                  ) 
                )
            ],
          ),
          trailing: Container(
              child: Hero(
                  tag: "avatar_" + record.activityName,
                  child: CircleAvatar(
                    radius: 32,
                    backgroundImage: NetworkImage(record.icon),
                    backgroundColor: Colors.white,
                  )
              )
          ),
          onTap: () {
            Navigator.push(
                context, MaterialPageRoute(builder: (context) => new DetailPage(record: record)));
          }
        ),
      )
    );
  }

我想要 3 列,以便我可以在这张卡片的中间添加一张图片。

Current output Expected output

【问题讨论】:

标签: dart flutter flutter-layout


【解决方案1】:

根据您的屏幕截图,这是解决方案。您可以根据需要对其进行修改。

Widget _buildListItem(BuildContext context) {
  return Card(
    margin: EdgeInsets.all(12),
    elevation: 4,
    color: Color.fromRGBO(64, 75, 96, .9),
    child: Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16),
      child: Row(
        children: <Widget>[
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text("Jumping", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
              SizedBox(height: 4),
              Text("Activations 9", style: TextStyle(color: Colors.white70)),
              Text("03-08-19", style: TextStyle(color: Colors.white70)),
            ],
          ),
          Spacer(),
          CircleAvatar(backgroundColor: Colors.white),
        ],
      ),
    ),
  );
}

输出:

【讨论】:

  • 谢谢!尽管我在问题中修改了“当前”和“预期”输出,但这通过在 CircleAvatar 之前添加另一个 Spacer 帮助我创建了一个新列。我应该在哪里添加 onTap 调用?
  • 您可以在我的解决方案中在Expanded 中使用Text/Image 在CircleAvatar 之前,您可能需要删除Spacer。
  • 那是我说的 :) onTap/onClick 调用呢?
  • 如果您想允许点击您的Text/Image,您需要将它们包装在GestureDetector 中并使用onTap()。
猜你喜欢
  • 2020-10-22
  • 2021-11-28
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 2020-10-15
相关资源
最近更新 更多