Countdown Timer In Flutter

Countdown Timer In Flutter :

Screenshot :

Countdown Timer In Flutter

adv_column.dart

import 'package:flutter/material.dart';

class AdvColumn extends StatelessWidget {
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  final Widget divider;
  final Key key;
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final CrossAxisAlignment crossAxisAlignment;
  final TextDirection textDirection;
  final VerticalDirection verticalDirection;
  final TextBaseline textBaseline;
  final List<Widget> children;

  AdvColumn({
    this.padding,
    this.margin,
    this.divider,
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })  : this.key = key,
        this.mainAxisAlignment = mainAxisAlignment,
        this.mainAxisSize = mainAxisSize,
        this.crossAxisAlignment = crossAxisAlignment,
        this.textDirection = textDirection,
        this.verticalDirection = verticalDirection,
        this.textBaseline = textBaseline,
        this.children = children;

  @override
  Widget build(BuildContext context) {
    List<Widget> newChildren = _rebuildChildren();

    return Container(
        padding: padding,
        margin: margin,
        child: Column(
          key: key,
          mainAxisAlignment: mainAxisAlignment,
          mainAxisSize: mainAxisSize,
          crossAxisAlignment: crossAxisAlignment,
          textDirection: textDirection,
          verticalDirection: verticalDirection,
          textBaseline: textBaseline,
          children: newChildren,
        ));
  }

  List<Widget> _rebuildChildren() {
    if (children.length == 0) return [];
    List<Widget> newChildren = [];

    for (Widget child in children) {
      if (child != null) {
        newChildren.add(child);
        if (divider != null) newChildren.add(divider);
      }
    }

    if (newChildren.length > 0 && divider != null)
      newChildren.removeAt(newChildren.length - 1);

    return newChildren;
  }
}

class ColumnDivider extends StatelessWidget {
  final double size;
  final Color color;

  ColumnDivider(double size, {Color color})
      : this.size = size ?? 0.0,
        this.color = color ?? Colors.transparent;

  @override
  Widget build(BuildContext context) {
    return Container(height: size, color: color);
  }
}

adv_countdown.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'adv_column.dart';
import 'adv_row.dart';

class AdvCountdown extends StatefulWidget {
  final DateTime futureDate;
  final TextStyle style;

  AdvCountdown({this.futureDate, TextStyle style})
      : this.style = style ??
            TextStyle(
                color: Colors.black,
                fontSize: 30.0,
                fontWeight: FontWeight.w600);

  @override
  State<StatefulWidget> createState() => AdvCountdownState();
}

class AdvCountdownState extends State<AdvCountdown> {
  TextStyle _numberStyle;
  TextStyle _datepartStyle;
  Timer _timer;

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

    _numberStyle = widget.style.copyWith(fontWeight: FontWeight.w700);
    _datepartStyle = widget.style;
    _timer = Timer.periodic(Duration(seconds: 1), (_) {
      if (this.mounted)setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    Duration duration = widget.futureDate.difference(DateTime.now());
    if (duration.isNegative) {
      _numberStyle = _numberStyle.copyWith(color: Colors.black);
      _datepartStyle = _datepartStyle.copyWith(color: Colors.black);
    }

    int day = duration.inDays.abs();
    int hour = (duration.inHours.abs() % 24);
    int minute = (duration.inMinutes.abs() % 60);
    int second = (duration.inSeconds.abs() % 60);
    String dayDatePart =
        day.abs() == 1 ? "day": "days";
    String hourDatePart =
        hour.abs() == 1 ? "hour" : "hours";
    String minuteDatePart = minute.abs() == 1
        ? "minute"
        : "minutes";
    String secondDatePart = second.abs() == 1
        ? "second"
        : "seconds";
    Widget separator = Text(":", style: _numberStyle);
    List<Widget> children = [];

    if (minute == 0 && hour == 0 && day == 0) {
      children.add(
          _buildItem(second * (duration.isNegative ? -1 : 1), secondDatePart));
    } else {
      children.addAll([separator, _buildItem(second, secondDatePart)]);
    }

    if (minute != 0) {
      if (hour == 0 && day == 0) {
        children.insert(
            0,
            _buildItem(
                minute * (duration.isNegative ? -1 : 1), minuteDatePart));
      } else {
        children.insert(0, _buildItem(minute, minuteDatePart));
        children.insert(0, separator);
      }
    }

    if (hour != 0) {
      if (day == 0) {
        children.insert(
            0, _buildItem(hour * (duration.isNegative ? -1 : 1), hourDatePart));
      } else {
        children.insert(0, _buildItem(hour, hourDatePart));
        children.insert(0, separator);
      }
    }

    if (day != 0) {
      children.insert(
          0, _buildItem(day * (duration.isNegative ? -1 : 1), dayDatePart));
    }

    return AdvRow(
        mainAxisSize: MainAxisSize.min,
        divider: RowDivider(8.0),
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children);
  }

  Widget _buildItem(int amount, String datePart) {
    List<Widget> children = [];

    children.add(Text("$amount", style: _numberStyle));
    children.add(Text("$datePart",
        style: _datepartStyle.copyWith(fontSize: _datepartStyle.fontSize * 0.4)));

    return AdvColumn(children: children);
  }
}

adv_row.dart

import 'package:flutter/material.dart';

class AdvRow extends StatelessWidget {
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  final Widget divider;
  final Key key;
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final CrossAxisAlignment crossAxisAlignment;
  final TextDirection textDirection;
  final VerticalDirection verticalDirection;
  final TextBaseline textBaseline;
  final List<Widget> children;

  AdvRow({
    this.padding,
    this.margin,
    this.divider,
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })  : this.key = key,
        this.mainAxisAlignment = mainAxisAlignment,
        this.mainAxisSize = mainAxisSize,
        this.crossAxisAlignment = crossAxisAlignment,
        this.textDirection = textDirection,
        this.verticalDirection = verticalDirection,
        this.textBaseline = textBaseline,
        this.children = children;

  @override
  Widget build(BuildContext context) {
    List<Widget> newChildren = _rebuildChildren();

    return Container(
      padding: padding,
      margin: margin,
      child: Row(
        key: key,
        mainAxisAlignment: mainAxisAlignment,
        mainAxisSize: mainAxisSize,
        crossAxisAlignment: crossAxisAlignment,
        textDirection: textDirection,
        verticalDirection: verticalDirection,
        textBaseline: textBaseline,
        children: newChildren,
      ),
    );
  }

  List<Widget> _rebuildChildren() {
    if (children.length == 0) return [];
    List<Widget> newChildren = [];

    for (Widget child in children) {
      if (child != null) {
        newChildren.add(child);
        if (divider != null) newChildren.add(divider);
      }
    }

    if (newChildren.length > 0 && divider != null) newChildren.removeAt(newChildren.length - 1);

    return newChildren;
  }
}

class RowDivider extends StatelessWidget {
  final double size;
  final Color color;

  RowDivider(double size, {Color color})
      : this.size = size ?? 0.0,
        this.color = color ?? Colors.transparent;

  @override
  Widget build(BuildContext context) {
    return Container(width: size, height: 1.0, color: color);
  }
}

main.dart

import 'package:flutter/material.dart';

import 'adv_countdown.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tutorial',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Countdown Timer in Flutter'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final DateTime alarmDate =  DateTime(2019, 10, 12, 15, 10);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body:  AdvCountdown(futureDate: alarmDate),
    );
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in