Flutter 如果用户登录正确,如何从表中选择所有数据
我尝试让用户登录到我的应用程序页面。页面正在工作,但我也需要做同样的事情。如果用户登录正确,我需要选择该用户的所有数据 我使用此代码登录:Flutter 如果用户登录正确,如何从表中选择所有数据,flutter,Flutter,我尝试让用户登录到我的应用程序页面。页面正在工作,但我也需要做同样的事情。如果用户登录正确,我需要选择该用户的所有数据 我使用此代码登录: var url = 'http://xxxxxxxx/login_user.php'; var data = {'email': email, 'password' : password}; var response = await http.post(url, body: json.encode(data));
var url = 'http://xxxxxxxx/login_user.php';
var data = {'email': email, 'password' : password};
var response = await http.post(url, body: json.encode(data));
var message = jsonDecode(response.body);
print("tapped ${data}");
if(message == 'Login Matched')
{
setState(() {
visible = false;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => mainpage(email : emailController.text)));
}else{
setState(() {
visible = false;
});
我只有电子邮件和密码。所以如果有人知道我该怎么做,请帮助我
完整代码
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:onefrist/main.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'MyPreferences.dart';
import 'main.dart';
import 'Registration.dart';
void main() => runApp(loginpage());
var id;
class loginpage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.amber,
title: Text('User Login Form')),
body: Center(
child: LoginUser()
)
)
);
}
}
class LoginUser extends StatefulWidget {
LoginUserState createState() => LoginUserState();
}
class LoginUserState extends State <LoginUser>{
MyPreferences _myPreferences = MyPreferences();
@override
void initState() {
// TODO: implement initState
super.initState();
_myPreferences.init().then((value) {
setState(() {
_myPreferences = value;
check_if_already_login();
});
});
}
void check_if_already_login() async {
if (_myPreferences.password !=null && _myPreferences.user !=null) {
Navigator.pushReplacement(
context, new MaterialPageRoute(builder: (context) =>mainpage ()));
}else{
}
}
bool visible = false ;
Future userLogin() async{
setState(() {
visible = true ;
});
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
// final idController = TextEditingController();
// Getting value from Controller
String email = emailController.text;
String password = passwordController.text;
// String id = idController.text;
if(email == '' || password == '')
{
// Put your code here which you want to execute when Text Field is Empty.
print('Text Field is empty, Please Fill All Data');
}else{
var url = 'http://192.168.8.105/login_user.php';
var data = {'email': email, 'password' : password};
var response = await http.post(url, body: json.encode(data));
var message = jsonDecode(response.body);
print("tapped ${data}");
if(message == 'Login Matched')
{
setState(() {
visible = false;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => mainpage(email : emailController.text)));
}else{
setState(() {
visible = false;
});
// Showing Alert Dialog with Response JSON Message.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(message),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);}
}
}
@override
Widget build(BuildContext context) {
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
final idController = TextEditingController();
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text('User Login Form',
style: TextStyle(fontSize: 21))),
Divider(),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: emailController,
onChanged: (value) {
_myPreferences.user = value;
_myPreferences.commit();
},
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Email Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: passwordController,
onChanged: (value) {
_myPreferences.password = value;
_myPreferences.commit();
},
autocorrect: true,
obscureText: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: idController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
RaisedButton(
onPressed: userLogin,
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
child: Text('Click Here To Login'),
),
FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => RegisterUser()
),);
},
child: Text(
"Reistraion page",
style: TextStyle(fontSize: 20.0),
),
),
Visibility(
visible: visible,
child: Container(
margin: EdgeInsets.only(bottom: 30),
child: CircularProgressIndicator()
)
),
],
),
)));
}
}
进口“包装:颤振/材料.省道”;
导入“dart:convert”;
将“package:http/http.dart”导入为http;
导入“package:onefrist/main.dart”;
导入“package:shared_preferences/shared_preferences.dart”;
导入“MyPreferences.dart”;
导入“main.dart”;
输入“Registration.dart”;
void main()=>runApp(loginpage());
变量id;
类loginpage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
背景颜色:Colors.amber,
标题:文本(“用户登录表单”),
正文:中(
子用户:登录用户()
)
)
);
}
}
类LoginUser扩展StatefulWidget{
LoginUserState createState()=>LoginUserState();
}
类LoginUserState扩展状态{
MyPreferences_MyPreferences=MyPreferences();
@凌驾
void initState(){
//TODO:实现initState
super.initState();
_myPreferences.init().then((值){
设置状态(){
_我的偏好=价值;
检查是否已登录();
});
});
}
无效检查\u是否已\u登录()异步{
if(_myPreferences.password!=null&&u myPreferences.user!=null){
导航器。更换(
context,newmaterialpage(builder:(context)=>mainpage());
}否则{
}
}
bool可见=错误;
Future userLogin()异步{
设置状态(){
可见=真实;
});
final emailController=TextEditingController(文本:_myPreferences.user);
最终密码控制器=文本编辑控制器(文本:_myPreferences.password);
//最终idController=TextEditingController();
//从控制器获取值
字符串email=emailController.text;
字符串密码=passwordController.text;
//字符串id=idController.text;
如果(电子邮件=“”| |密码=“”)
{
//将您要在文本字段为空时执行的代码放在此处。
打印('文本字段为空,请填写所有数据');
}否则{
var url='1〕http://192.168.8.105/login_user.php';
var data={'email':email,'password':password};
var response=wait http.post(url,body:json.encode(data));
var message=jsonDecode(response.body);
打印(“点击${data}”);
如果(消息==“登录匹配”)
{
设置状态(){
可见=假;
});
Navigator.push(上下文,materialpage(builder:(context)=>mainpage(email:emailController.text));
}否则{
设置状态(){
可见=假;
});
//显示带有响应JSON消息的警报对话框。
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
标题:新文本(消息),
行动:[
扁平按钮(
子项:新文本(“确定”),
已按下:(){
Navigator.of(context.pop();
},
),
],
);
},
);}
}
}
@凌驾
小部件构建(构建上下文){
final emailController=TextEditingController(文本:_myPreferences.user);
最终密码控制器=文本编辑控制器(文本:_myPreferences.password);
最终idController=TextEditingController();
返回脚手架(
正文:SingleChildScrollView(
儿童:中心(
子:列(
儿童:[
填充物(
填充:常数边集全部(12.0),
子项:文本('用户登录表单',
样式:TextStyle(fontSize:21)),
分隔符(),
容器(
宽度:280,
填充:所有边缘设置(10.0),
孩子:TextField(
控制器:emailController,
一旦更改:(值){
_myPreferences.user=值;
_提交();
},
自动更正:正确,
装饰:输入装饰(hintText:“在此处输入您的电子邮件”),
)
),
容器(
宽度:280,
填充:所有边缘设置(10.0),
孩子:TextField(
控制器:密码控制器,
一旦更改:(值){
_myPreferences.password=值;
_提交();
},
自动更正:正确,
蒙昧文字:对,
装饰:输入装饰(hintText:“在此处输入密码”),
)
),
容器(
宽度:280,
填充:所有边缘设置(10.0),
孩子:TextField(
控制器:idController,
自动更正:正确,
装饰:输入装饰(hintText:“在此处输入密码”),
)
),
升起的按钮(
onPressed:userLogin,
颜色:颜色。绿色,
textColor:Colors.white,
填充:从LTRB(9,9,9,9)开始的边缘设置,
子项:文本(“单击此处登录”),
),
扁平按钮(
print("tapped ${data}");
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:onefrist/main.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'MyPreferences.dart';
import 'main.dart';
import 'Registration.dart';
void main() => runApp(loginpage());
var id;
class loginpage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.amber,
title: Text('User Login Form')),
body: Center(
child: LoginUser()
)
)
);
}
}
class LoginUser extends StatefulWidget {
LoginUserState createState() => LoginUserState();
}
class LoginUserState extends State <LoginUser>{
MyPreferences _myPreferences = MyPreferences();
@override
void initState() {
// TODO: implement initState
super.initState();
_myPreferences.init().then((value) {
setState(() {
_myPreferences = value;
check_if_already_login();
});
});
}
void check_if_already_login() async {
if (_myPreferences.password !=null && _myPreferences.user !=null) {
Navigator.pushReplacement(
context, new MaterialPageRoute(builder: (context) =>mainpage ()));
}else{
}
}
bool visible = false ;
Future userLogin() async{
setState(() {
visible = true ;
});
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
// final idController = TextEditingController();
// Getting value from Controller
String email = emailController.text;
String password = passwordController.text;
// String id = idController.text;
if(email == '' || password == '')
{
// Put your code here which you want to execute when Text Field is Empty.
print('Text Field is empty, Please Fill All Data');
}else{
var url = 'http://192.168.8.105/login_user.php';
var data = {'email': email, 'password' : password};
var response = await http.post(url, body: json.encode(data));
var message = jsonDecode(response.body);
print("tapped ${data}");
if(message == 'Login Matched')
{
setState(() {
visible = false;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => mainpage(email : emailController.text)));
}else{
setState(() {
visible = false;
});
// Showing Alert Dialog with Response JSON Message.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(message),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);}
}
}
@override
Widget build(BuildContext context) {
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
final idController = TextEditingController();
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text('User Login Form',
style: TextStyle(fontSize: 21))),
Divider(),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: emailController,
onChanged: (value) {
_myPreferences.user = value;
_myPreferences.commit();
},
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Email Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: passwordController,
onChanged: (value) {
_myPreferences.password = value;
_myPreferences.commit();
},
autocorrect: true,
obscureText: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: idController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
RaisedButton(
onPressed: userLogin,
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
child: Text('Click Here To Login'),
),
FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => RegisterUser()
),);
},
child: Text(
"Reistraion page",
style: TextStyle(fontSize: 20.0),
),
),
Visibility(
visible: visible,
child: Container(
margin: EdgeInsets.only(bottom: 30),
child: CircularProgressIndicator()
)
),
],
),
)));
}
}
$check = mysqli_fetch_array(mysqli_query($con,$loginQuery));
if($check){
$check['password'] = '';
// Make sure you are not returning the password for security reasons!
$check['result'] = 'Login Matched';
$SuccessMSG = json_encode($check);
echo $SuccessMSG ;
}
else{
$InvalidMSG= array("result"=>"Invalid Username or Password Please Try Again");
$InvalidMSGJSon = json_encode($InvalidMSG);
echo $InvalidMSGJSon ;
}
if(message['result'] == 'Login Matched')
{
// Do Something
}else{
// Do Something
}