Home > Mobile >  Im trying to create a splash screen on flutter. Its showing error like these
Im trying to create a splash screen on flutter. Its showing error like these

Time:10-05

lib/Splash.dart:36:28: Error: Type 'DiagnosticPropertiesBuilder' not found. void debugFillProperties(DiagnosticPropertiesBuilder properties) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/Splash.dart:36:28: Error: 'DiagnosticPropertiesBuilder' isn't a type. void debugFillProperties(DiagnosticPropertiesBuilder properties) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/Splash.dart:38:20: Error: The method 'DiagnosticsProperty' isn't defined for the class '_SplashState'.

  • '_SplashState' is from 'package:g1/Splash.dart' ('lib/Splash.dart'). Try correcting the name to the name of an existing method, or defining a method named 'DiagnosticsProperty'. properties.add(DiagnosticsProperty('initState', initState)); ^^^^^^^^^^^^^^^^^^^

CodePudding user response:

Flutter actually gives a simpler way to add Splash Screen to our application. We first need to design a basic page as we design other app screens. You need to make it a StatefulWidget since the state of this will change in a few seconds.

  import 'dart:async';
  import 'package:flutter/material.dart';
  import 'home.dart';

   class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
  }

  class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
  super.initState();
    Timer(
       Duration(seconds: 3),
       () => Navigator.of(context).pushReplacement(MaterialPageRoute(
        builder: (BuildContext context) => HomeScreen())));
  }

  @override
  Widget build(BuildContext context) {
  return Scaffold(
  backgroundColor: Colors.white,
  body: Center(
    child: Image.asset('assets/splash.png'),
    ),
   );
 }
}

Logic Inside the initState(), call a Timer() with the duration, as you wish, I made it 3 seconds, once done push the navigator to Home Screen of our application.

Note: The application should show the splash screen only once, the user should not go back to it again on back button press. For this, we use Navigator.pushReplacement(), It will move to a new screen and remove the previous screen from the navigation history stack.

CodePudding user response:

Try this packages flutter_native_splash , animated_splash_screen or splashscreen

  • Related