Skip to content

Instantly share code, notes, and snippets.

@akshatapp
Created January 22, 2020 13:34
Show Gist options
  • Save akshatapp/483c4052470c0a42cad7c7b1a331bb9c to your computer and use it in GitHub Desktop.
Save akshatapp/483c4052470c0a42cad7c7b1a331bb9c to your computer and use it in GitHub Desktop.
Flutter Hex Color Clock
// Code Licensed under the Apache License, Version 2.0 - visit : https://github.com/akshatapp/flutter-gist/blob/master/LICENSE
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Hex Color Clock Demo',
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
DateTime now;
Timer _timer;
Color color = Colors.black;
String time = "00:00:00";
String hexCode = "000000";
String hrs = "00";
String min = "00";
String sec = "00";
@override
void initState() {
super.initState();
_updateTime();
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
void _updateTime() {
setState(
() {
now = DateTime.now();
hrs = now.hour.toString().padLeft(2, '0');
min = now.minute.toString().padLeft(2, '0');
sec = now.second.toString().padLeft(2, '0');
time = '$hrs : $min : $sec';
hexCode = '#$hrs$min$sec';
color = rgbValue(hrs, min, sec);
_timer = Timer(
Duration(seconds: 1) - Duration(milliseconds: now.millisecond),
_updateTime,
);
},
);
}
Color rgbValue(h, m, s) {
int r = int.parse(h, radix: 16);
int g = int.parse((m), radix: 16);
int b = int.parse((s), radix: 16);
return Color.fromRGBO(r, g, b, 1.0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
body: Center(
child: Text(
'$time \n $hexCode',
style: TextStyle(fontSize: getFontSize(context), color: Colors.white),
),
),
);
}
}
double getFontSize(BuildContext context) {
final shortSide = MediaQuery.of(context).size.shortestSide;
return ((shortSide * 0.09).roundToDouble());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment