Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show running clock on flutter

I wanted to know if there is any way to show a real time clock in dart? Date and time (e.g 11/14/2018 19:34) and the time will continue to run.

Time can be taken from the device itself.

like image 467
OrrGorenn Avatar asked Nov 14 '18 17:11

OrrGorenn


People also ask

How do you display the clock in flutter?

timer = Timer. periodic(Duration(seconds: 1), (Timer t) => _getTime()); and call timer.

How do you use stopwatch in flutter?

It shows how the stopwatch timer will work in your flutter applications. It shows when code successfully runs, then user press the start timer button, then count down timing will be start and the user also press the stop and cancel timer button. It will be shown on your devices.

How do you set a timer on flutter?

Creating a simple timer Now, to create a simple 3-second timer, add the following, which triggers a callback after it executes: final timer = Timer( const Duration(seconds: 3), () { // Navigate to your favorite place }, ); Once the callback triggers, we can navigate a user to a new screen, for example.


2 Answers

The below uses the intl plugin to format the time into MM/dd/yyyy hh:mm:ss. Make sure to update your pubspec.yaml.

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Time Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Time Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _timeString;

  @override
  void initState() {
    _timeString = _formatDateTime(DateTime.now());
    Timer.periodic(Duration(seconds: 1), (Timer t) => _getTime());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(_timeString),
      ),
    );
  }

  void _getTime() {
    final DateTime now = DateTime.now();
    final String formattedDateTime = _formatDateTime(now);
    setState(() {
      _timeString = formattedDateTime;
    });
  }

  String _formatDateTime(DateTime dateTime) {
    return DateFormat('MM/dd/yyyy hh:mm:ss').format(dateTime);
  }
}
like image 90
Albert Lardizabal Avatar answered Sep 19 '22 09:09

Albert Lardizabal


Simplest solution:

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

class Clock extends StatelessWidget {
  const Clock({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: Stream.periodic(const Duration(seconds: 1)),
        builder: (context, snapshot) {
          return Center(
            child: Text(
              DateFormat('MM/dd/yyyy hh:mm:ss').format(DateTime.now()),
            ),
          );
        },
      ),
    );
  }
}
like image 38
Esen Mehmet Avatar answered Sep 18 '22 09:09

Esen Mehmet