Shared Preferences Login and Home Screen Validation

 import "package:flutter/material.dart";

import "package:shared_preferences/shared_preferences.dart";
import "package:wscube_tech/8th_April_2024_Monday/HomeScreen.dart";
import "package:wscube_tech/8th_April_2024_Monday/Splash_Screen.dart";
import "package:wscube_tech/8th_April_2024_Monday/custom_Widget_LoginScreen.dart";

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

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Shared Preferences",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const SplashScreenEighth(),
);
}
}

class LoginScreenShared extends StatefulWidget {
const LoginScreenShared({super.key});

@override
State<LoginScreenShared> createState() {
return LoginScreenSharedState();
}
}

class LoginScreenSharedState extends State<LoginScreenShared> {
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();

bool _obscureText = true;

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
return FocusManager.instance.primaryFocus?.unfocus();
},
child: Scaffold(
appBar: AppBar(
title: const Text(
"Shared Preferences",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: SizedBox(
width: 360.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
UiHelperShared.customTextFormField(
controller: emailController,
text: "Enter the Email",
text2: "Email",
iconData: Icons.email,
),
const SizedBox(height: 30.0),
UiHelperShared.customTextFormField(
controller: passwordController,
text: "Enter the password",
text2: "Password",
iconData: Icons.remove_red_eye,
),
const SizedBox(height: 40.0),
ElevatedButton(
onPressed: () {
saveLogin(emailController.text.toString(),
passwordController.text.toString());
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0, 50.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
),
child: const Text(
"Login",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
],
),
),
),
),
);
}

saveLogin(String email, String password) async {
if (email == "" || password == "") {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Enter required fields"),
),
);
} else {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool("login", true);

Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return HomeScreenShared();
},
),
);

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Boolean value is set to True"),
duration: Duration(seconds: 3),
),
);
}
}
}
import "package:flutter/material.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:wscube_tech/8th_April_2024_Monday/Shared_preferences.dart";

class HomeScreenShared extends StatelessWidget {
const HomeScreenShared({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: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"Home Screen",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 40.0),
),
const SizedBox(height: 40.0),
ElevatedButton(
onPressed: () async {
SharedPreferences preferences =
await SharedPreferences.getInstance();
preferences.setBool("login", false);

Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return const LoginScreenShared();
},
),
);

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Boolean value is set to False"),
duration: Duration(seconds: 3),
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(300.0, 50.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
child: const Text(
"Back to Login Screen",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
],
),
),
);
}
}

import "package:flutter/material.dart";

class UiHelperShared {
static customTextFormField(
{required TextEditingController controller,
required String text,
required String text2,
required IconData iconData}) {
return TextFormField(
controller: controller,
decoration: InputDecoration(
hintText: text,
labelText: text2,
suffixIcon: IconButton(
onPressed: () {},
icon: Icon(iconData),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12.0),
),
),
);
}
}

import "dart:async";
import "package:flutter/material.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:wscube_tech/8th_April_2024_Monday/HomeScreen.dart";
import "package:wscube_tech/8th_April_2024_Monday/Shared_preferences.dart";

class SplashScreenEighth extends StatefulWidget {
const SplashScreenEighth({super.key});

@override
State<SplashScreenEighth> createState() {
return SplashScreenEighthState();
}
}

class SplashScreenEighthState extends State<SplashScreenEighth> {
@override
void initState() {
super.initState();
Timer(
const Duration(seconds: 3),
() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
bool? value = preferences.getBool("login");

if (value != null) {
if (value) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const HomeScreenShared();
},
),
);
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const LoginScreenShared();
},
),
);
}
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) {
return const LoginScreenShared();
},
),
);
}
},
);
}

@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text(
"Splash Screen",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0,
),
),
),
);
}
}





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