0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AndroidStudio:flutter(dart)で現在日時をマイクロ秒の粒度で表示

Posted at

やりたいこと

  • AndroidStudio:flutter(dart)で、現在日時をマイクロ秒の粒度で表示する。

実装

main.dart


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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ClockState();
  }
}

class _ClockState extends State<MyHomePage> {
  String _time = '';
  @override
  void initState() {
    Timer.periodic(
      Duration(microseconds: 1000), // 1000000で1秒
      _onTimer,
    );
    super.initState();
  }

  void _onTimer(Timer timer) {
    setState(() => _time = DateTime.now().toString());
  }

  @override
  Widget build(BuildContext context) {
    return Text(
      _time,
      style: TextStyle(
          fontSize: 50.0,
          color: Colors.yellow,
          backgroundColor: Colors.blue
      ),
    );
  }
}

画面

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?