import 'dart:async'; import 'package:fast/constants.dart'; import 'package:fast/utils/api.dart'; import 'package:fast/utils/global.dart'; import 'package:fast/utils/http_utils.dart'; import 'package:fast/utils/size_fit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; class ChangeTime extends StatefulWidget { List list; var callback; ChangeTime({Key? key, required this.list,required this.callback}) : super(key: key); @override State createState() => _ChangeTimeState(); } class _ChangeTimeState extends State with SingleTickerProviderStateMixin { late AnimationController controller; late Animation animation; late Timer timerUpdate; String strSysTime = ''; @override void initState() { controller = AnimationController( vsync: this, duration: const Duration(milliseconds: 300)); controller.addListener(() { setState(() {}); }); animation = Tween(begin: 0.0, end: 1.0).animate( CurvedAnimation(parent: controller, curve: const Interval(0.0, 1.0))); controller.forward(); showSystemTime(); super.initState(); } @override void dispose() { controller.dispose(); timerUpdate.cancel(); super.dispose(); } close() { controller.reverse(); Timer(const Duration(milliseconds: 300), () { Navigator.of(context).pop(); }); } void showSystemTime() { timerUpdate = Timer.periodic(const Duration(seconds: 1), (e) { int milliseconds = DateTime.now().millisecondsSinceEpoch; milliseconds = milliseconds + Global().timeSeconds * 1000; DateTime dt = DateTime.fromMillisecondsSinceEpoch(milliseconds); if (mounted) { setState(() { strSysTime = DateFormat('yyyy-MM-dd HH:mm:ss').format(dt); }); } }); } DateTime serverTime() { int milliseconds = DateTime.now().millisecondsSinceEpoch; milliseconds = milliseconds + Global().timeSeconds * 1000; return DateTime.fromMillisecondsSinceEpoch(milliseconds); } Future updateTimes(seconds) async { Map data = await HttpUtils.get(Api.fixTime, params: {"fix": seconds}); widget.callback(); } showPicker(int seconds) { DateTime dt = DateTime.fromMillisecondsSinceEpoch(seconds*1000); DatePicker.showDatePicker(context, showTitleActions: true, currentTime: dt, theme: const DatePickerTheme( headerColor: Colors.orange, backgroundColor: Colors.white, itemStyle: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18), doneStyle: TextStyle(color: Colors.white, fontSize: 16)), onConfirm: (date) { DatePicker.showTimePicker(context, showTitleActions: true, currentTime: dt, theme: const DatePickerTheme( headerColor: Colors.orange, backgroundColor: Colors.white, itemStyle: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18), doneStyle: TextStyle(color: Colors.white, fontSize: 16)), onConfirm: (date2) { DateTime dt = DateTime.parse(date.year.toString() + '-' + (date.month.toString()).padLeft(2, '0') + '-' + (date.day.toString()).padLeft(2, '0') + 'T' + (date2.hour.toString()).padLeft(2, '0') + ':' + (date2.minute.toString()).padLeft(2, '0') + ':' + (date2.second.toString()).padLeft(2, '0')); int seconds = (dt.millisecondsSinceEpoch - DateTime.now().millisecondsSinceEpoch) ~/ 1000; updateTimes(seconds); }); }); } @override Widget build(BuildContext context) { SizeFit.initialize(context); double screenHeight = MediaQuery.of(context).size.height; return Stack( children: [ GestureDetector( onTap: () { close(); }, child: Container( width: 375.px, height: screenHeight * 0.2, color: Colors.transparent, ), ), Positioned( bottom: -(1 - animation.value) * screenHeight * 0.8, child: Container( width: 375.px, padding: EdgeInsets.all(20.px), height: screenHeight * 0.8, decoration: BoxDecoration( color: const Color(0x99000000), borderRadius: BorderRadius.only( topLeft: Radius.circular(16.px), topRight: Radius.circular(16.px))), child: Column( children: [ Row( children: [ SizedBox( width: 160.px, child: Text( '系统时间', style: TextStyle(color: Colors.white, fontSize: 12.px), ), ), SizedBox( width: 148.px, child: Text( strSysTime, style: TextStyle(color: Colors.white, fontSize: 12.px), ), ), GestureDetector( onTap: () { updateTimes(0); }, child: Text('重置', style: TextStyle( color: Colors.white, fontSize: 12.px)), ) ], ), SizedBox( height: 20.px, ), Expanded( child: SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ ...List.generate(widget.list.length, (i) { return Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: 334.px, padding: EdgeInsets.only(bottom: 10.px), decoration: BoxDecoration( border: Border( bottom: BorderSide( color: const Color(0x4DFFFFFF), width: 1.px))), child: Text( widget.list[i]['day'], style: TextStyle( color: kThemeColor, fontSize: 14.px), ), ), Column( children: [ ...List.generate( widget.list[i]['contents'].length, (index) { String strColor = widget.list[i]['contents'] [index]['fColor']; strColor = strColor.substring(2, 10); int value = int.parse(strColor, radix: 16); return Container( width: 334.px, height: 40.px, alignment: Alignment.centerLeft, decoration: BoxDecoration( border: Border( bottom: BorderSide( color: const Color(0x4DFFFFFF), width: 1.px))), child: Row( mainAxisSize: MainAxisSize.max, children: [ SizedBox( width: 160.px, child: Text( widget.list[i]['contents'][index] ['name'], style: TextStyle( color: Color(value), fontSize: 12.px), ), ), GestureDetector( onTap: () { int seconds = int.parse(widget .list[i]['contents'] [index]['time']) - (DateTime.now() .millisecondsSinceEpoch) ~/ 1000; updateTimes(seconds); // showPicker(); }, child: SizedBox( width: 148.px, child: Text( widget.list[i]['contents'] [index]['timeStr'], style: TextStyle( color: Color(value), fontSize: 12.px), ), )), GestureDetector( onTap: () { showPicker(int.parse(widget .list[i]['contents'] [index]['time'])); }, child: Text( '选择', style: TextStyle( color: kThemeColor, fontSize: 12.px), )) ], ), ); }) ], ) ], ); }) ], ), )) ], ), )), Positioned( left: 0.0, top: 0.2*screenHeight, child: GestureDetector(child: Container(width: 300.px,height: 60.px,color: Colors.transparent,),onVerticalDragStart: (details) => { close() },)) ], ); } }