Session Management with Shared Preferences Simple

 import "package:flutter/material.dart";

import "package:shared_preferences/shared_preferences.dart";

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Shared Preferences Session Management",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: const SplashScreen2(),
);
}
}

class SplashScreen2 extends StatelessWidget {
const SplashScreen2({super.key});

@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: checkLoggedInStatus(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.data == true) {
return HomeScreen();
} else {
return LoginScreen();
}
},
);
}

Future<bool> checkLoggedInStatus() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getBool("keyName") ?? false;
}
}

class LoginScreen extends StatelessWidget {
const LoginScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Login Screen",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: ElevatedButton(
onPressed: () async {
SharedPreferences preferences =
await SharedPreferences.getInstance();
await preferences.setBool("keyName", true);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return HomeScreen();
},
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(250.0, 50.0),
),
child: const Text(
"Login",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
),
);
}
}

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Home Screen",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: ElevatedButton(
onPressed: () async {
SharedPreferences preferences =
await SharedPreferences.getInstance();
await preferences.setBool("keyName", false);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return LoginScreen();
},
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(250.0, 50.0),
),
child: const Text(
"Logout",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter