Skip to content

All topics  /  General

Show Current Time in Flutter Example

General

Mar 24, 2022

Hi friends,

In this post, we will learn how to show current time in flutter Example. i explained simply step by step How to Get Current Time on Flutter. Here you will learn Simple how to obtain current time in flutter?. This tutorial will give you simple example How to Display Current Time in Flutter.

I will give you simple Example of how to get current time in flutter.

So let's see bellow example:

import 'package:flutter/material.dart';
import 'dart:async';
void main () => runApp(MyApp());
class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.red),
      home: FlutterTimeDemo(),
    );
  }
}
class FlutterTimeDemo extends StatefulWidget{
  @override
  _FlutterTimeDemoState createState()=> _FlutterTimeDemoState();
}
class _FlutterTimeDemoState extends State
{
  late String _timeString;
  @override
  void initState(){
    _timeString = "${DateTime.now().hour} : ${DateTime.now().minute} :${DateTime.now().second}";
    Timer.periodic(Duration(seconds:1), (Timer t)=>_getCurrentTime());
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Display Current Time'),),
      body:Center(
        child: Text(_timeString, style: TextStyle(fontSize: 30),),
      ),
    );
  }
  void _getCurrentTime()  {
    setState(() {
      _timeString = "${DateTime.now().hour} : ${DateTime.now().minute} :${DateTime.now().second}";
    });
  }
}

Output


I hope it will help you....