change_time.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import 'dart:async';
  2. import 'package:fast/constants.dart';
  3. import 'package:fast/utils/api.dart';
  4. import 'package:fast/utils/global.dart';
  5. import 'package:fast/utils/http_utils.dart';
  6. import 'package:fast/utils/size_fit.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  9. import 'package:get/get.dart';
  10. import 'package:intl/intl.dart';
  11. class ChangeTime extends StatefulWidget {
  12. List<dynamic> list;
  13. var callback;
  14. ChangeTime({Key? key, required this.list,required this.callback}) : super(key: key);
  15. @override
  16. State<ChangeTime> createState() => _ChangeTimeState();
  17. }
  18. class _ChangeTimeState extends State<ChangeTime>
  19. with SingleTickerProviderStateMixin {
  20. late AnimationController controller;
  21. late Animation animation;
  22. late Timer timerUpdate;
  23. String strSysTime = '';
  24. @override
  25. void initState() {
  26. controller = AnimationController(
  27. vsync: this, duration: const Duration(milliseconds: 300));
  28. controller.addListener(() {
  29. setState(() {});
  30. });
  31. animation = Tween(begin: 0.0, end: 1.0).animate(
  32. CurvedAnimation(parent: controller, curve: const Interval(0.0, 1.0)));
  33. controller.forward();
  34. showSystemTime();
  35. super.initState();
  36. }
  37. @override
  38. void dispose() {
  39. controller.dispose();
  40. timerUpdate.cancel();
  41. super.dispose();
  42. }
  43. close() {
  44. controller.reverse();
  45. Timer(const Duration(milliseconds: 300), () {
  46. Navigator.of(context).pop();
  47. });
  48. }
  49. void showSystemTime() {
  50. timerUpdate = Timer.periodic(const Duration(seconds: 1), (e) {
  51. int milliseconds = DateTime.now().millisecondsSinceEpoch;
  52. milliseconds = milliseconds + Global().timeSeconds * 1000;
  53. DateTime dt = DateTime.fromMillisecondsSinceEpoch(milliseconds);
  54. if (mounted) {
  55. setState(() {
  56. strSysTime = DateFormat('yyyy-MM-dd HH:mm:ss').format(dt);
  57. });
  58. }
  59. });
  60. }
  61. DateTime serverTime() {
  62. int milliseconds = DateTime.now().millisecondsSinceEpoch;
  63. milliseconds = milliseconds + Global().timeSeconds * 1000;
  64. return DateTime.fromMillisecondsSinceEpoch(milliseconds);
  65. }
  66. Future updateTimes(seconds) async {
  67. Map<String, dynamic> data =
  68. await HttpUtils.get(Api.fixTime, params: {"fix": seconds});
  69. widget.callback();
  70. }
  71. showPicker(int seconds) {
  72. DateTime dt = DateTime.fromMillisecondsSinceEpoch(seconds*1000);
  73. DatePicker.showDatePicker(context,
  74. showTitleActions: true,
  75. currentTime: dt,
  76. theme: const DatePickerTheme(
  77. headerColor: Colors.orange,
  78. backgroundColor: Colors.white,
  79. itemStyle: TextStyle(
  80. color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18),
  81. doneStyle: TextStyle(color: Colors.white, fontSize: 16)),
  82. onConfirm: (date) {
  83. DatePicker.showTimePicker(context,
  84. showTitleActions: true,
  85. currentTime: dt,
  86. theme: const DatePickerTheme(
  87. headerColor: Colors.orange,
  88. backgroundColor: Colors.white,
  89. itemStyle: TextStyle(
  90. color: Colors.black,
  91. fontWeight: FontWeight.bold,
  92. fontSize: 18),
  93. doneStyle: TextStyle(color: Colors.white, fontSize: 16)),
  94. onConfirm: (date2) {
  95. DateTime dt = DateTime.parse(date.year.toString() +
  96. '-' +
  97. (date.month.toString()).padLeft(2, '0') +
  98. '-' +
  99. (date.day.toString()).padLeft(2, '0') +
  100. 'T' +
  101. (date2.hour.toString()).padLeft(2, '0') +
  102. ':' +
  103. (date2.minute.toString()).padLeft(2, '0') +
  104. ':' +
  105. (date2.second.toString()).padLeft(2, '0'));
  106. int seconds = (dt.millisecondsSinceEpoch -
  107. DateTime.now().millisecondsSinceEpoch) ~/
  108. 1000;
  109. updateTimes(seconds);
  110. });
  111. });
  112. }
  113. @override
  114. Widget build(BuildContext context) {
  115. SizeFit.initialize(context);
  116. double screenHeight = MediaQuery.of(context).size.height;
  117. return Stack(
  118. children: [
  119. GestureDetector(
  120. onTap: () {
  121. close();
  122. },
  123. child: Container(
  124. width: 375.px,
  125. height: screenHeight * 0.2,
  126. color: Colors.transparent,
  127. ),
  128. ),
  129. Positioned(
  130. bottom: -(1 - animation.value) * screenHeight * 0.8,
  131. child: Container(
  132. width: 375.px,
  133. padding: EdgeInsets.all(20.px),
  134. height: screenHeight * 0.8,
  135. decoration: BoxDecoration(
  136. color: const Color(0x99000000),
  137. borderRadius: BorderRadius.only(
  138. topLeft: Radius.circular(16.px),
  139. topRight: Radius.circular(16.px))),
  140. child: Column(
  141. children: [
  142. Row(
  143. children: [
  144. SizedBox(
  145. width: 160.px,
  146. child: Text(
  147. '系统时间',
  148. style:
  149. TextStyle(color: Colors.white, fontSize: 12.px),
  150. ),
  151. ),
  152. SizedBox(
  153. width: 148.px,
  154. child: Text(
  155. strSysTime,
  156. style:
  157. TextStyle(color: Colors.white, fontSize: 12.px),
  158. ),
  159. ),
  160. GestureDetector(
  161. onTap: () {
  162. updateTimes(0);
  163. },
  164. child: Text('重置',
  165. style: TextStyle(
  166. color: Colors.white, fontSize: 12.px)),
  167. )
  168. ],
  169. ),
  170. SizedBox(
  171. height: 20.px,
  172. ),
  173. Expanded(
  174. child: SingleChildScrollView(
  175. child: Column(
  176. mainAxisAlignment: MainAxisAlignment.start,
  177. crossAxisAlignment: CrossAxisAlignment.start,
  178. children: [
  179. ...List<Widget>.generate(widget.list.length, (i) {
  180. return Column(
  181. mainAxisAlignment: MainAxisAlignment.start,
  182. crossAxisAlignment: CrossAxisAlignment.start,
  183. children: [
  184. Container(
  185. width: 334.px,
  186. padding: EdgeInsets.only(bottom: 10.px),
  187. decoration: BoxDecoration(
  188. border: Border(
  189. bottom: BorderSide(
  190. color: const Color(0x4DFFFFFF),
  191. width: 1.px))),
  192. child: Text(
  193. widget.list[i]['day'],
  194. style: TextStyle(
  195. color: kThemeColor, fontSize: 14.px),
  196. ),
  197. ),
  198. Column(
  199. children: [
  200. ...List<Widget>.generate(
  201. widget.list[i]['contents'].length,
  202. (index) {
  203. String strColor = widget.list[i]['contents']
  204. [index]['fColor'];
  205. strColor = strColor.substring(2, 10);
  206. int value = int.parse(strColor, radix: 16);
  207. return Container(
  208. width: 334.px,
  209. height: 40.px,
  210. alignment: Alignment.centerLeft,
  211. decoration: BoxDecoration(
  212. border: Border(
  213. bottom: BorderSide(
  214. color:
  215. const Color(0x4DFFFFFF),
  216. width: 1.px))),
  217. child: Row(
  218. mainAxisSize: MainAxisSize.max,
  219. children: [
  220. SizedBox(
  221. width: 160.px,
  222. child: Text(
  223. widget.list[i]['contents'][index]
  224. ['name'],
  225. style: TextStyle(
  226. color: Color(value),
  227. fontSize: 12.px),
  228. ),
  229. ),
  230. GestureDetector(
  231. onTap: () {
  232. int seconds = int.parse(widget
  233. .list[i]['contents']
  234. [index]['time']) -
  235. (DateTime.now()
  236. .millisecondsSinceEpoch) ~/
  237. 1000;
  238. updateTimes(seconds);
  239. // showPicker();
  240. },
  241. child: SizedBox(
  242. width: 148.px,
  243. child: Text(
  244. widget.list[i]['contents']
  245. [index]['timeStr'],
  246. style: TextStyle(
  247. color: Color(value),
  248. fontSize: 12.px),
  249. ),
  250. )),
  251. GestureDetector(
  252. onTap: () {
  253. showPicker(int.parse(widget
  254. .list[i]['contents']
  255. [index]['time']));
  256. },
  257. child: Text(
  258. '选择',
  259. style: TextStyle(
  260. color: kThemeColor,
  261. fontSize: 12.px),
  262. ))
  263. ],
  264. ),
  265. );
  266. })
  267. ],
  268. )
  269. ],
  270. );
  271. })
  272. ],
  273. ),
  274. ))
  275. ],
  276. ),
  277. )),
  278. Positioned(
  279. left: 0.0,
  280. top: 0.2*screenHeight,
  281. child: GestureDetector(child: Container(width: 300.px,height: 60.px,color: Colors.transparent,),onVerticalDragStart: (details) => {
  282. close()
  283. },))
  284. ],
  285. );
  286. }
  287. }