onetime onboarding flutter
Flutter Onboarding Screen only one time from onboarding to Login to Mainscreen
Future < void > main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key ? key
}): super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Colors.transparent, // transparent status bar
statusBarIconBrightness: Brightness.dark // dark text for status bar
));
//get stored int from sharedpref
Future < int > alreadyAUser = IsFirstTime().getOnce();
return MultiProvider(
providers: AppProviders.providers,
child: MaterialApp(
debugShowCheckedModeBanner: false,
routes: AppRoutes.routes(),
home: FutureBuilder(
builder: (ctx, snapshot) {
// Checking if future is resolved or not
if (snapshot.connectionState == ConnectionState.done) {
// If data is present
if (snapshot.hasData) {
final data = snapshot.data as int;
print(data);
switch (data) {
case 0:
return OnboardingScreen();
case 1:
return LoginScreen();
case 2:
return MainScreen();
}
// if we got our data
}
}
// Displaying blank to indicate waiting state
return Container(
color: Colors.white,
);
},
// Future that needs to be resolved
// inorder to display something on the Canvas
future: alreadyAUser,
),
),
);
}
}
class IsFirstTime {
Future < int > getOnce() async {
int ? intValue;
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
if (prefs.getInt("initScreen") == null) {
intValue = 0;
} else {
intValue = prefs.getInt("initScreen");
}
return intValue!;
}
Future once(int num) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//initScreen
await prefs.setInt("initScreen", num);
int initScreen = (prefs.getInt("initScreen")) !;
print('initScreen ${initScreen}');
// return initScreen;
}
}
class OnboardingScreen extends StatefulWidget {
OnboardingScreen({
Key ? key
}): super(key: key);
@override
State < OnboardingScreen > createState() => _onboardingScreenState();
}
class _onboardingScreenState extends State < OnboardingScreen > {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: GestureDetector(
onTap: () {
IsFirstTime().once(1);
//navigate
},
child: Text('Login screen something')),
));
}
}
class login extends StatefulWidget {
login({
Key ? key
}): super(key: key);
@override
State < login > createState() => _loginState();
}
class _loginState extends State < login > {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: GestureDetector(
onTap: () {
IsFirstTime().once(2);
//navigate to mainscreen
},
child: Text('Login screen something')),
));
}
}
Xenophobic Xenomorph