Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- class CurrentDay extends StatefulWidget {
- @override
- _CurrentDayState createState() => _CurrentDayState();
- }
- class _CurrentDayState extends State<CurrentDay> {
- DateTime now = DateTime.now();
- bool update = true;
- @override
- void initState() {
- super.initState();
- _generateCurrentDateTimes().listen((date) {
- setState(() {
- this.now = date;
- });
- });
- }
- Stream<DateTime> _generateCurrentDateTimes() async* {
- while (update) {
- yield await Future.delayed(Duration(seconds: 1), () => DateTime.now());
- }
- }
- @override
- Widget build(BuildContext context) {
- final polandDateTime = now.add(Duration(hours: 1));
- final japanDateTime = polandDateTime.add(Duration(hours: 8));
- return Container(
- child: Row(
- children: <Widget>[
- _localDateTime(title: "Poland", now: polandDateTime),
- _localDateTime(title: "Japan", now: japanDateTime),
- ],
- ),
- );
- }
- Widget _localDateTime({String title, DateTime now}) {
- final fix = (number) => number < 10 ? "0$number" : "$number";
- final time = "${fix(now.hour)}:${fix(now.minute)}";
- final date = "${fix(now.day)}.${fix(now.month)}.${now.year}";
- final daytime = now.hour > 6 && now.hour < 20;
- final bgColor = daytime ? Colors.lightBlue[100] : Colors.blueGrey[900];
- final fontColor = daytime ? Colors.black : Colors.white;
- return Expanded(
- child: Container(
- padding: EdgeInsets.symmetric(vertical: 16.0),
- color: bgColor,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Text(title, style: TextStyle(fontSize: 24.0, color: fontColor)),
- Text(date, style: TextStyle( fontSize: 14.0,color: fontColor)),
- Text(time, style: TextStyle(fontSize: 32.0, color: fontColor))
- ],
- ),
- ),
- );
- }
- @override
- void dispose() {
- setState(() {
- this.update = false;
- });
- super.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement