time_listener 0.0.3 copy "time_listener: ^0.0.3" to clipboard
time_listener: ^0.0.3 copied to clipboard

An extremely easy-to-use flutter plugin that allows you to listen time changes.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late TimeListener _listener;
  late DateTime _time;

  @override
  void initState() {
    _time = DateTime.now();
    _listener = TimeListener.create(interval: CheckInterval.seconds);
    WidgetsBinding.instance.addPostFrameCallback(
      (_) => _listener.listen(_handleTimeChange),
    );
    super.initState();
  }

  void _handleTimeChange(DateTime dt) {
    _time = dt;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Current time',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 10.0),
            Text(
              '${_time.hour}:${_time.minute}:${_time.second}',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
    );
  }
}
6
likes
150
points
49
downloads

Publisher

unverified uploader

Weekly Downloads

An extremely easy-to-use flutter plugin that allows you to listen time changes.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on time_listener