splash_page.dart 1.92 KB
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wow_english/common/core/user_util.dart';
import 'package:wow_english/common/extension/string_extension.dart';
import 'package:wow_english/models/user_entity.dart';
import 'package:wow_english/route/route.dart';
import 'package:wow_english/utils/sp_util.dart';

import '../common/blocs/cachebloc/cache_bloc.dart';

class SplashPage extends StatelessWidget {
  const SplashPage({super.key});

  @override
  Widget build(BuildContext context) {
    return const TransitionView();
  }
}

class TransitionView extends StatefulWidget {
  const TransitionView({super.key});

  @override
  State<StatefulWidget> createState() {
    return _TransitionViewState();
  }
}

class _TransitionViewState extends State<TransitionView> {
  Future startTime() async {
    // 判断是否登录
    UserEntity? userEntity = UserUtil.getUser();
    if (kDebugMode) {
      print('userEntity: $userEntity');
    }
    Timer(const Duration(seconds: 2), () {
      if (userEntity != null && userEntity.token.isNotEmpty) {
        context.read<CacheBloc>().add(UserInfoChangeEvent(userEntity));
        Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.home, (route) => false);
      } else {
        Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false);
      }
    });
  }

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

  void init() async {
    await SpUtil.preInit();
    startTime();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                    'splash'.assetGif,
                  ),
                  fit: BoxFit.fill)),
        ),
      ),
    );
  }
}