flutter 日期时间抖动

2nbm6dog  于 2023-02-09  发布在  Flutter
关注(0)|答案(6)|浏览(138)

如何将当前时间实现为文本格式?我觉得应该相当简单,但很难做到。
基本示例;

kd3sttzy

kd3sttzy1#

使用答案here并稍微更改一下:-
您可以尝试以下操作:

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


void main() {
 runApp(TabBarDemo());
}

class TabBarDemo extends StatelessWidget {
 @override
Widget build(BuildContext context) {
DateTime now = DateTime.now();
String formattedDate = DateFormat('kk:mm:ss \n EEE d MMM').format(now);
return MaterialApp(
  home: DefaultTabController(
    length: 3,
    child: Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          tabs: [
            Tab(icon: Icon(Icons.access_alarm),text: "Alarm",),
            Tab(icon: Icon(Icons.access_time),text:"Clock" ,),
            Tab(icon: Icon(Icons.timer),text:"Timer"),
          ],
        ),
        title: Text('Tabs Demo'),backgroundColor: Colors.black,
      ),
      body: TabBarView(
        children: [
          Icon(Icons.access_alarm),
          Center(child: Text(formattedDate,textAlign: TextAlign.center,style: new TextStyle(fontWeight: FontWeight.bold,fontSize: 25.0),)),
          Icon(Icons.timer),
        ],
      ),
    ),
  ),
);
}
}

应该给你这个:

lsmepo6l

lsmepo6l2#

Flutter1.20时:

DateTime now = DateTime.now();
print(now.hour.toString() + ":" + now.minute.toString() + ":" + now.second.toString());

t5zmwmid

t5zmwmid3#

获取AM/PM格式的当前时间。
dynamic currentTime = DateFormat.jm().format(DateTime.now());

sg2wtvxw

sg2wtvxw4#

var now = DateTime.now();
var formatterDate = DateFormat('dd/MM/yy');
var formatterTime = DateFormat('kk:mm');
String actualDate = formatterDate.format(now);
String actualTime = formatterTime.format(now);
gwbalxhn

gwbalxhn5#

要设置连续运行时钟:

void initState() {
_timeString = _formatTime(DateTime.now());

Timer.periodic(const Duration(seconds: 1), (Timer t) => _getTimeString());
super.initState();

}
然后使用获取时间:

_getTimeString() {
final DateTime now = DateTime.now();
final String formattedTime = _formatTime(now);

setState(() {
  _timeString = formattedTime;    
});

}
用于设置时间格式:

String _formatTime(DateTime dateTime) {
return DateFormat('hh:mm:ss').format(dateTime);

}
要显示时间,请创建一个文本小部件并使用

'$_timeString'

显示不断更新的时钟。

wbgh16ku

wbgh16ku6#

在Flutter应用程序中选择准确时间并放入本地通知包...完整详细指南。//(alarm App)enter image description here

相关问题