Advertisement
Guest User

asd

a guest
Apr 6th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:http/http.dart';
  5.  
  6. class LoginForm extends StatefulWidget {
  7. @override
  8. _LoginFormState createState() => _LoginFormState();
  9. }
  10.  
  11. class _LoginFormState extends State<LoginForm> {
  12.  
  13. GlobalKey<FormState> _formKey = GlobalKey();
  14. String username = "";
  15. String password = "";
  16. String message ="";
  17.  
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. appBar: AppBar(
  22. title: Center(child: Text("Login Form")),
  23. ),
  24. body: Form(
  25. key: _formKey,
  26. child: Column(
  27. children: <Widget>[
  28. SizedBox(
  29. height: 20,
  30. ),
  31. Text(message),
  32. TextFormField(
  33. decoration: InputDecoration(
  34. hintText: "Username",
  35. icon: Icon(Icons.person),
  36. ),
  37. validator: (value){
  38. if(value.isEmpty) return "Username cannot be empty";
  39. return null;
  40. },
  41. onSaved: (value){
  42. username=value;
  43. },
  44. ),
  45. TextFormField(
  46. decoration: InputDecoration(
  47. hintText: "Password",
  48. icon: Icon(Icons.vpn_key),
  49. ),
  50. validator: (value){
  51. if(value.isEmpty) return "Password cannot be empty";
  52. return null;
  53. },
  54. onSaved: (value){
  55. password=value;
  56. },
  57. ),
  58. RaisedButton(
  59. onPressed: this._onLoginClicked,
  60. child: Text("Login"),
  61. )
  62. ],
  63. ),
  64. ),
  65. );
  66. }
  67.  
  68. _onLoginClicked() async{
  69. if(!_formKey.currentState.validate())return;
  70. _formKey.currentState.save();
  71.  
  72. Map<String,dynamic> body = {
  73. 'username':username,
  74. 'password':password
  75. };
  76. try{
  77. Response response=await post('http://flutter.sochware.com/api/login',body:body);
  78. print("COde : ${response.statusCode}");
  79. print(response.body);
  80. var jsonData=json.decode(response.body);
  81. setState((){
  82. message=jsonData['message'];
  83. });
  84. }catch(error){
  85. print(error);
  86. if(error is SocketException){
  87. setState(() {
  88. message="Internet not available";
  89. });
  90. return;
  91. }
  92.  
  93. }
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement