【问题标题】:flutter count the time remain for given hour updated by seconds颤振计数给定小时的剩余时间以秒为单位更新
【发布时间】:2021-05-31 00:16:59
【问题描述】:

我想在容器中将此功能添加到我的应用程序中,因此,我有 h:m:s 中的时间,例如,如果给定时间是(下午 6:27)我想要这个结果(剩余时间 02:21: 02)

【问题讨论】:

  • 请分享您的代码。

标签: flutter


【解决方案1】:

我想你想要的只是:

target.difference(DateTime.now()).toString().split('.')[0])

我做了.split('.')[0] Duration 来删除秒的小数部分。

完整源代码

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CountDown(
          target: DateTime.now().add(
            Duration(minutes: 5),
          ),
        ),
      ),
    );
  }
}

class CountDown extends StatelessWidget {
  final DateTime target;

  const CountDown({
    Key key,
    this.target,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Stream.periodic(const Duration(seconds: 1)),
      builder: (context, snapshot) {
        return Column(
          children: [
            Text('Time until ${DateFormat.Hms().format(target)}'),
            const SizedBox(height: 24.0),
            Text(target.difference(DateTime.now()).toString().split('.')[0]),
          ],
        );
      },
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多