Skip to content

Instantly share code, notes, and snippets.

@godilite
Created February 5, 2020 04:00
Show Gist options
  • Save godilite/eb0fefd5ab7456f459dd2f4cbe4491b0 to your computer and use it in GitHub Desktop.
Save godilite/eb0fefd5ab7456f459dd2f4cbe4491b0 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:tutorial_app/screen/login.dart';
import 'package:tutorial_app/screen/home.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test App',
debugShowCheckedModeBanner: false,
home: CheckAuth(),
);
}
}
class CheckAuth extends StatefulWidget {
@override
_CheckAuthState createState() => _CheckAuthState();
}
class _CheckAuthState extends State<CheckAuth> {
bool isAuth = false;
@override
void initState() {
_checkIfLoggedIn();
super.initState();
}
void _checkIfLoggedIn() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var token = localStorage.getString('token');
if(token != null){
setState(() {
isAuth = true;
});
}
}
@override
Widget build(BuildContext context) {
Widget child;
if (isAuth) {
child = Home();
} else {
child = Login();
}
return Scaffold(
body: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment