Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.11 KB | None | 0 0
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3.  
  4. class CurrentDay extends StatefulWidget {
  5.   @override
  6.   _CurrentDayState createState() => _CurrentDayState();
  7. }
  8.  
  9. class _CurrentDayState extends State<CurrentDay> {
  10.   DateTime now = DateTime.now();
  11.   bool update = true;
  12.  
  13.   @override
  14.   void initState() {
  15.     super.initState();
  16.     _generateCurrentDateTimes().listen((date) {
  17.       setState(() {
  18.         this.now = date;
  19.       });
  20.     });
  21.   }
  22.  
  23.   Stream<DateTime> _generateCurrentDateTimes() async* {
  24.     while (update) {
  25.       yield await Future.delayed(Duration(seconds: 1), () => DateTime.now());
  26.     }
  27.   }
  28.  
  29.   @override
  30.   Widget build(BuildContext context) {
  31.     final polandDateTime = now.add(Duration(hours: 1));
  32.     final japanDateTime = polandDateTime.add(Duration(hours: 8));
  33.     return Container(
  34.       child: Row(
  35.         children: <Widget>[
  36.           _localDateTime(title: "Poland", now: polandDateTime),
  37.           _localDateTime(title: "Japan", now: japanDateTime),
  38.         ],
  39.       ),
  40.     );
  41.   }
  42.  
  43.   Widget _localDateTime({String title, DateTime now}) {
  44.     final fix = (number) => number < 10 ? "0$number" : "$number";
  45.     final time = "${fix(now.hour)}:${fix(now.minute)}";
  46.     final date = "${fix(now.day)}.${fix(now.month)}.${now.year}";
  47.     final daytime = now.hour > 6 && now.hour < 20;
  48.     final bgColor = daytime ? Colors.lightBlue[100] : Colors.blueGrey[900];
  49.     final fontColor = daytime ? Colors.black : Colors.white;
  50.     return Expanded(
  51.       child: Container(
  52.         padding: EdgeInsets.symmetric(vertical: 16.0),
  53.         color: bgColor,
  54.         child: Column(
  55.           crossAxisAlignment: CrossAxisAlignment.center,
  56.           children: <Widget>[
  57.             Text(title, style: TextStyle(fontSize: 24.0, color: fontColor)),
  58.             Text(date, style: TextStyle( fontSize: 14.0,color: fontColor)),
  59.             Text(time, style: TextStyle(fontSize: 32.0, color: fontColor))
  60.           ],
  61.         ),
  62.       ),
  63.     );
  64.   }
  65.  
  66.   @override
  67.   void dispose() {
  68.     setState(() {
  69.       this.update = false;
  70.     });
  71.     super.dispose();
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement