— Use timer directly
List of topics: juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, greenwillow, v-green, vue-pro
Contribution Subject:Github.com/xitu/juejin…
theme: juejin highlight:
The Flutter gets the current date
- Datetime. day What’s the date today
- Datetime. month Current month
- dateTime.year
- dateTime.hour
- dateTime.minute
- dateTime.second
- dateTime.millisecond
- dateTime.millisecondSinceEpoch
Specific code
import 'dart:async'; import 'package:flutter/material.dart'; class KmTimer extends StatefulWidget { @override State<KmTimer> createState() => _KmTimerState(); Void cancel() {}} class _KmTimerState extends State<KmTimer> { DateTime dateTime; @override void initState() { super.initState(); dateTime = new DateTime.now(); this._timer = new Timer.periodic(Duration (seconds: 1), setTime); } void setTime(Timer timer) { setState(() { dateTime = new DateTime.now(); }); } @override void dispose() { _timer.cancel(); super.dispose(); } var weekday = [", "" Monday" and "Tuesday", "on Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]. @override Widget build(BuildContext context) {return Scaffold(body: new Container(child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: EdgeInsets.only(top: 200)). Text( "${dateTime.hour}:${dateTime.minute.toString().padLeft(2,'0')}:${dateTime.second.toString().padLeft(2,'0')}", style: TextStyle( color: Colors.black, fontSize: 18 ), ), Padding( padding: EdgeInsets.only(left: 20) ), Text( "${weekday[dateTime.weekday]}", style: TextStyle( color: Colors.black, fontSize: 18 ), ), Padding( padding: EdgeInsets.only(left: 20) ), Text( "${dateTime.year}/${dateTime.month}/${dateTime.day}", style: TextStyle( color: Colors.black, fontSize: () [(), ((), ((), ((); }}Copy the code
The results