Created
January 23, 2023 13:12
-
-
Save yusufbiberoglu/3612d6646cc6e977d62ca5b64dc84863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:blog_app/home_screen.dart'; | |
import 'package:blog_app/service/api.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
class SignIn extends StatefulWidget { | |
const SignIn({Key? key}) : super(key: key); | |
static const route = '/signin'; | |
@override | |
State<SignIn> createState() => _SignInState(); | |
} | |
class _SignInState extends State<SignIn> { | |
TextEditingController emailController = TextEditingController(); | |
TextEditingController passwordController = TextEditingController(); | |
login() async { | |
var data = { | |
'email': emailController.text, | |
'password': passwordController.text, | |
}; | |
var res = await Api().login(data); | |
var body = json.decode(res.body); | |
if (body['code'] == 401) { | |
ScaffoldMessenger.of(context) | |
.showSnackBar(const SnackBar(content: Text('Invalid Credentials'))); | |
} | |
if (body['token'] != null) { | |
SharedPreferences localStorage = await SharedPreferences.getInstance(); | |
localStorage.setString('token', body['token']); | |
Navigator.push( | |
context, MaterialPageRoute(builder: (context) => HomeScreen())); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Flutter-Symfony Rest API'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 24), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
TextField( | |
controller: emailController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
hintText: 'E-mail', | |
labelStyle: TextStyle(fontSize: 18), | |
), | |
), | |
const SizedBox(height: 24), | |
TextField( | |
controller: passwordController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
hintText: 'Password', | |
labelStyle: TextStyle(fontSize: 18), | |
), | |
), | |
const SizedBox(height: 24), | |
Align( | |
alignment: Alignment.center, | |
child: ElevatedButton( | |
onPressed: () { | |
login(); | |
}, | |
child: const Text( | |
'Sign In', | |
style: TextStyle(fontSize: 16), | |
), | |
), | |
), | |
const SizedBox(height: 16), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment