【发布时间】:2021-05-31 00:16:59
【问题描述】:
我想在容器中将此功能添加到我的应用程序中,因此,我有 h:m:s 中的时间,例如,如果给定时间是(下午 6:27)我想要这个结果(剩余时间 02:21: 02)
【问题讨论】:
-
请分享您的代码。
标签: flutter
我想在容器中将此功能添加到我的应用程序中,因此,我有 h:m:s 中的时间,例如,如果给定时间是(下午 6:27)我想要这个结果(剩余时间 02:21: 02)
【问题讨论】:
标签: flutter
我想你想要的只是:
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]),
],
);
},
);
}
}
【讨论】: