diff --git a/lib/main.dart b/lib/main.dart index c0b042f..ca73085 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,53 +1,67 @@ -import 'package:blogapp/Blog/addBlog.dart'; -import 'package:blogapp/Pages/HomePage.dart'; -import 'package:blogapp/Profile/MainProfile.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:google_fonts/google_fonts.dart'; import 'Pages/LoadingPage.dart'; import 'Pages/WelcomePage.dart'; -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'Pages/HomePage.dart'; void main() { runApp(MyApp()); } -class MyApp extends StatefulWidget { +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData( + textTheme: GoogleFonts.openSansTextTheme( + Theme.of(context).textTheme, + ), + ), + home: AuthenticationWrapper(), + ); + } +} + +class AuthenticationWrapper extends StatefulWidget { @override - _MyAppState createState() => _MyAppState(); + _AuthenticationWrapperState createState() => _AuthenticationWrapperState(); } -class _MyAppState extends State { +class _AuthenticationWrapperState extends State { Widget page = LoadingPage(); final storage = FlutterSecureStorage(); + @override void initState() { - // TODO: implement initState super.initState(); checkLogin(); } - void checkLogin() async { - String token = await storage.read(key: "token"); - if (token != null) { - setState(() { - page = HomePage(); - }); - } else { + Future checkLogin() async { + try { + String token = await storage.read(key: "token"); + if (token != null) { + setState(() { + page = HomePage(); + }); + } else { + setState(() { + page = WelcomePage(); + }); + } + } catch (e) { + print("Error occurred: $e"); setState(() { - page = WelcomePage(); + page = WelcomePage(); // Navigate to welcome page in case of error }); } } @override Widget build(BuildContext context) { - return MaterialApp( - theme: ThemeData( - textTheme: GoogleFonts.openSansTextTheme( - Theme.of(context).textTheme, - ), - ), - home: page, - ); + return page; } } + +