Flutter True Time

Flutter True Time:

In this tutorial, we going to learn Flutter custom time settings manually based on the location of the user to manual changes to device clock time.

Most of the times it becomes important to get the real or “true” date and time. sometimes the clock has been changed manually, then a new DateTime instance gives you a time impacted by local settings.

Use do clock settings based on his requirements based on his geographical location. And sometimes what happens is device clock is not updated automatically at that time we have to set the clock manually. some times we set device clock time early to punctual to reach a place or to attend a meeting. you can create manual time functionality in flutter app using TrueTime package.

This plugin is based on truetime in Android and TrueTime in iOS for the whole SNTP protocol implementation.

we can implement this manual device clock setting functionality in Flutter for both Android and IOS apps.

Creating TrueTime/ Manual Clock time Settings in Flutter:

Follow the below procedure to create TrueTime Functionality in Flutter Aap.

Add the dependency package:

To create TrueTime Functionality if flutter we need to add the dependency package to pubspec.yaml file. use the below code to add dependency package. After adding the dependency package run the get package method to import all the required files to the app.

dependencies:
true_time: ^0.1.0

Install the package:

You can install the package from the command line using the below code with Flutter as shown.

$ flutter packages get

Importing the Package:

After, Adding the dependency package to the pubspec.yaml file, you can now import the package into the dart code by using the below code. without adding the dependency package to the pubspec.yaml file if you import it will show package not found an error.

import 'package:true_time/true_time.dart';

Complete Example Code for Flutter TrueTime: 

Copy and Paste Below code into your main.dart file.

Main.dart

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

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

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State {
bool _initialized = false;
DateTime _currentTime;

@override
initState() {
super.initState();
_initPlatformState();
}

_initPlatformState() async {
bool initialized = await TrueTime.init();
setState(() {
_initialized = initialized;
});
_updateCurrentTime();
}

_updateCurrentTime() async {
DateTime now = await TrueTime.now();
setState(() {
_currentTime = now;
});
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
floatingActionButton: new FloatingActionButton(
tooltip: 'Get current time',
child: new Icon(Icons.timer),
onPressed: _updateCurrentTime,
),
appBar: new AppBar(
title: new Text('Example TrueTime app'),
),
body: new Column(
children: [
new Text('TrueTime is initialized: $_initializedn'),
new Text('Current Time: $_currentTimen'),
],
),
),
);
}
}

Congratulations You Have Learned Flutter TrueTime Or Manual Device  Clock Settings ….!!!

If It Is Helpful Share It With Your Friends….!!!

Leave a Reply

Categories