main_page.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'dart:async';
  2. import 'dart:math' as math;
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_clock/model/config.dart';
  5. import 'package:intl/intl.dart';
  6. class MainPage extends StatefulWidget {
  7. @override
  8. State<StatefulWidget> createState() {
  9. return _MainPageState();
  10. }
  11. }
  12. class _MainPageState extends State<MainPage> {
  13. final _utcMidnightRadiansOffset = radiansFromDegrees(-64);
  14. static const _secondsInDay = 86400;
  15. DateTime _localTime = DateTime.now();
  16. @override
  17. void initState() {
  18. super.initState();
  19. Timer.periodic(Duration(seconds: 1),
  20. (_) => setState(() => _localTime = DateTime.now()));
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. backgroundColor: Colors.white,
  26. body: SingleChildScrollView(
  27. child: Center(
  28. child: Column(
  29. children: [
  30. Text(
  31. "Clock",
  32. style: TextStyle(
  33. fontSize: 30,
  34. color: Colors.red,
  35. ),
  36. ),
  37. Text(
  38. "author: 天问",
  39. style: TextStyle(fontFamily: Config.defaultFontFamily),
  40. ),
  41. Text(
  42. "contact: liuyuqi.gov@msn.cn",
  43. style: TextStyle(fontFamily: Config.defaultFontFamily),
  44. ),
  45. SizedBox(
  46. height: 5,
  47. ),
  48. Stack(
  49. children: [
  50. Image.asset(
  51. 'assets/face.png',
  52. width: 350,
  53. ),
  54. Transform.rotate(
  55. angle: -(radiansFromTime(_localTime.toUtc()) +
  56. _utcMidnightRadiansOffset),
  57. child: ClipOval(
  58. clipper: InnerFaceClipper(),
  59. child: Image.asset(
  60. 'assets/face.png',
  61. width: 350,
  62. ),
  63. ),
  64. ),
  65. ],
  66. ),
  67. Text(DateFormat.EEEE().format(_localTime),
  68. style: TextStyle(fontSize: 48)),
  69. Text(DateFormat.yMMMMd().format(_localTime),
  70. style: TextStyle(fontSize: 20)),
  71. Text(DateFormat.jms().format(_localTime),
  72. style: TextStyle(fontSize: 42)),
  73. ],
  74. ),
  75. ),
  76. ));
  77. }
  78. static double radiansFromDegrees(double degrees) => degrees * math.pi / 180;
  79. static double radiansFromTime(DateTime time) {
  80. final midnightToday = DateTime(time.year, time.month, time.day);
  81. final secondsSinceMidnight = midnightToday.difference(time).inSeconds;
  82. final percent = secondsSinceMidnight / _secondsInDay;
  83. final degrees = percent * 360;
  84. return radiansFromDegrees(degrees);
  85. }
  86. }
  87. class InnerFaceClipper extends CustomClipper<Rect> {
  88. final percent = 0.85;
  89. @override
  90. Rect getClip(Size size) => Rect.fromCenter(
  91. center: size.center(Offset(0, 0)),
  92. width: size.width * percent,
  93. height: size.height * percent,
  94. );
  95. @override
  96. bool shouldReclip(CustomClipper oldClipper) => true;
  97. }