Flutter Onboarding Screen Only One Time - Stack Overflow PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Stack Overflow sign up log in

Questions Jobs Tags Users Badges Ask

0 Flutter Onboarding Screen only one time


flutter dart

i am new to flutter and dart and i am trying to make an app with an onboarding screen with 3 pages and i am
trying to skip the onboarding screen when the user has already opened the app once. I have seen something
similar with the shared preferences, but i couldn't make it work.
This is my onboarding screen:

class OnboardingScreen extends StatefulWidget {


@override
_OnboardingScreenState createState() => _OnboardingScreenState();
}

class _OnboardingScreenState extends State<OnboardingScreen> {

final int _numPages = 3;


final PageController _pageController = PageController(initialPage: 0);
int _currentPage = 0;

List<Widget> _buildPageIndicator() {
List<Widget> list = [];
for (int i = 0; i < _numPages; i++) {
list.add(i == _currentPage ? _indicator(true) : _indicator(false));
}
return list;
}

Widget indicator(bool isActive) {

share improve this question follow

Michael Markou asked


3●2 Dec 9 '19 at 1:46

You can use SharedPreferences to se a value when the first Screen is see. In the main you can load this value and use it to
decide where to route the user. – Guilherme Dec 9 '19 at 2:35

add a comment

1 Answer order by votes

You can use SharedPreferences in main()


2 And set a initScreen variable when first time init, set to 1 to indicate it has initialized
And In MyApp initialRoute check initScreen is null to decide which route to go
You can copy paste run full code below

code snippet
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: initScreen == 0 || initScreen == null ? "first" : "/",
routes: {
'/': (context) => MyHomePage(
title: "demo",
),
"first": (context) => OnboardingScreen(),
},
);
}
}

full code
onTap: () => print('Get Started'),
child: Center(
child: Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(
'Get Started',
style: TextStyle(
color: Color(0xFF5B16D0),
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
)
: Text(''),
);
}
}

share improve this answer follow

chunhunghan answered
18.1k ● 1 ● 19 ● 30 Dec 9 '19 at 2:29

edited
Dec 9 '19 at 2:36

2 Good answer. If he doesn't want to setup a route he can also do... home: initScreen ? OnboardingScreen() :
MyHomePage(), – kent Dec 9 '19 at 2:54

add a comment

Your Answer

Body
Add picture

Log in

OR

Name

Email

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
2020 Stack Exchange, Inc. user contributions under cc by-sa

You might also like