import 'package:flutter/material.dart';
import '../common/app_colors.dart';
import '../common/assets.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  //计数变量
  final duration = 10;
  int number = 0;

  //倒计时函数
  Future<void> _countdown() async {
    number = duration;
    for (var i = 0; i < duration; i++) {
      await Future.delayed(const Duration(seconds: 1), () {
        // 除非 [mounted] 为 true，否则调用 [setState] 是错误的。
        if (mounted == true) {
          setState(() {
            number--;
          });
        }
      });
    }

    if (number == 0) {
      print('倒计时结束 ');
    }
  }

  @override
  void initState() {
    super.initState();
    _countdown();
  }

  //logo
  Widget _buildLogo() {
    return Stack(
      alignment: Alignment.center,
      children: [
        //底部白色背景
        Container(
          // color: Colors.white,
          width: 120,
          height: 120,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(48),
          ),
        ),

        //上层图片
        Image.asset(
          AssetsImages.logopang,
          width: 84,
          height: 80,
        )
      ],
    );
  }

  //text方法抽出
  Text _bulidText(String text) {
    return Text(
      text,
      style: const TextStyle(
        fontSize: 19,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.bold,
        color: Colors.white,
        height: 22 / 19,
      ),
    );
  }

  //主视图 拆成函数
  Widget _buildView(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          //logo
          _buildLogo(),

          const SizedBox(
            height: 24,
          ),

          //标题
          _bulidText('Online Market'),

          const SizedBox(
            height: 24,
          ),

          //计数器
          _bulidText(number > 0 ? '$number' : 'done'),

          //end
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildView(context),
      backgroundColor: AppColors.backgroundSplash,
    );
  }
}
