Skip to content

Instantly share code, notes, and snippets.

@samuchakraborty
Last active July 4, 2023 05:30
Show Gist options
  • Save samuchakraborty/3a79cdc14c9622266e22226b79e94b80 to your computer and use it in GitHub Desktop.
Save samuchakraborty/3a79cdc14c9622266e22226b79e94b80 to your computer and use it in GitHub Desktop.
SharedPrefs data
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: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
LoginPageState createState() => LoginPageState();
}
class LoginPageState extends State<LoginPage> {
bool _isChecked = false;
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@override
void initState() {
super.initState();
loadData();
}
void loadData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? email = prefs.getString("email");
String? password = prefs.getString("password");
if (email != null) {
setState(() {
emailController.text = email.toString();
});
}
if (password != null) {
setState(() {
passwordController.text = password.toString();
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: const Text("Login Page"),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
CustomTextField(
controller: emailController,
icon: const Icon(Icons.email_outlined),
label: "Enter Email",
),
const SizedBox(
height: 10,
),
CustomTextField(
controller: passwordController,
icon: const Icon(Icons.lock),
label: "Enter password",
),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
height: 24.0,
width: 24.0,
child: Theme(
data: ThemeData(
unselectedWidgetColor: const Color(0xff00C8E8),
),
child: Checkbox(
activeColor: const Color(0xff00C8E8),
value: _isChecked,
onChanged: (value) {
setState(
() {
_isChecked = value!;
},
);
},
),
),
),
const SizedBox(width: 10.0),
const Text(
"Remember Me",
style: TextStyle(
color: Color(0xff646464),
fontSize: 12,
fontFamily: 'Rubic',
),
)
],
),
const SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () {
getLogin();
},
child: const Text(
"Login",
style: TextStyle(color: Colors.blue),
),
),
],
),
),
),
);
}
void getLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (_isChecked) {
prefs.setString("email", emailController.text);
prefs.setString("password", passwordController.text);
}
if (!mounted) return;
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const LogoutPage(),
),
);
}
}
class CustomTextField extends StatelessWidget {
const CustomTextField(
{super.key,
required this.icon,
required this.controller,
required this.label});
final TextEditingController controller;
final Icon icon;
final String label;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(
color: Colors.white,
blurRadius: 5.0,
),
],
borderRadius: BorderRadius.circular(30),
border: Border.all(
color: const Color(0xffECEBEB),
),
),
child: TextField(
controller: controller,
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(top: 8, left: 20),
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
border: InputBorder.none,
suffixIcon: icon,
labelText: label,
labelStyle:
const TextStyle(fontSize: 14, decoration: TextDecoration.none),
),
),
);
}
}
class LogoutPage extends StatelessWidget {
const LogoutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Logout"),
actions: [
GestureDetector(
onTap: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const LoginPage(),
),
(route) => false);
},
child: const Icon(Icons.logout),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment