如何在 flutter 中使用 DateTime.now() 仅显示当前时间?

How I can show only current time using DateTime.now() in flutter?

 DateTime currentTime = new DateTime.now();

  Widget CustomCards(Color frontColor, String fetchTime) {
    Card cards = new Card(
      elevation: 1.0,
      margin: const EdgeInsets.symmetric(horizontal: 2.0, vertical: 2.0),
      color: bgColor,
      shape: new RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15.0),
      ),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(5.0),
            child: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 15.0),
                  child: Text(
                  currentTime.toString(),
                  style: new TextStyle(
                    fontStyle: FontStyle.italic,
                    fontFamily: 'Satisfy',
                    fontSize: 15.0,
                    color: frontColor,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
    return cards;
  }

我如何显示 DateTime 的当前时间,因为它总是显示当前日期和时间,但我想以 格式 hh:mm:ss 显示时间。这里秒应该一定是他们的。我找不到任何逻辑请帮助我。 谢谢

使用 DateFormat class 将其转换为字符串和您想要的模式。

示例:

DateFormat('your date pattern').format(currentTime);

在 pubspec.yaml intl: ^0.16.1 中添加依赖项(检查 pub.dev 中的最新依赖项)然后您可以在 dart 文件中使用 DateFormat.yMMMd().format(DateTime.now());。您可以根据需要更改日期格式格式。

对于 DateFormat 集中不存在的自定义日期模式,您可以使用

DateFormat('hh:mm:ss').format(DateTime.now());