I have an intro screen for my app, but it shows every time I open the app, I need to show that for the 1st time only.
How to do that?
//
You need to store boolean value whenever app is launched first time. For this you need to use shared preferences.
First we create custom class for shared preferences, So you can access it from any page.
import 'package:shared_preferences/shared_preferences.dart';
class MySharedPreferences {
MySharedPreferences._privateConstructor();
static final MySharedPreferences instance =
MySharedPreferences._privateConstructor();
setBooleanValue(String key, bool value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setBool(key, value);
}
Future getBooleanValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getBool(key) ?? false;
}
}
Then we need to check boolean value at every launch of application. If value is false then you must show intro screen otherwise show home screen.
import 'package:flutter/material.dart';
import 'my_shared_preferences.dart';
import 'intro.dart';
import 'home.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State createState() {
// TODO: implement createState
return MyAppState();
}
}
class MyAppState extends State {
// This widget is the root of your application.
bool isFirstTimeOpen = false;
MyAppState() {
MySharedPreferences.instance
.getBooleanValue("firstTimeOpen")
.then((value) => setState(() {
isFirstTimeOpen = value;
}));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: isFirstTimeOpen ? Home() : Intro());
}
}
Also store boolean value true in intro screen so intro screen is not shown everytime. You can also refer example at here.
MySharedPreferences.instance.setBooleanValue("firstTimeOpen", true);