Created
February 7, 2021 09:59
-
-
Save febritecno/11ef8ba4c5a963c3b01dba46705ba27f to your computer and use it in GitHub Desktop.
Kumpulan Flutter Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/widgets.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
/// [Widget] that determines if an app has been opened and if not, displays an intro screen | |
class FirstTimeScreen extends StatefulWidget { | |
/// [Widget] that is displayed as a placeholder until it determines which route to show | |
final Widget loadingScreen; | |
/// [PageRoute] that goes to the intro screen, if the app is opened for the first time | |
final PageRoute introScreen; | |
/// [PageRoute] that goes to the landing screen, if the app has already been opened | |
final PageRoute landingScreen; | |
FirstTimeScreen( | |
{Key key, | |
this.loadingScreen = const Center(child: CircularProgressIndicator()), | |
@required this.introScreen, | |
@required this.landingScreen}) | |
: super(key: key); | |
_FirstTimeScreenState createState() => _FirstTimeScreenState(); | |
} | |
class _FirstTimeScreenState extends State<FirstTimeScreen> { | |
Future _checkFirstSeen() async { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
bool _seen = (prefs.getBool('intro_screen_seen') ?? false); | |
if (_seen) { | |
Navigator.of(context).pushReplacement(widget.landingScreen); | |
} else { | |
prefs.setBool('intro_screen_seen', true); | |
Navigator.of(context).pushReplacement(widget.introScreen); | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
new Timer(new Duration(milliseconds: 200), () { | |
_checkFirstSeen(); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return widget.loadingScreen; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
first_time_screen.dart -> management first time app loaded turn into intro_page.
how deal to use with getx router ?
static const INITIAL = Routes.HOME;
static final routes = [
....
GetPage(
name: INITIAL,
page: (){ return FirstTimeScreen(
loadingScreen: Text("Loading"), // loading screen splash app
introScreen: GetPageRoute(page:()=> IntroPage()), //intro slider or other
landingScreen: GetPageRoute(page:()=> HomePage()), // launch to home_page
);}
);