hey guys i want to make login page in flutter with PHP i want to parse the data from flutter to PHP code and return the result but i cant make it because when i run the code and try to login there is no response from the server and didn’t give my anything
this is my flutter code:
import 'dart:convert';
import 'dart:ui';
import 'ReturnPass.dart';
import 'package:flutter/material.dart';
import 'CreateAcconut.dart';
import 'Add_ads.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.transparent,
body: //uploudd(),
SingIn(),
)));
}
class SingIn extends StatefulWidget {
const SingIn({key});
@override
State<SingIn> createState() => _SingInState();
}
class _SingInState extends State<SingIn> {
//This is method icon visibility_off OR visibility_sharp use in TextField
Icon oricon(int namber) {
if (namber == 1) {
return Icon(
Icons.visibility_off,
color: Color(0xff072A52),
);
} else {
return Icon(
Icons.visibility_sharp,
color: Color(0xff072A52),
);
}
}
TextEditingController name = new TextEditingController();
TextEditingController pasword = new TextEditingController();
var TorF = true;
int iconnumber = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: SingleChildScrollView(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage('assets/sing2.png'),
),
),
//
//'images/sing.png'
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 230,
),
//Image.asset('images/sing.png'),
//=============================This is TextField Email ==========================================
Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
controller: name,
// maxLength: 15,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
labelText: " email addres ",
labelStyle: TextStyle(
fontSize: 25,
color: Color.fromRGBO(244, 172, 71, 1),
fontWeight: FontWeight.w600),
hintText: " [email protected] ",
suffix: Icon(
Icons.person,
size: 45,
color: Colors.white,
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Color(0xff072A52),
width: 4,
)),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0xff072A52), width: 4),
)),
onSaved: (value) => name = value as TextEditingController,
),
),
//=============================This is TextField Password ==========================================
SizedBox(
height: 15,
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10,
),
child: TextFormField(
controller: pasword,
decoration: InputDecoration(
filled: true, //true حط fillcolor عشان تفعل ال
fillColor: Colors.white,
labelText: ' Password ',
labelStyle: TextStyle(
color: Color.fromRGBO(244, 172, 71, 1),
fontSize: 25,
fontWeight: FontWeight.w600),
hintText: " Your Password",
//prefixIcon: Icon(Icons.person,size: 30,),
suffix: IconButton(
onPressed: () {
setState(() {
if (TorF == true) {
TorF = false;
iconnumber = 1;
} else if (TorF == false) {
iconnumber = 2;
TorF = true;
}
});
},
icon: oricon(iconnumber),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Color(0xff072A52),
width: 4,
)),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0xff072A52), width: 4),
)),
obscureText: TorF,
onSaved: (value) => pasword = value as TextEditingController,
),
),
//=============================This is ElevatedButton ==========================================
SizedBox(
height: 15,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: ConstrainedBox(
constraints:
BoxConstraints.tightFor(width: 400, height: 70),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Color(0xff072A52),
shape: BeveledRectangleBorder(
borderRadius:
BorderRadiusDirectional.circular(4))),
onPressed: () {
setState(() {
print(name.text.toString());
print(pasword.text.toString());
});
// Navigator.of(context).push(
// MaterialPageRoute(
// builder: (context) => welcome(
// paswordd: pasword.text, uernamee: name.text)
// )
// );
},
child: Text(
"login",
style: TextStyle(fontSize: 30),
),
Future ReadData() async {
var res = await http.post(Uri.parse('http://111.111.111.111/login.php'),
body: {'username': name, 'password': pasword});
print(res.body);
}
),
),
),
//This is TextButton حقت تغير كلمة المرور
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
children: [
Align(
alignment: Alignment.topLeft,
child: TextButton(
onPressed: (() {
Navigator.of(context).push(
MaterialPageRoute(builder: ((context) {
return Return_Password();
})));
}),
child: Text(
'Forget password?',
style: TextStyle(
color: Colors.red,
fontSize: 18,
fontWeight: FontWeight.w600),
)),
),
SizedBox(
width: 80,
),
//This is Textbutton Create account
Align(
child: TextButton(
onPressed: (() {
Navigator.of(context).push(
MaterialPageRoute(builder: ((context) {
return Create_Account();
})));
}),
child: Text(
'create account',
style: TextStyle(
color: Color.fromRGBO(66, 169, 210, 1),
fontSize: 18,
fontWeight: FontWeight.w600),
)),
)
],
),
),
SizedBox(
height: 200,
),
],
),
),
),
),
)),
);
}
}
and this is my php code:
<?php
include('config.php');
$name = $_POST['name'];
$pasword = $_POST['pasword'];
$query = "SELECT * FROM users WHERE username='$name' AND password='$pasword'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
// login successful
session_start();
$_SESSION['username'] = $username;
echo json_encode(['status' => 'success']);
} else {
// login failed
echo json_encode(['status' => 'failed']);
}
?>