header.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'dart:async';
  2. import 'package:fast/utils/api.dart';
  3. import 'package:fast/utils/global.dart';
  4. import 'package:fast/utils/http_utils.dart';
  5. import 'package:fast/utils/size_fit.dart';
  6. import 'package:flutter/material.dart';
  7. class Header extends StatefulWidget {
  8. bool isIndexPage;
  9. Header({Key? key, required this.isIndexPage}) : super(key: key);
  10. @override
  11. State<Header> createState() => _HeaderState();
  12. }
  13. class _HeaderState extends State<Header> {
  14. String topCover = 'assets/images/morning.png';
  15. var welcome = '早上好';
  16. var isNight = false;
  17. var date = '';
  18. List<dynamic> list = [];
  19. Timer? _timer;
  20. @override
  21. void dispose() {
  22. if (_timer != null) {
  23. _timer!.cancel();
  24. }
  25. super.dispose();
  26. }
  27. @override
  28. void initState() {
  29. updateTime();
  30. getWelcome();
  31. _timer = Timer.periodic(const Duration(seconds: 10), (timer) {
  32. updateTime();
  33. });
  34. super.initState();
  35. }
  36. Future getWelcome() async {
  37. List<dynamic> data = await HttpUtils.get(Api.prompts);
  38. setState(() {
  39. list = data;
  40. });
  41. updateTime();
  42. }
  43. updateTime() {
  44. var now = DateTime.now();
  45. var hour = now.hour;
  46. String img = 'assets/images/morning.png';
  47. var t = '';
  48. if (hour >= 4 && hour < 6) {
  49. t = list.isNotEmpty?list[0]['prompt']:'凌晨好';
  50. img = 'assets/images/evening.png';
  51. isNight = true;
  52. } else if (hour >= 18 || hour < 4) {
  53. t = list.isNotEmpty?list[4]['prompt']:'晚上好!晚上早点休息、断食期间不要吃夜宵哦~';
  54. img = 'assets/images/evening.png';
  55. isNight = true;
  56. } else if (hour < 8) {
  57. t = list.isNotEmpty?list[0]['prompt']:'早安!一天好心情哦~';
  58. } else if (hour < 11) {
  59. t = list.isNotEmpty?list[1]['prompt']:'上午好!';
  60. img = 'assets/images/shangwu.png';
  61. } else if (hour < 14) {
  62. t = list.isNotEmpty?list[2]['prompt']:'午安!';
  63. img = 'assets/images/middle.png';
  64. } else if (hour < 18) {
  65. t = list.isNotEmpty?list[3]['prompt']:'下午好!早点吃完晚餐,餐后开始断食哦~';
  66. img = 'assets/images/afternoon.png';
  67. } else {
  68. t = list.isNotEmpty?list[4]['prompt']:'晚上好!晚上早点休息、断食期间不要吃夜宵哦~';
  69. img = 'assets/images/evening.png';
  70. isNight = true;
  71. }
  72. setState(() {
  73. welcome = t;
  74. date = now.year.toString() +
  75. '-' +
  76. now.month.toString().padLeft(2, '0') +
  77. '-' +
  78. now.day.toString().padLeft(2, '0');
  79. topCover = img;
  80. isNight = isNight;
  81. });
  82. }
  83. @override
  84. Widget build(BuildContext context) {
  85. SizeFit.initialize(context);
  86. var size = MediaQuery.of(context).size;
  87. String nickname = '';
  88. if (Global().userBean != null) {
  89. nickname = Global().userBean!.nickname!;
  90. }
  91. return Container(
  92. width: size.width,
  93. height: size.height <= 667 ? 155.px : 180.px,
  94. padding: EdgeInsets.fromLTRB(
  95. 24.px, size.height <= 667 ? 50.px : 68.px, 24.px, 0),
  96. alignment: Alignment.topLeft,
  97. child: Row(
  98. mainAxisAlignment: MainAxisAlignment.start,
  99. crossAxisAlignment: CrossAxisAlignment.start,
  100. children: [
  101. if (Global().userBean != null && widget.isIndexPage)
  102. ClipOval(
  103. child: Image.network(
  104. Global().userBean!.avatar!,
  105. width: 64.px,
  106. height: 64.px,
  107. fit: BoxFit.cover,
  108. ),
  109. ),
  110. SizedBox(
  111. width: 10.px,
  112. ),
  113. SizedBox(
  114. width: 253.px,
  115. child: Column(
  116. crossAxisAlignment: CrossAxisAlignment.start,
  117. children: [
  118. if (widget.isIndexPage)
  119. Text(nickname,
  120. style: TextStyle(
  121. color: isNight ? Colors.white : Colors.black,
  122. fontSize: 20.px,
  123. fontWeight: FontWeight.bold)),
  124. SizedBox(height: 4.px),
  125. if (widget.isIndexPage)
  126. Text(welcome,
  127. style: TextStyle(
  128. height: 1.0,
  129. color: isNight ? Colors.white : Colors.black,
  130. fontSize: 16.px)),
  131. SizedBox(height: 4.px),
  132. if (widget.isIndexPage)
  133. Text(date,
  134. style: TextStyle(
  135. color: isNight
  136. ? const Color(0x99FFFFFF)
  137. : const Color(0x99000000),
  138. fontSize: 12.px))
  139. ],
  140. ),
  141. )
  142. ],
  143. ),
  144. decoration: BoxDecoration(
  145. image: DecorationImage(
  146. alignment: Alignment.bottomLeft,
  147. image: AssetImage(topCover),
  148. fit: BoxFit.cover)));
  149. }
  150. }