import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:intl/intl.dart';

class LargeCustomClock extends StatefulWidget {
  const LargeCustomClock({Key? key}) : super(key: key);

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

class _LargeCustomClockState extends State<LargeCustomClock> {
  String _timeString = '';
  String _dateString = '';
  Timer? _timer;

  @override
  void initState() {
    _timeString = _formatTime(DateTime.now());
    _dateString = _formatDate(DateTime.now());
    _timer = Timer.periodic(Duration(seconds: 30), (Timer t) => _getTime());
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          _timeString,
          style: TextStyle(
            fontSize: 38,
            height: 1,
            color: LabColors.text400,
            fontWeight: FontWeight.w300,
          ),
        ),
        Text(
          _dateString,
          style: TextStyle(
            fontSize: 21,
            height: 1,
            color: LabColors.text400,
            fontWeight: FontWeight.w300,
          ),
        ),
      ],
    );
  }

  void _getTime() {
    final String formattedDateTime = _formatDate(DateTime.now());
    final String timeString = _formatTime(DateTime.now());
    setState(() {
      _timeString = timeString;
      _dateString = formattedDateTime;
    });
  }

  String _formatDate(DateTime dateTime) {
    return DateFormat('yyyy年MM月dd日').format(dateTime);
  }

  String _formatTime(DateTime dateTime) {
    return DateFormat('hh:mm a').format(dateTime);
  }
}
