aboutsummaryrefslogtreecommitdiffstats
path: root/lib/widget_clock.dart
blob: ae9fb26075239ba624413bbbc4ab0511b35e92bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import 'dart:async';

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

class ClockWiddget extends StatefulWidget {
  final double size;
  final Color textColor;

  const ClockWiddget({Key? key, required this.size, required this.textColor})
      : super(key: key);

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

class _ClockWiddgetState extends State<ClockWiddget> {
  late Timer _timer;
  DateTime _now = DateTime.now();

  @override
  void initState() {
    _now = DateTime.now();
    _timer = new Timer.periodic(
      Duration(seconds: 1),
      (Timer timer) {
        setState(() {
          _now = DateTime.now();
        });
      },
    );
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    TextStyle? textStyle = Theme.of(context)
        .textTheme
        .headline2
        ?.copyWith(color: widget.textColor);
    return Container(
      height: widget.size,
      padding: EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(
            width: 1.0,
          ),
        ),
      ),
      child: Column(
        children: [
          FittedBox(
            fit: BoxFit.contain,
            child: Text(
              DateFormat('EEEE').format(_now),
              style: textStyle,
            ),
          ),
          const Divider(thickness: 1),
          FittedBox(
            fit: BoxFit.contain,
            child: Text(
              DateFormat.jm().format(_now),
              style: textStyle,
            ),
          ),
        ],
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      ),
      alignment: Alignment.center,
    );
  }
}