as you know, some apps have splash screen. I have learned the make splash screen with native codes on Flutter.
For example: android/app/src/main/res/drawable/launch_background.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/background" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/hwalogo" />
</item>
</layer-list>
So,I will have splash screen without dart. Also, iOS side has too. Before the that, I was creating splash screen like homepage or login. My question is which one should I use to be more suitable? Many Thanks..
CodePudding user response:
Maybe both of them, you can use native splash screen utils to get rid of the white screen when you open the app.
At the same time, you can use a splash screen that you create with dart and make some api calls, async jobs etc.
Or you can use a prebuilt splash screens like splashscreen or easy_splash_screen.
So it is about your needs, do the one that suits your case.
CodePudding user response:
I have created a splash screen using Dart code itself. Sharing the code for reference :
import 'package:flutter/material.dart';
import '../constants/constants.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State<StatefulWidget> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
bool? loginCheck; //checking the shared preference and navigating accordingly
@override
void initState() {
super.initState();
loginCheck = prefs!.getBool('login');
navigate();
}
@override
Widget build(BuildContext context) {
return const Material(
child: Center(child: Text('Loading....')),
);
}
navigate() async {
if (loginCheck == true) {
Future(() => Navigator.of(context).pushReplacementNamed('/home'));
} else {
Future(() => Navigator.of(context).pushReplacementNamed('/login'));
}
}
}
Additionally, you can provide a delay of some seconds manually but the above code actually does the job.