timer_builder 1.2.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 76

TimerBuilder #

A widget that rebuilds itself on scheduled, periodic, or dynamically generated time events.

Here are some use cases for this widget:

  • When showing time since or until a specified event;
  • When the model updates frequently but you want to limit UI update frequency;
  • When showing current date or time;
  • When the representation a widget depends on a certain time event.

animated image

Examples #

Periodic rebuild #

import 'package:timer_builder/timer_builder.dart';

class ClockWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return TimerBuilder.periodic(Duration(seconds: 1),
      builder: (context) {
        return Text("${DateTime.now()}");
      }
    );
  }
  
}

Rebuild on a schedule #

import 'package:timer_builder/timer_builder.dart';

class StatusIndicator extends StatelessWidget {

  final DateTime startTime;
  final DateTime endTime;
  
  StatusIndicator(this.startTime, this.endTime);
  
  @override
  Widget build(BuildContext context) {
    return TimerBuilder.scheduled([startTime, endTime],
      builder: (context) {
        final now = DateTime.now();
        final started = now.compareTo(startTime) >= 0;
        final ended = now.compareTo(endTime) >= 0;
        return Text(started ? ended ? "Ended": "Started": "Not Started");
      }
    );
  }
  
}

[1.2.3] - 02/18/2019

  • Updated README

[1.2.2] - 02/18/2019

  • Updated package documentation

[1.2.0] - 02/18/2019

  • Changed date alignment rules, supporting alignment by the day

[1.1.0] - 02/18/2019

  • Introduced factory constructors for scheduled and periodic time events

[1.0.0] - 02/16/2019

  • Initial release

example/lib/example.dart

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

void main() {
  runApp(SampleApp());
}

class SampleApp extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return SampleAppState();
  }

}

class SampleAppState extends State<SampleApp> {

  DateTime alert;

  @override
  void initState() {
    super.initState();
    alert = DateTime.now().add(Duration(seconds: 10));
  }

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sample App'),
        ),
        body:

        TimerBuilder.scheduled([alert],
          builder: (context) {
            // This function will be called once the alert time is reached
            var now = DateTime.now();
            var reached = now.compareTo(alert) >= 0;
            final textStyle = Theme.of(context).textTheme.title;
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(
                    reached ? Icons.alarm_on: Icons.alarm,
                    color: reached ? Colors.red: Colors.green,
                    size: 48,
                  ),
                  !reached ?
                    TimerBuilder.periodic(
                        Duration(seconds: 1),
                        alignment: Duration.zero,
                        builder: (context) {
                          // This function will be called every second until the alert time
                          var now = DateTime.now();
                          var remaining = alert.difference(now);
                          return Text(formatDuration(remaining), style: textStyle,);
                        }
                    )
                    :
                    Text("Alert", style: textStyle),
                  RaisedButton(
                    child: Text("Reset"),
                    onPressed: () {
                      setState(() {
                        alert = DateTime.now().add(Duration(seconds: 10));
                      });
                    },
                  ),
                ],
              ),
            );
          }
        ),
      ),
      theme: ThemeData(
        backgroundColor: Colors.white
      ),
    );
  }
}

String formatDuration(Duration d) {
  String f(int n) {
    return n.toString().padLeft(2, '0');
  }
  // We want to round up the remaining time to the nearest second
  d += Duration(microseconds: 999999);
  return "${f(d.inMinutes)}:${f(d.inSeconds%60)}";
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  timer_builder: ^1.2.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:timer_builder/timer_builder.dart';
  
Version Uploaded Documentation Archive
1.2.3 Feb 19, 2019 Go to the documentation of timer_builder 1.2.3 Download timer_builder 1.2.3 archive
1.2.2 Feb 19, 2019 Go to the documentation of timer_builder 1.2.2 Download timer_builder 1.2.2 archive
1.2.1 Feb 18, 2019 Go to the documentation of timer_builder 1.2.1 Download timer_builder 1.2.1 archive
1.2.0 Feb 18, 2019 Go to the documentation of timer_builder 1.2.0 Download timer_builder 1.2.0 archive
1.1.0 Feb 18, 2019 Go to the documentation of timer_builder 1.1.0 Download timer_builder 1.1.0 archive
1.0.0+2 Feb 17, 2019 Go to the documentation of timer_builder 1.0.0+2 Download timer_builder 1.0.0+2 archive
1.0.0 Feb 17, 2019 Go to the documentation of timer_builder 1.0.0 Download timer_builder 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Mar 6, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.3.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/timer_builder.dart.

Run flutter format to format lib/timer_builder.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test