berkeresvan / flutter

1 stars 0 forks source link

sharedpreferences_login #48

Open berkeresvan opened 7 months ago

berkeresvan commented 7 months ago

import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sharedpreferences_login/Anasayfa.dart';

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

class MyApp extends StatelessWidget { Future OturumKontrol() async { var sp = await SharedPreferences.getInstance();

String spKullaniciAdi = sp.getString("KullaniciAdi") ?? "Kullanıcı adı yok";
String spSifre = sp.getString("Sifre") ?? "sifre yok";

if (spKullaniciAdi == "admin" && spSifre == "123") {
  return true;
} else {
  return false;
}

}

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: FutureBuilder( future: OturumKontrol(), builder: (context, snapshot) { if (snapshot.hasData) { bool? gecisIzni = snapshot.data; return gecisIzni! ? const Anasayfa() : LoginEkrani(); } else { return Container(); } }, ), ); } }

class LoginEkrani extends StatefulWidget { @override State createState() => _LoginEkraniState(); }

class _LoginEkraniState extends State { var tfKullaniciadi = TextEditingController(); var tfSifre = TextEditingController();

var scaffoldKey = GlobalKey();

Future girisKontrol() async { var ka = tfKullaniciadi.text; var s = tfSifre.text;

if (ka == "admin" && s == "123") {
  var sp = await SharedPreferences.getInstance();
  sp.setString("kullaniciadi", ka);
  sp.setString("sifre", s);
  Navigator.pushReplacement(
      context, MaterialPageRoute(builder: (context) => Anasayfa()));
} else {
  ScaffoldMessenger.of(context)
      .showSnackBar(SnackBar(content: Text("giriş hatalı")));
}

}

@override Widget build(BuildContext context) { return Scaffold( key: scaffoldKey, appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text("Login Ekranı"), ), body: Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextField( controller: tfKullaniciadi, decoration: InputDecoration(hintText: "Kullanıcı Adı"), ), TextField( obscureText: true, controller: tfSifre, decoration: InputDecoration(hintText: "Şifre"), ), ElevatedButton( onPressed: () { girisKontrol(); }, child: Text("Giriş Yap")) ], ), ), ), ); } }

berkeresvan commented 7 months ago

import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sharedpreferences_login/main.dart';

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

@override State createState() => _AnasayfaState(); }

class _AnasayfaState extends State { String? spKullaniciAdi; String? spSifre; Future OturumBilgisiOku() async { var sp = await SharedPreferences.getInstance(); setState(() { spKullaniciAdi = sp.getString("KullaniciAdi") ?? "Kullanıcı adı yok"; spSifre = sp.getString("Sifre") ?? "sifre yok"; }); }

Future CikisYap() async { var sp = await SharedPreferences.getInstance(); sp.remove("KullaniciAdi"); sp.remove("Sifre"); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => LoginEkrani())); }

@override void initState() { // TODO: implement initState super.initState(); OturumBilgisiOku(); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Aanasayfa"), actions: [ IconButton( onPressed: () { CikisYap(); }, icon: Icon(Icons.exit_to_app), ) ], ), body: Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Kullanıcı Adı : $spKullaniciAdi ", style: TextStyle( fontSize: 30, ), ), Text( "Şifre : $spSifre", style: TextStyle( fontSize: 30, ), ), ], ), ), ), ); } }