| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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<Header> createState() => _HeaderState();
- }
- class _HeaderState extends State<Header> {
- String topCover = 'assets/images/morning.png';
- var welcome = '早上好';
- var isNight = false;
- var date = '';
- List<dynamic> 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<dynamic> 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)));
- }
- }
|