Guest User

login

a guest
Jan 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 14.42 KB | None | 0 0
  1. import 'dart:_http';
  2. import 'dart:convert';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:restaurant_app/globalVar.dart';
  6. import 'package:restaurant_app/homescreen.dart';
  7. import 'package:restaurant_app/models/auth.dart';
  8. import 'package:restaurant_app/signup.dart';
  9. import 'package:http/http.dart' as http;
  10.  
  11. class SignIn extends StatefulWidget {
  12.   final Post post;
  13.  
  14.   SignIn({Key key, this.post}) : super(key: key);
  15.  
  16.   @override
  17.   _SignInState createState() => _SignInState();
  18. }
  19.  
  20. class _SignInState extends State<SignIn> with SingleTickerProviderStateMixin {
  21.   TabController controller;
  22.  
  23.   @override
  24.   void initState() {
  25.     // TODO: implement initState
  26.     super.initState();
  27.     controller = new TabController(length: 2, vsync: this);
  28.   }
  29.  
  30.   @override
  31.   void dispose() {
  32.     // TODO: implement dispose
  33.     super.dispose();
  34.     controller.dispose();
  35.   }
  36.  
  37.   Future<Post> getAuth() async {
  38.     final response = await http.get(
  39.         "${GlobalVar.Ip}wp-json/jwt-auth/v1/token",
  40.         headers: {HttpHeaders.authorizationHeader: ''});
  41.     final responseJson = json.decode(response.body);
  42.     return Post.fromJson(responseJson);
  43.   }
  44.  
  45.   String validateEmail(String value) {
  46.     Pattern pattern =
  47.         r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
  48.     RegExp regex = new RegExp(pattern);
  49.     if (!regex.hasMatch(value))
  50.       return 'Enter Valid Email';
  51.     else
  52.       return null;
  53.   }
  54.  
  55.   String validatePassword(String value) {
  56.     if (value.length < 4)
  57.       return 'Password must be more than 2 charater';
  58.     else
  59.       return null;
  60.   }
  61.  
  62.   final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  63.   bool _autoValidate = false;
  64.   String _password;
  65.   String _email;
  66.  
  67.   void _validateInputs() {
  68.     if (_formKey.currentState.validate()) {
  69. //    If all data are correct then save data to out variables
  70.       _formKey.currentState.save();
  71.       Navigator.push(
  72.           context, MaterialPageRoute(builder: (context) => HomePage()));
  73.     } else {
  74. //    If all data are not valid then start auto validation.
  75.       setState(() {
  76.         _autoValidate = true;
  77.       });
  78.     }
  79.   }
  80.  
  81.   @override
  82.   Widget build(BuildContext context) {
  83.     return Scaffold(
  84.       resizeToAvoidBottomPadding: false,
  85.       body: Container(
  86.         decoration: BoxDecoration(
  87.           image: DecorationImage(
  88.             image: AssetImage('images/art.png'),
  89.             fit: BoxFit.fill,
  90.             colorFilter: ColorFilter.mode(
  91.                 Colors.white12.withOpacity(0.2), BlendMode.dstATop),
  92.           ),
  93.         ),
  94.         child: ListView(
  95.           shrinkWrap: true,
  96.           physics: BouncingScrollPhysics(),
  97.           children: <Widget>[
  98.             SizedBox(
  99.               height: MediaQuery.of(context).size.height / 30,
  100.             ),
  101.             Align(
  102.               alignment: Alignment.topCenter,
  103.               child: CircleAvatar(
  104.                 backgroundColor: Colors.grey,
  105.                 radius: 55.0,
  106.                 backgroundImage: AssetImage('images/logo.png'),
  107.               ),
  108.             ),
  109.             SizedBox(
  110.               height: MediaQuery.of(context).size.height / 30,
  111.             ),
  112.             Stack(
  113.               alignment: Alignment.center,
  114.               children: <Widget>[
  115.                 SizedBox(
  116.                   height: MediaQuery.of(context).size.height / 300,
  117.                   child: new Center(
  118.                     child: new Container(
  119.                       height: 10.0,
  120.                       color: Colors.black12,
  121.                     ),
  122.                   ),
  123.                 ),
  124.                 Row(
  125.                   children: <Widget>[
  126.                     SizedBox(
  127.                       width: MediaQuery.of(context).size.width / 4,
  128.                     ),
  129.                     Chip(
  130.                       label: Text(
  131.                         "SIGN IN",
  132.                         style: TextStyle(color: Colors.white, fontSize: 18.0),
  133.                       ),
  134.                       backgroundColor: Color(0xFFD1A155),
  135.                     ),
  136.                     SizedBox(
  137.                       width: MediaQuery.of(context).size.width / 35,
  138.                       child: Container(
  139.                         width: MediaQuery.of(context).size.height / 12,
  140.                         height: 2.0,
  141.                         color: Colors.white,
  142.                       ),
  143.                     ),
  144.                     Chip(
  145.                       label: Text(
  146.                         "SIGN UP",
  147.                         style: TextStyle(color: Colors.white, fontSize: 18.0),
  148.                       ),
  149.                       backgroundColor: Colors.black87,
  150.                     ),
  151.                   ],
  152.                 )
  153.               ],
  154.             ),
  155.             SizedBox(
  156.               height: MediaQuery.of(context).size.height / 35,
  157.             ),
  158.             Align(
  159.                 alignment: Alignment.center,
  160.                 child: Text(
  161.                   "Welcome back!",
  162.                   style: TextStyle(
  163.                     fontSize: 20.0,
  164.                     color: Color(0xFFD1A155),
  165.                   ),
  166.                 )),
  167.             SizedBox(
  168.               height: MediaQuery.of(context).size.height / 30,
  169.             ),
  170.             SizedBox(
  171.               height: MediaQuery.of(context).size.height / 15,
  172.             ),
  173.             Stack(
  174.               alignment: Alignment.center,
  175.               children: <Widget>[
  176.                 SizedBox(
  177.                   height: 2.0,
  178.                   child: new Center(
  179.                     child: new Container(
  180.                       height: 10.0,
  181.                       color: Colors.black12,
  182.                     ),
  183.                   ),
  184.                 ),
  185.                 Container(
  186.                   height: MediaQuery.of(context).size.height / 18,
  187.                   width: MediaQuery.of(context).size.height / 11,
  188.                   decoration: BoxDecoration(
  189.                       borderRadius: BorderRadius.circular(23.0),
  190.                       color: Colors.white,
  191.                       border: Border.all(color: Colors.black12)),
  192.                   child: Center(
  193.                       child: Text(
  194.                     "OR",
  195.                     style: TextStyle(fontSize: 18.0),
  196.                   )),
  197.                 ),
  198.               ],
  199.             ),
  200.             SizedBox(
  201.               height: MediaQuery.of(context).size.height / 30,
  202.             ),
  203.             Padding(
  204.               padding: const EdgeInsets.only(left: 15.0, right: 15.0),
  205.               child: Row(
  206.                 children: <Widget>[
  207.                   Container(
  208.                     height: MediaQuery.of(context).size.height / 13,
  209.                     width: MediaQuery.of(context).size.width / 2.2,
  210.                     decoration: BoxDecoration(
  211.                         borderRadius: BorderRadius.circular(5.0),
  212.                         color: Colors.white,
  213.                         border: Border.all(color: Colors.black12)),
  214.                     child: Row(
  215.                       children: <Widget>[
  216.                         SizedBox(
  217.                           width: 18.0,
  218.                         ),
  219.                         Icon(Icons.tag_faces),
  220.                         SizedBox(
  221.                           width: 10.0,
  222.                         ),
  223.                         Text(
  224.                           "Facebook",
  225.                           style: TextStyle(fontSize: 22.0, color: Colors.blue),
  226.                         ),
  227.                       ],
  228.                     ),
  229.                   ),
  230.                   SizedBox(
  231.                     width: MediaQuery.of(context).size.width / 40,
  232.                   ),
  233.                   Container(
  234.                     height: MediaQuery.of(context).size.height / 13,
  235.                     width: MediaQuery.of(context).size.width / 2.3,
  236.                     decoration: BoxDecoration(
  237.                         borderRadius: BorderRadius.circular(5.0),
  238.                         color: Colors.white,
  239.                         border: Border.all(color: Colors.black12)),
  240.                     child: Row(
  241.                       children: <Widget>[
  242.                         SizedBox(
  243.                           width: 18.0,
  244.                         ),
  245.                         Icon(Icons.tag_faces),
  246.                         SizedBox(
  247.                           width: 10.0,
  248.                         ),
  249.                         Text(
  250.                           "Google+",
  251.                           style: TextStyle(fontSize: 22.0, color: Colors.red),
  252.                         ),
  253.                       ],
  254.                     ),
  255.                   ),
  256.                 ],
  257.               ),
  258.             ),
  259.             SizedBox(
  260.               height: MediaQuery.of(context).size.height / 20,
  261.             ),
  262.             Align(
  263.               alignment: Alignment.center,
  264.               child: InkWell(
  265.                 onTap: () => Navigator.push(
  266.                     context, MaterialPageRoute(builder: (context) => SignUp())),
  267.                 child: RichText(
  268.                     text: TextSpan(
  269.                         text: "Don't have an account?",
  270.                         style: TextStyle(fontSize: 20.0, color: Colors.black87),
  271.                         children: <TextSpan>[
  272.                       TextSpan(
  273.                           text: ' Sign up',
  274.                           style: TextStyle(
  275.                               color: Color(0xFFD1A155),
  276.                               fontWeight: FontWeight.bold)),
  277.                     ])),
  278.               ),
  279.             ),
  280.             SizedBox(
  281.               height: MediaQuery.of(context).size.height / 30,
  282.             ),
  283.           ],
  284.         ),
  285.       ),
  286.     );
  287.   }
  288.  
  289.   Widget buildForm() {
  290.     return FutureBuilder<Post>(builder: (context, snapshot) {
  291.       if(snapshot.hasData){
  292.         Post articles = snapshot.data;
  293.         return Form(
  294.           key: _formKey,
  295.           autovalidate: _autoValidate,
  296.           child: Column(
  297.             children: <Widget>[
  298.               Theme(
  299.                 data: ThemeData(
  300.                   hintColor: Colors.black26,
  301.                   primaryColor: Color(0xFFD1A155),
  302.                 ),
  303.                 child: Padding(
  304.                   padding: const EdgeInsets.only(left: 15.0, right: 15.0),
  305.                   child: TextFormField(
  306.                     keyboardType: TextInputType.emailAddress,
  307.                     validator: validateEmail,
  308.                     onSaved: (String val) {
  309.                       articles.userName = val;
  310.                     },
  311.                     decoration: InputDecoration(
  312.                         border:
  313.                         OutlineInputBorder(borderSide: BorderSide()),
  314.                         prefixIcon: Icon(
  315.                           Icons.email,
  316.                           color: Color(0xFFD1A155),
  317.                         ),
  318.                         hintText: 'Email Address',
  319.                         hintStyle: TextStyle(
  320.                             color: Colors.black,
  321.                             fontWeight: FontWeight.w400)),
  322.                   ),
  323.                 ),
  324.               ),
  325.               SizedBox(
  326.                 height: MediaQuery.of(context).size.height / 45,
  327.               ),
  328.               Theme(
  329.                 data: ThemeData(
  330.                     primaryColor: Color(0xFFD1A155),
  331.                     hintColor: Colors.black26),
  332.                 child: Padding(
  333.                   padding: const EdgeInsets.only(left: 15.0, right: 15.0),
  334.                   child: TextFormField(
  335.                     keyboardType: TextInputType.text,
  336.                     obscureText: true,
  337.                     validator: validatePassword,
  338.                     onSaved: (String val) {
  339.                       articles.password = val;
  340.                     },
  341.                     decoration: InputDecoration(
  342.                         border:
  343.                         OutlineInputBorder(borderSide: BorderSide()),
  344.                         prefixIcon: Icon(
  345.                           Icons.lock,
  346.                           color: Color(0xFFD1A155),
  347.                         ),
  348.                         hintText: 'Password',
  349.                         hintStyle: TextStyle(
  350.                             color: Colors.black,
  351.                             fontWeight: FontWeight.w400)),
  352.                   ),
  353.                 ),
  354.               ),
  355.               Padding(
  356.                 padding: const EdgeInsets.only(right: 15.0, left: 10.0),
  357.                 child: Row(
  358.                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  359.                   children: <Widget>[
  360.                     Row(
  361.                       children: <Widget>[
  362.                         ActionChip(
  363.                           onPressed: () {},
  364.                           avatar: Checkbox(
  365.                             value: false,
  366.                             onChanged: (bool z) {
  367.                               print(z);
  368.                             },
  369.                             activeColor: Color(0xFFD1A155),
  370.                           ),
  371.                           label: Text("Remember Me"),
  372.                           backgroundColor: Colors.transparent,
  373.                         ),
  374.                       ],
  375.                     ),
  376.                     Text(
  377.                       "Forgot Password?",
  378.                       style: TextStyle(
  379.                         color: Color(0xFFD1A155),
  380.                       ),
  381.                     ),
  382.                   ],
  383.                 ),
  384.               ),
  385.               Padding(
  386.                 padding: const EdgeInsets.only(left: 15.0, right: 15.0),
  387.                 child: InkWell(
  388.                   onTap: _validateInputs,
  389.                   child: Container(
  390.                     height: MediaQuery.of(context).size.height / 13,
  391.                     //width: MediaQuery.of(context).size.height / 1.8,
  392.                     decoration: BoxDecoration(
  393.                       color: Color(0xFFD1A155),
  394.                       borderRadius: BorderRadius.circular(5.0),
  395.                     ),
  396.                     child: Center(
  397.                       child: Text(
  398.                         "LOGIN",
  399.                         style:
  400.                         TextStyle(color: Colors.white, fontSize: 18.0),
  401.                       ),
  402.                     ),
  403.                   ),
  404.                 ),
  405.               )
  406.             ],
  407.           ),
  408.         );
  409.       }
  410.     });
  411.   }
  412. }
Add Comment
Please, Sign In to add comment