Advertisement
Guest User

Untitled

a guest
May 7th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 8.03 KB | None | 0 0
  1. import 'package:firebase_auth/firebase_auth.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:page_transition/page_transition.dart';
  6. import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
  7.  
  8.  
  9. import './login.dart';
  10.  
  11. final FirebaseAuth _auth = FirebaseAuth.instance;
  12.  
  13.  
  14. class ForgotPass extends StatefulWidget {
  15.   @override
  16.   ForgotPassState createState( ) =>
  17.       ForgotPassState (
  18.       );
  19. }
  20.  
  21. class ForgotPassState extends State<ForgotPass> with WidgetsBindingObserver {
  22.   final GlobalKey<FormState> _formKey = GlobalKey<FormState> (
  23.   );
  24.   final TextEditingController _emailController = TextEditingController (
  25.   );
  26.   final TextEditingController _passwordController = TextEditingController (
  27.   );
  28.  
  29.   bool _success;
  30.   String _userEmail;
  31.   String val;
  32.   String _userID;
  33.  
  34.   @override
  35.   void initState( ) {
  36.     super.initState (
  37.     );
  38.     WidgetsBinding.instance.addObserver (
  39.         this
  40.         );
  41.   }
  42.  
  43.  
  44.   @override
  45.   Widget build( BuildContext context ) {
  46.     SystemChrome.setSystemUIOverlayStyle (
  47.         SystemUiOverlayStyle.dark.copyWith (
  48.           statusBarColor: Colors.yellow.shade800 ,
  49.           )
  50.         );
  51.  
  52.     return Stack (
  53.         children: <Widget>[
  54.     Container (
  55.     color: Color (
  56.         0xFF18181C
  57.         )) ,
  58.  
  59.     Container (
  60.     alignment: Alignment.topCenter ,
  61.     color: Color (
  62.     0xFF232428
  63.     ) ,
  64.     margin:
  65.     const EdgeInsets.only(
  66.     left: 0.0 , right: 0.0 , top: 0.0 , bottom: 0.0
  67.     ) ,
  68.     child: new Image.asset(
  69.     'images/login2.png' ,
  70.     width: 300.0 , height: 248.0 , alignment: Alignment.center
  71.     ) ,
  72.     ) ,
  73.     Container (
  74.     alignment: Alignment.topCenter ,
  75.     color: Color (
  76.     0xFF232428
  77.     ) ,
  78.     margin: const EdgeInsets.only(
  79.     left: 0.0 , right: 0.0 , top: 260.0 , bottom: 0.0
  80.     ) ,
  81.     child: new Image.asset(
  82.     'images/login_title_forgot.png' ,
  83.     width: 49.0 , height: 14.0 , alignment: Alignment.center
  84.     ) ,
  85.     ) ,
  86.  
  87.     Container (
  88.  
  89.     alignment: Alignment.topCenter ,
  90.  
  91.     child: Form (
  92.     key: _formKey ,
  93.     child: GestureDetector (
  94.     behavior: HitTestBehavior.opaque ,
  95.     onTap: ( ) {
  96.     SystemChannels.textInput.invokeMethod (
  97.     'TextInput.hide'
  98.     );
  99. //            FocusScope.of(context).requestFocus(new FocusNode());
  100.     } ,
  101.     child: Card (
  102.     elevation: 0 ,
  103.     margin: const EdgeInsets.only(
  104.     top: 280.0 ,
  105.     bottom: 0.0 ,
  106.     right: 0.0 ,
  107.     left: 0.0
  108.     ) ,
  109.  
  110.     child: Container (
  111.     padding: const EdgeInsets.all(
  112.     20.0
  113.     ) ,
  114.     height: 243.0 ,
  115.     width: 350.0 ,
  116.     color: Color (
  117.     0xFF232428
  118.     ) ,
  119.     child: new Column(
  120.     children: <Widget>[
  121.     new TextFormField(
  122.     controller: _emailController ,
  123.     cursorColor: Color (
  124.     0xFFEDAC0B
  125.     ) ,
  126.     validator: ( String value ) {
  127.     if (value.isEmpty) {
  128.     return 'Please Enter A Valid Email';
  129.     }
  130.     if (RegExp (
  131.     r'(\w+@\w+\.\w+)'
  132.     ).stringMatch (
  133.     val
  134.     ) != val)
  135.     return 'Please Enter A Valid E-Mail';
  136.     } ,
  137. //                    onSaved: (input) => _email = input,
  138.  
  139.     style: TextStyle (
  140.     color: Color (
  141.     0xFF606060
  142.     ) ,
  143.     fontWeight: FontWeight.w300
  144.     ) ,
  145.  
  146.     keyboardType: TextInputType
  147.         .emailAddress ,
  148.     decoration: new InputDecoration(
  149.     enabledBorder: OutlineInputBorder (
  150.     borderRadius: BorderRadius
  151.         .circular (
  152.     10.0
  153.     ) ,
  154.     borderSide: const BorderSide(
  155.     color: Color (
  156.     0xFFEDAC0B
  157.     ) , width: 0.0
  158.     ) ,
  159.     ) ,
  160.     focusedBorder: OutlineInputBorder (
  161.     borderRadius: BorderRadius
  162.         .circular (
  163.     10.0
  164.     ) ,
  165.     borderSide:
  166.     const BorderSide(
  167.     color: Color (
  168.     0xFFEDAC0B
  169.     )
  170.     ) ,
  171.     ) ,
  172.     labelText: 'Email' ,
  173.     labelStyle: TextStyle (
  174.     color: Color (
  175.     0xFF606060
  176.     ) ,
  177.     fontWeight: FontWeight.w300
  178.     ) ,
  179.     hintStyle: TextStyle (
  180.     color: Color (
  181.     0xFF606060
  182.     ) ,
  183.     fontWeight: FontWeight.w300
  184.     ) ,
  185.     prefixIcon:
  186.     new Icon(
  187.     Icons.email , color: Color (
  188.     0xFFEDAC0B
  189.     )
  190.     ) ,
  191.     ) ,
  192.     ) ,
  193.     new Container(
  194.     height: 8.0 ,
  195.     ) ,
  196.     new TextFormField(
  197.     cursorColor: Color (
  198.     0xFFEDAC0B
  199.     ) ,
  200.     controller: _passwordController ,
  201.     validator: ( String value ) {
  202.     if (value.isEmpty) {
  203.     return 'Please Enter A Valid Password';
  204.     }
  205.     } ,
  206. //                    onSaved: (input) => _password = input,
  207.     style: TextStyle (
  208.     color: Color (
  209.     0xFF606060
  210.     ) ,
  211.     fontWeight: FontWeight.w300
  212.     ) ,
  213.  
  214.     keyboardType: TextInputType.text ,
  215.     decoration: new InputDecoration(
  216.     enabledBorder: OutlineInputBorder (
  217.     borderRadius: BorderRadius
  218.         .circular (
  219.     10.0
  220.     ) ,
  221.     borderSide: const BorderSide(
  222.     color: Color (
  223.     0xFFEDAC0B
  224.     ) , width: 0.0
  225.     ) ,
  226.     ) ,
  227.     focusedBorder: OutlineInputBorder (
  228.     borderRadius: BorderRadius
  229.         .circular (
  230.     10.0
  231.     ) ,
  232.     borderSide:
  233.     const BorderSide(
  234.     color: Color (
  235.     0xFFEDAC0B
  236.     )
  237.     ) ,
  238.     ) ,
  239.     labelText: 'Password' ,
  240.     labelStyle: TextStyle (
  241.     color: Color (
  242.     0xFF606060
  243.     ) ,
  244.     fontWeight: FontWeight.w300
  245.     ) ,
  246.     hintStyle: TextStyle (
  247.     color: Color (
  248.     0xFF606060
  249.     ) ,
  250.     fontWeight: FontWeight.w300
  251.     ) ,
  252.     prefixIcon:
  253.     new Icon(
  254.     Icons.lock , color: Color (
  255.     0xFFEDAC0B
  256.     )
  257.     ) ,
  258.     suffixIcon: new Icon(
  259.     Icons.visibility_off ,
  260.     color: Color (
  261.     0xFF606060
  262.     )
  263.     ) ,
  264.     ) ,
  265.     obscureText: true ,
  266.     ) ,
  267.  
  268.     ]
  269.     )
  270.     )
  271.  
  272.     )
  273.  
  274.     ))
  275.  
  276.     ),
  277.  
  278.     new Container(
  279.     alignment: Alignment.bottomRight ,
  280.     margin: const EdgeInsets.only(
  281.     left: 0.0 , right: 30.0 , top: 0.0 , bottom: 48.0
  282.     ) ,
  283.     child: GestureDetector (
  284.     onTap: () {
  285.     if (_formKey.currentState.validate()) {
  286.    _signInWithEmailAndLink();
  287.    }
  288.  
  289.     print("Back To Login Button Clicked");
  290.     Navigator.pop(
  291.     context,
  292.     new MaterialPageRoute(
  293.     maintainState: true,
  294.     builder: (context) =>
  295.     new Login()));
  296.     },
  297.  
  298. //
  299.  
  300.     child: Image.asset (
  301.     'images/login_button_redarrow.png' ,
  302.     width: 40.0 ,
  303.     height: 40.0 ,
  304.     ) ,
  305.     )
  306.     ) ,
  307.  
  308.  
  309.     new Container(
  310.     alignment: Alignment.bottomCenter ,
  311.     margin: const EdgeInsets.only(
  312.     left: 0.0 , right: 00.0 , top: 0.0 , bottom: 10.0
  313.     ) ,
  314.  
  315.     child: new Image.asset(
  316.     'images/footer2.png' ,
  317.     width: 123.0 ,
  318.     height: 14.0 ,
  319.     )
  320.     ) ,
  321.  
  322.  
  323.     ]
  324.     );
  325.  
  326.   }
  327.  
  328.   @override
  329.   void dispose( ) {
  330.     _emailController.dispose (
  331.     );
  332.     WidgetsBinding.instance.removeObserver (
  333.         this
  334.         );
  335.     super.dispose (
  336.     );
  337.   }
  338.  
  339.   @override
  340.   void didChangeAppLifecycleState( AppLifecycleState state ) async {
  341.     if (state == AppLifecycleState.resumed) {
  342.       final Uri link = await _retrieveDynamicLink (
  343.       );
  344.  
  345.       if (link != null) {
  346.         final FirebaseUser user = await _auth.signInWithEmailAndLink (
  347.           email: _userEmail ,
  348.           link: link.toString (
  349.           ) ,
  350.           );
  351.  
  352.         if (user != null) {
  353.           _userID = user.uid;
  354.           _success = true;
  355.         } else {
  356.           _success = false;
  357.         }
  358.       } else {
  359.         _success = false;
  360.  
  361.         setState (
  362.                 ( ) {}
  363.                 );
  364.       }
  365.     }
  366.  
  367.     Future<Uri> _retrieveDynamicLink( ) async {
  368.       final PendingDynamicLinkData data =
  369.       await FirebaseDynamicLinks.instance.retrieveDynamicLink (
  370.       );
  371.       return data?.link;
  372.     }
  373.  
  374.  
  375.     Future<void> _signInWithEmailAndLink( ) async {
  376.       _userEmail = _emailController.text;
  377.  
  378.       return await _auth.sendSignInWithEmailLink (
  379.         email: _userEmail ,
  380.         url: 'localhost' ,
  381.         handleCodeInApp: true ,
  382.         iOSBundleID: 'io.flutter.plugins.firebaseAuthExample' ,
  383.         androidPackageName: 'io.svgo.svgo' ,
  384.         androidInstallIfNotAvailable: true ,
  385.         androidMinimumVersion: "1" ,
  386.         );
  387.     }
  388.   }
  389.  
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement