Advertisement
Guest User

Untitled

a guest
May 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 13.20 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. import '../emails.dart';
  4. import '../../models/user.dart';
  5.  
  6. import 'dart:io';
  7. import 'package:http/io_client.dart';
  8. import 'dart:convert' as JSON;
  9. // import 'package:http/http.dart' as http;
  10. // import 'dart:async';
  11. // import 'package:async/async.dart';
  12. // import 'package:dio/dio.dart';
  13.  
  14. class LoginPage extends StatefulWidget {
  15.  
  16.   State<StatefulWidget> createState() => _LoginPageStates();
  17. }
  18.  
  19. class _LoginPageStates extends State<LoginPage> {
  20.  
  21.   TextEditingController _userControllerLogin = TextEditingController();
  22.   TextEditingController _passwordControllerLogin = TextEditingController();
  23.  
  24.   TextEditingController _userdControllerSign = TextEditingController();
  25.   TextEditingController _passwordControllerSign = TextEditingController();
  26.   TextEditingController _nameControllerSign = TextEditingController();
  27.  
  28.   static String tag = 'login-page';
  29.   String valor = "";
  30.  
  31.   final FocusNode _login = FocusNode();
  32.   final FocusNode _sign = FocusNode();
  33.   final FocusNode _loginP = FocusNode();
  34.   final FocusNode _signP = FocusNode();
  35.   final FocusNode _signName = FocusNode();
  36.  
  37.  
  38.   @override
  39.   Widget build(BuildContext context) {
  40.     return
  41.  
  42.         Scaffold(
  43.           backgroundColor: Colors.black,
  44.           body: getContainer(),
  45.  
  46.       );
  47.   }
  48.  
  49.   Widget getContainer(){
  50.     return(
  51.       ListView(
  52.         children: <Widget>[
  53.           Container(
  54.             color: Colors.black,
  55.             height: 100,
  56.             width:  100,
  57.           ),
  58.           DefaultTabController(
  59.             length: 2,
  60.             initialIndex: 0,
  61.  
  62.             child:
  63.             Column(
  64.               children: <Widget>[
  65.                 TabBar(
  66.                   isScrollable: true,
  67.                   labelColor: Colors.white,
  68.                   tabs: <Widget>[
  69.                     Tab(
  70.                       text:'Login' ,
  71.                     ),
  72.                     Tab(
  73.                       text: 'Sign-Up',)
  74.                   ],
  75.                 ),
  76.                 Container(
  77.                   height: 400,
  78.                   width: 300,
  79.                   child: TabBarView(
  80.                     children: <Widget>[
  81.                       getContentLogin(),
  82.                       getContentSignin()
  83.                     ],
  84.                   ),
  85.                 )
  86.               ],
  87.             ),
  88.           )
  89.  
  90.  
  91.         ],
  92.       )
  93.     );
  94.   }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.   Widget getContentLogin() {
  101.     return (
  102.  
  103.         Center(
  104.  
  105.             child: Column(
  106.                 mainAxisAlignment: MainAxisAlignment.center,
  107.                 children: <Widget>[
  108.  
  109.  
  110.                   Padding(padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
  111.                     child: TextFormField(
  112.                       controller: _userControllerLogin,
  113.                       textInputAction: TextInputAction.next,
  114.                       focusNode: _login,
  115.                       onFieldSubmitted: (term){
  116.                         FocusScope.of(context).requestFocus(_loginP);
  117.                       },
  118.                       textAlign: TextAlign.center,
  119.                       style: TextStyle(color: Colors.black),
  120.                       maxLines: 1,
  121.                       decoration: InputDecoration(
  122.                           fillColor: Colors.white,
  123.                           filled: true,
  124.                           hintText: 'Nome de usuário',
  125.                           //hintStyle: TextStyle(color: Colors.black),
  126.                           border: OutlineInputBorder(
  127.                             borderRadius: BorderRadius.circular(50.0),
  128.                           )
  129.  
  130.                       ),
  131.  
  132.                     ),
  133.                   ),
  134.  
  135.                   TextFormField(
  136.                       textInputAction: TextInputAction.done,
  137.                     controller: _passwordControllerLogin,
  138.                     style: TextStyle(color: Colors.black),
  139.                       textAlign: TextAlign.center,
  140.                       focusNode: _loginP,
  141.                       maxLines: 1,
  142.                       obscureText: true,
  143.                       decoration: InputDecoration(
  144.                           filled: true,
  145.                           fillColor: Colors.white,
  146.                           hintText: 'Senha',
  147.                           //hintStyle: TextStyle(color: Colors.black),
  148.                           border: OutlineInputBorder(
  149.                             borderRadius: BorderRadius.circular(50.0),
  150.                           )
  151.                       ),
  152.                     ),
  153.  
  154.  
  155.  
  156.                   Padding(padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
  157.                       child: ButtonTheme(
  158.                           height: 50,
  159.                           minWidth: 200,
  160.                           child: RaisedButton(
  161.                             color: Colors.deepOrangeAccent,
  162.                             textColor: Colors.white,
  163.                             child: Text('Login'),
  164.                             shape: RoundedRectangleBorder(
  165.                                 borderRadius: BorderRadius.circular(25.0)),
  166.                             onPressed: () {
  167.                               submitLogin();
  168.                               //submitLogin();
  169.                               //Navigator.pushReplacement(context,
  170.                                  // MaterialPageRoute(
  171.                                     //  builder: (BuildContext context) =>
  172.                                      //     Emails()));
  173.                             },
  174.  
  175.                           )
  176.                       )
  177.                   )
  178.                 ]
  179.             )
  180.  
  181.         )
  182.     );
  183.   }
  184.  
  185.   Widget getContentSignin() {
  186.  
  187.     return (
  188.         Center(
  189.             child: Column(
  190.              
  191.                 mainAxisAlignment: MainAxisAlignment.center,
  192.                 children: <Widget>[
  193.  
  194.                   Padding(padding: EdgeInsets.fromLTRB(0, 35, 0, 10),
  195.                     child: TextFormField(
  196.                     controller: _nameControllerSign,
  197.                       textInputAction: TextInputAction.done,
  198.                       style: TextStyle(color: Colors.black),
  199.                       textAlign: TextAlign.center,
  200.                       maxLines: 1,
  201.                       focusNode: _signName,
  202.                       decoration: InputDecoration(
  203.                         fillColor: Colors.white,
  204.                           filled: true,
  205.                           hintText: "Nome",
  206.                           //hintStyle: TextStyle(color: Colors.black),
  207.                           border: OutlineInputBorder(
  208.                             borderRadius: BorderRadius.circular(50.0),
  209.                           )
  210.                       ),
  211.                     ),
  212.                     ),
  213.                    
  214.                     TextFormField(
  215.                       controller: _userdControllerSign,
  216.                       textInputAction: TextInputAction.next,
  217.                       focusNode: _sign,
  218.                       onFieldSubmitted: (term){
  219.                         FocusScope.of(context).requestFocus(_signP);
  220.                       },
  221.                       style: TextStyle(color: Colors.black),
  222.                       textAlign: TextAlign.center,
  223.                       maxLines: 1,
  224.                       decoration: InputDecoration(
  225.                         fillColor: Colors.white,
  226.                           filled: true,
  227.                           hintText: "Nome de usuário",
  228.                           //hintStyle: TextStyle(color: Colors.black),
  229.                           border: OutlineInputBorder(
  230.                             borderRadius: BorderRadius.circular(50.0),
  231.                           )
  232.                       ),
  233.                     ),
  234.                  
  235.                   Padding(
  236.                     padding: EdgeInsets.only(top: 10.0),
  237.                     child: TextFormField(
  238.                     controller: _passwordControllerSign,
  239.                       textInputAction: TextInputAction.done,
  240.                       style: TextStyle(color: Colors.black),
  241.                       textAlign: TextAlign.center,
  242.                       maxLines: 1,
  243.                       focusNode: _signP,
  244.                       obscureText: true,
  245.                       decoration: InputDecoration(
  246.                         fillColor: Colors.white,
  247.                           filled: true,
  248.                           hintText: "Senha",
  249.                           //hintStyle: TextStyle(color: Colors.black),
  250.                           border: OutlineInputBorder(
  251.                             borderRadius: BorderRadius.circular(50.0),
  252.                           )
  253.                       ),
  254.                     ),
  255.                    
  256.                   ),
  257.  
  258.  
  259.                   Padding(padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
  260.                       child: ButtonTheme(
  261.                           height: 50,
  262.                           minWidth: 200,
  263.                           child: RaisedButton(
  264.                             color: Colors.deepOrangeAccent,
  265.                             textColor: Colors.white,
  266.                             child: Text('Criar conta'),
  267.                             shape: RoundedRectangleBorder(
  268.                                 borderRadius: BorderRadius.circular(25.0)),
  269.                             onPressed: () {
  270.                               submitSign();
  271.                             },
  272.                           )
  273.                       )
  274.                   ),
  275.                
  276.               ]
  277.             )
  278.         )
  279.     );
  280.   }
  281.  
  282.   void submitLogin() async {
  283.    
  284.     if (_userControllerLogin.text.isNotEmpty &&
  285.         _passwordControllerLogin.text.isNotEmpty) {
  286.  
  287.       HttpClient httpClient = new HttpClient()
  288.         ..badCertificateCallback =
  289.         ((X509Certificate cert, String host, int port) {
  290.           print("CERTIFICADO HTTP");
  291.           // tests that cert is self signed, correct subject and correct date(s)
  292.           return true;
  293.         });
  294.  
  295.       IOClient ioClient = new IOClient(httpClient);
  296.       var urlLogin = 'https://dry-peak-13680.herokuapp.com/user/login';
  297.  
  298.       var login = Map<String, dynamic>();
  299.  
  300.       login['nickname'] = _userControllerLogin.text;
  301.       login['password'] = _passwordControllerLogin.text;
  302.  
  303.       ioClient
  304.           .put(urlLogin,
  305.           headers: {"Accept": "application/json"},
  306.           body: login)
  307.           .then((response) {
  308.         print("Login:");
  309.         print('Response: ${response.statusCode}  Body:${response.body}');
  310.  
  311.         print('$response');
  312.         //User newUser = User(result['name'], result['nickname'], []);
  313.  
  314.         if(response.body.isNotEmpty ) {
  315.           debugPrint("usuario logado");
  316.           //debugPrint(newUser.nickname);
  317.           // Navigator.push(
  318.           //   context,
  319.           //   MaterialPageRoute(
  320.           //     builder: (BuildContext context) =>
  321.           //       //Emails(result['name'], result['nickname'])
  322.           //       ));
  323.             }
  324.           }
  325.         )
  326.           .catchError((err) {
  327.             print(err.toString());
  328.             print('deu ruim');
  329.       });
  330.  
  331.       ioClient.close();
  332.     }
  333.   }
  334.  
  335.    void submitSign() async {
  336.     if (_userdControllerSign.text.isNotEmpty &&
  337.         _passwordControllerSign.text.isNotEmpty) {
  338.  
  339.  
  340.       HttpClient httpClient = new HttpClient()
  341.         ..badCertificateCallback =
  342.         ((X509Certificate cert, String host, int port) {
  343.           // tests that cert is self signed, correct subject and correct date(s)
  344.           return true;
  345.         });
  346.  
  347.       IOClient ioClient = new IOClient(httpClient);
  348.       var urlSignUp = 'https://dry-peak-13680.herokuapp.com/user/signup';
  349.  
  350.       var signUp = Map<String, dynamic>();
  351.    
  352.       signUp['name'] = _nameControllerSign.text;
  353.       signUp['nickname'] = _userdControllerSign.text;
  354.       signUp['password'] = _passwordControllerSign.text;
  355.  
  356.       print("clicked");
  357.       ioClient
  358.           .post(
  359.             urlSignUp,
  360.             headers: {"Accept": "application/json"},
  361.             body: signUp)
  362.           .then((response) {
  363.             print("Signup:");
  364.             print('Response: ${response.statusCode}  Body:${response.body} ');
  365.  
  366.             // if(result['loggedIn'] && false) {
  367.             //   User newUser = (result["name"], result['nickname'], []);
  368.             //   Navigator.pushReplacement(context,
  369.             //     MaterialPageRoute(builder: (BuildContext context) => Emails(newUser)));
  370.             // }
  371.  
  372.  
  373.         })
  374.           .catchError((err) {
  375.             print(err.toString());
  376.             print('Não funcionou');
  377.             //ioClient.close();
  378.  
  379.       });
  380.  
  381.       ioClient.close();
  382.  
  383.       // User newUser = User( "A", _userdControllerSign.text, _passwordControllerSign.text,
  384.       //     DateTime.now().toString(), []);
  385.       // Navigator.pushReplacement(
  386.       //     context,
  387.       //     MaterialPageRoute(
  388.       //         builder: (BuildContext context) => Emails(newUser)));
  389.     }
  390.   }
  391.  
  392.   submitLoginSemServer() {
  393.     if( _userControllerLogin.text.isNotEmpty ) {
  394.       debugPrint("usuario logado");
  395.       //debugPrint(newUser.nickname);
  396.       Navigator.push(
  397.         context,
  398.         MaterialPageRoute(
  399.           builder: (BuildContext context) =>
  400.             Emails(User(_userControllerLogin.text, _userControllerLogin.text, []))));
  401.     }
  402.   }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement