尝试在 flutter 中创建一个更新的数字时钟

Van*_*thy 0 clock dart flutter

所以我使用一个名为timebuilder 的包来重建我的小部件。对于 DateFormat 类,我使用了这个包。

 final String currentTime = getSystemTime();

 static String getSystemTime() {
   var now = new DateTime.now();
   return new DateFormat("H:m:s").format(now);
 }

 @override
 Widget build(BuildContext context) {
   return TimerBuilder.periodic(Duration(seconds: 1), builder: (context) {
     print("$currentTime");
     return Text(
       "$currentTime",
       style: TextStyle(
           color: Color(0xff2d386b),
           fontSize: 30,
           fontWeight: FontWeight.w700),
     );
   });
 }
}
Run Code Online (Sandbox Code Playgroud)

然后我将显示 ClockWidget。我的小部件重建得很好。但 currentTime 的值仍然是我构建项目时最初的值。关于为什么 currentTime 不更新的任何线索?

Rav*_*mar 8

static从......中 去除getSystemTime()

尽管我已经根据您的代码创建了一个工作示例,

  String getSystemTime() {
      var now = new DateTime.now();
      return new DateFormat("H:m:s").format(now);
  }

Run Code Online (Sandbox Code Playgroud)
            TimerBuilder.periodic(Duration(seconds: 1), builder: (context) {
                print("${getSystemTime()}");
                return Text(
                  "${getSystemTime()}",
                  style: TextStyle(
                      color: Color(0xff2d386b),
                      fontSize: 30,
                      fontWeight: FontWeight.w700),
                );
              }),
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述