import 'dart:async'; 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'; class Header extends StatefulWidget { bool isIndexPage; Header({Key? key, required this.isIndexPage}) : super(key: key); @override State
createState() => _HeaderState(); } class _HeaderState extends State
{ String topCover = 'assets/images/morning.png'; var welcome = '早上好'; var isNight = false; var date = ''; List list = []; Timer? _timer; @override void dispose() { if (_timer != null) { _timer!.cancel(); } super.dispose(); } @override void initState() { updateTime(); getWelcome(); _timer = Timer.periodic(const Duration(seconds: 10), (timer) { updateTime(); }); super.initState(); } Future getWelcome() async { List data = await HttpUtils.get(Api.prompts); setState(() { list = data; }); updateTime(); } updateTime() { var now = DateTime.now(); var hour = now.hour; String img = 'assets/images/morning.png'; var t = ''; if (hour >= 4 && hour < 6) { t = list.isNotEmpty?list[0]['prompt']:'凌晨好'; img = 'assets/images/evening.png'; isNight = true; } else if (hour >= 18 || hour < 4) { t = list.isNotEmpty?list[4]['prompt']:'晚上好!晚上早点休息、断食期间不要吃夜宵哦~'; img = 'assets/images/evening.png'; isNight = true; } else if (hour < 8) { t = list.isNotEmpty?list[0]['prompt']:'早安!一天好心情哦~'; } else if (hour < 11) { t = list.isNotEmpty?list[1]['prompt']:'上午好!'; img = 'assets/images/shangwu.png'; } else if (hour < 14) { t = list.isNotEmpty?list[2]['prompt']:'午安!'; img = 'assets/images/middle.png'; } else if (hour < 18) { t = list.isNotEmpty?list[3]['prompt']:'下午好!早点吃完晚餐,餐后开始断食哦~'; img = 'assets/images/afternoon.png'; } else { t = list.isNotEmpty?list[4]['prompt']:'晚上好!晚上早点休息、断食期间不要吃夜宵哦~'; img = 'assets/images/evening.png'; isNight = true; } setState(() { welcome = t; date = now.year.toString() + '-' + now.month.toString().padLeft(2, '0') + '-' + now.day.toString().padLeft(2, '0'); topCover = img; isNight = isNight; }); } @override Widget build(BuildContext context) { SizeFit.initialize(context); var size = MediaQuery.of(context).size; String nickname = ''; if (Global().userBean != null) { nickname = Global().userBean!.nickname!; } return Container( width: size.width, height: size.height <= 667 ? 155.px : 180.px, padding: EdgeInsets.fromLTRB( 24.px, size.height <= 667 ? 50.px : 68.px, 24.px, 0), alignment: Alignment.topLeft, child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ if (Global().userBean != null && widget.isIndexPage) ClipOval( child: Image.network( Global().userBean!.avatar!, width: 64.px, height: 64.px, fit: BoxFit.cover, ), ), SizedBox( width: 10.px, ), SizedBox( width: 253.px, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (widget.isIndexPage) Text(nickname, style: TextStyle( color: isNight ? Colors.white : Colors.black, fontSize: 20.px, fontWeight: FontWeight.bold)), SizedBox(height: 4.px), if (widget.isIndexPage) Text(welcome, style: TextStyle( height: 1.0, color: isNight ? Colors.white : Colors.black, fontSize: 16.px)), SizedBox(height: 4.px), if (widget.isIndexPage) Text(date, style: TextStyle( color: isNight ? const Color(0x99FFFFFF) : const Color(0x99000000), fontSize: 12.px)) ], ), ) ], ), decoration: BoxDecoration( image: DecorationImage( alignment: Alignment.bottomLeft, image: AssetImage(topCover), fit: BoxFit.cover))); } }