ntp 1.0.4 copy "ntp: ^1.0.4" to clipboard
ntp: ^1.0.4 copied to clipboard

outdated

Fetch accurate time or current DateTime object from (NTP) Network Time Protocol server.

example/lib/main.dart

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

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

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

class _MyAppState extends State<MyApp> {
  DateTime _currentTime;
  DateTime _ntpTime;
  int _ntpOffset;

  @override
  void initState() {
    super.initState();

    _updateTime();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Example of NTP library'),
        ),
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _showData('Current time:', '$_currentTime'),
              _showData('NTP offset:', '$_ntpOffset ms'),
              _showData('NTP time:', '$_ntpTime'),
            ],
          ),
        ),
        floatingActionButton: new FloatingActionButton(
          tooltip: 'Update time',
          child: const Icon(Icons.timer),
          onPressed: _updateTime,
        ),
      ),
    );
  }

  Widget _showData(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          new Text(title,
              style: Theme.of(context)
                  .textTheme
                  .title
                  .copyWith(fontWeight: FontWeight.w500, fontSize: 16.0)),
          new Text(value,
              style: Theme.of(context)
                  .textTheme
                  .title
                  .copyWith(fontWeight: FontWeight.w300, fontSize: 16.0)),
        ],
      ),
    );
  }

  void _updateTime() async {
    _currentTime = DateTime.now();

    NTP.getNtpOffset().then((int value) {
      setState(() {
        _ntpOffset = value;
        _ntpTime = _currentTime.add(Duration(milliseconds: _ntpOffset));
      });
    });
  }
}
213
likes
0
pub points
98%
popularity

Publisher

verified publishersquare-pear.com

Fetch accurate time or current DateTime object from (NTP) Network Time Protocol server.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on ntp