splash_screen.dart 1.67 KB
Newer Older
1
import 'dart:async';
Clouard Regis's avatar
Clouard Regis committed
2

3
import 'package:flutter/material.dart';
Clouard Regis's avatar
Clouard Regis committed
4
5

class SplashScreen extends StatefulWidget {
CLOUARD Regis's avatar
CLOUARD Regis committed
6
  final Widget homeWidget;
7

CLOUARD Regis's avatar
CLOUARD Regis committed
8
  SplashScreen({required this.homeWidget, VoidCallback? doInBackground}) {
CLOUARD Regis's avatar
CLOUARD Regis committed
9
10
11
12
    if (doInBackground != null) {
      doInBackground();
    }
  }
13

Clouard Regis's avatar
Clouard Regis committed
14
  @override
CLOUARD Regis's avatar
CLOUARD Regis committed
15
  _SplashScreenState createState() => _SplashScreenState();
Clouard Regis's avatar
Clouard Regis committed
16
17
18
}

class _SplashScreenState extends State<SplashScreen> with TickerProviderStateMixin {
CLOUARD Regis's avatar
CLOUARD Regis committed
19
20
  late final AnimationController? _scaleController;
  double _scale = 1.0;
21

Clouard Regis's avatar
Clouard Regis committed
22
23
24
  @override
  void initState() {
    super.initState();
CLOUARD Regis's avatar
CLOUARD Regis committed
25
26
27
28
29
    _scaleController = buildAnimationController();
  }

  AnimationController buildAnimationController() {
    AnimationController scaleController = AnimationController(
30
      vsync: this,
CLOUARD Regis's avatar
CLOUARD Regis committed
31
      duration: Duration(seconds: 1),
32
    );
CLOUARD Regis's avatar
CLOUARD Regis committed
33
    scaleController.addStatusListener((status) {
Clouard Regis's avatar
Clouard Regis committed
34
      if (status == AnimationStatus.completed) {
CLOUARD Regis's avatar
CLOUARD Regis committed
35
        scaleController.reverse();
Clouard Regis's avatar
Clouard Regis committed
36
      } else if (status == AnimationStatus.dismissed) {
CLOUARD Regis's avatar
CLOUARD Regis committed
37
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => widget.homeWidget));
Clouard Regis's avatar
Clouard Regis committed
38
39
      }
    });
CLOUARD Regis's avatar
CLOUARD Regis committed
40
    scaleController.addListener(() {
Clouard Regis's avatar
Clouard Regis committed
41
      setState(() {
CLOUARD Regis's avatar
CLOUARD Regis committed
42
        _scale = 1 + scaleController.value / 2.0;
Clouard Regis's avatar
Clouard Regis committed
43
44
      });
    });
CLOUARD Regis's avatar
CLOUARD Regis committed
45
    Timer(Duration(milliseconds: 500), () => scaleController.forward());
CLOUARD Regis's avatar
CLOUARD Regis committed
46
    return scaleController;
Clouard Regis's avatar
Clouard Regis committed
47
48
49
50
  }

  @override
  void dispose() {
CLOUARD Regis's avatar
CLOUARD Regis committed
51
    _scaleController?.dispose();
Clouard Regis's avatar
Clouard Regis committed
52
53
54
55
56
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
Clouard Regis's avatar
Clouard Regis committed
57
58
59
60
61
62
63
    return Center(
      child: Transform.scale(
        scale: _scale,
        child: Image.asset(
          'assets/images/splash.webp',
          isAntiAlias: true,
          scale: 2.5,
CLOUARD Regis's avatar
CLOUARD Regis committed
64
        ),
65
66
      ),
    );
Clouard Regis's avatar
Clouard Regis committed
67
68
  }
}