Advertisement
Guest User

Untitled

a guest
May 7th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.72 KB | None | 0 0
  1. import 'package:firebase_auth/firebase_auth.dart';
  2. import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6.  
  7. import './login.dart';
  8.  
  9. final FirebaseAuth _auth = FirebaseAuth.instance;
  10.  
  11. class ForgotPass extends StatefulWidget {
  12.   @override
  13.   ForgotPassState createState() => ForgotPassState();
  14. }
  15.  
  16. class ForgotPassState extends State<ForgotPass> with WidgetsBindingObserver {
  17.   final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  18.   final TextEditingController _emailController = TextEditingController();
  19.   final TextEditingController _passwordController = TextEditingController();
  20.  
  21.   bool _success;
  22.   String _userEmail;
  23.   String val;
  24.   String _userID;
  25.  
  26.   @override
  27.   void initState() {
  28.     super.initState();
  29.     WidgetsBinding.instance.addObserver(this);
  30.   }
  31.  
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
  35.       statusBarColor: Colors.yellow.shade800,
  36.     ));
  37.  
  38.     return Stack(children: <Widget>[
  39.       Container(color: Color(0xFF18181C)),
  40.       Container(
  41.         alignment: Alignment.topCenter,
  42.         color: Color(0xFF232428),
  43.         margin:
  44.             const EdgeInsets.only(left: 0.0, right: 0.0, top: 0.0, bottom: 0.0),
  45.         child: new Image.asset('images/login2.png',
  46.             width: 300.0, height: 248.0, alignment: Alignment.center),
  47.       ),
  48.       Container(
  49.         alignment: Alignment.topCenter,
  50.         color: Color(0xFF232428),
  51.         margin: const EdgeInsets.only(
  52.             left: 0.0, right: 0.0, top: 260.0, bottom: 0.0),
  53.         child: new Image.asset('images/login_title_forgot.png',
  54.             width: 49.0, height: 14.0, alignment: Alignment.center),
  55.       ),
  56.       Container(
  57.           alignment: Alignment.topCenter,
  58.           child: Form(
  59.               key: _formKey,
  60.               child: GestureDetector(
  61.                   behavior: HitTestBehavior.opaque,
  62.                   onTap: () {
  63.                     SystemChannels.textInput.invokeMethod('TextInput.hide');
  64. //            FocusScope.of(context).requestFocus(new FocusNode());
  65.                   },
  66.                   child: Card(
  67.                       elevation: 0,
  68.                       margin: const EdgeInsets.only(
  69.                           top: 280.0, bottom: 0.0, right: 0.0, left: 0.0),
  70.                       child: Container(
  71.                           padding: const EdgeInsets.all(20.0),
  72.                           height: 243.0,
  73.                           width: 350.0,
  74.                           color: Color(0xFF232428),
  75.                           child: new Column(children: <Widget>[
  76.                             new TextFormField(
  77.                               controller: _emailController,
  78.                               cursorColor: Color(0xFFEDAC0B),
  79.                               validator: (String value) {
  80.                                 if (value.isEmpty) {
  81.                                   return 'Please Enter A Valid Email';
  82.                                 }
  83.                                 if (RegExp(r'(\w+@\w+\.\w+)')
  84.                                         .stringMatch(val) !=
  85.                                     val) return 'Please Enter A Valid E-Mail';
  86.                               },
  87. //                    onSaved: (input) => _email = input,
  88.  
  89.                               style: TextStyle(
  90.                                   color: Color(0xFF606060),
  91.                                   fontWeight: FontWeight.w300),
  92.  
  93.                               keyboardType: TextInputType.emailAddress,
  94.                               decoration: new InputDecoration(
  95.                                 enabledBorder: OutlineInputBorder(
  96.                                   borderRadius: BorderRadius.circular(10.0),
  97.                                   borderSide: const BorderSide(
  98.                                       color: Color(0xFFEDAC0B), width: 0.0),
  99.                                 ),
  100.                                 focusedBorder: OutlineInputBorder(
  101.                                   borderRadius: BorderRadius.circular(10.0),
  102.                                   borderSide: const BorderSide(
  103.                                       color: Color(0xFFEDAC0B)),
  104.                                 ),
  105.                                 labelText: 'Email',
  106.                                 labelStyle: TextStyle(
  107.                                     color: Color(0xFF606060),
  108.                                     fontWeight: FontWeight.w300),
  109.                                 hintStyle: TextStyle(
  110.                                     color: Color(0xFF606060),
  111.                                     fontWeight: FontWeight.w300),
  112.                                 prefixIcon: new Icon(Icons.email,
  113.                                     color: Color(0xFFEDAC0B)),
  114.                               ),
  115.                             ),
  116.                             new Container(
  117.                               height: 8.0,
  118.                             ),
  119.                             new TextFormField(
  120.                               cursorColor: Color(0xFFEDAC0B),
  121.                               controller: _passwordController,
  122.                               validator: (String value) {
  123.                                 if (value.isEmpty) {
  124.                                   return 'Please Enter A Valid Password';
  125.                                 }
  126.                               },
  127. //                    onSaved: (input) => _password = input,
  128.                               style: TextStyle(
  129.                                   color: Color(0xFF606060),
  130.                                   fontWeight: FontWeight.w300),
  131.  
  132.                               keyboardType: TextInputType.text,
  133.                               decoration: new InputDecoration(
  134.                                 enabledBorder: OutlineInputBorder(
  135.                                   borderRadius: BorderRadius.circular(10.0),
  136.                                   borderSide: const BorderSide(
  137.                                       color: Color(0xFFEDAC0B), width: 0.0),
  138.                                 ),
  139.                                 focusedBorder: OutlineInputBorder(
  140.                                   borderRadius: BorderRadius.circular(10.0),
  141.                                   borderSide: const BorderSide(
  142.                                       color: Color(0xFFEDAC0B)),
  143.                                 ),
  144.                                 labelText: 'Password',
  145.                                 labelStyle: TextStyle(
  146.                                     color: Color(0xFF606060),
  147.                                     fontWeight: FontWeight.w300),
  148.                                 hintStyle: TextStyle(
  149.                                     color: Color(0xFF606060),
  150.                                     fontWeight: FontWeight.w300),
  151.                                 prefixIcon: new Icon(Icons.lock,
  152.                                     color: Color(0xFFEDAC0B)),
  153.                                 suffixIcon: new Icon(Icons.visibility_off,
  154.                                     color: Color(0xFF606060)),
  155.                               ),
  156.                               obscureText: true,
  157.                             ),
  158.                           ])))))),
  159.       new Container(
  160.           alignment: Alignment.bottomRight,
  161.           margin: const EdgeInsets.only(
  162.               left: 0.0, right: 30.0, top: 0.0, bottom: 48.0),
  163.           child: GestureDetector(
  164.             onTap: () {
  165.               if (_formKey.currentState.validate()) {
  166.                 _signInWithEmailAndLink();
  167.               }
  168.  
  169.               print("Back To Login Button Clicked");
  170.               Navigator.pop(
  171.                   context,
  172.                   new MaterialPageRoute(
  173.                       maintainState: true, builder: (context) => new Login()));
  174.             },
  175.  
  176. //
  177.  
  178.             child: Image.asset(
  179.               'images/login_button_redarrow.png',
  180.               width: 40.0,
  181.               height: 40.0,
  182.             ),
  183.           )),
  184.       new Container(
  185.           alignment: Alignment.bottomCenter,
  186.           margin: const EdgeInsets.only(
  187.               left: 0.0, right: 00.0, top: 0.0, bottom: 10.0),
  188.           child: new Image.asset(
  189.             'images/footer2.png',
  190.             width: 123.0,
  191.             height: 14.0,
  192.           )),
  193.     ]);
  194.   }
  195.  
  196.   @override
  197.   void dispose() {
  198.     _emailController.dispose();
  199.     WidgetsBinding.instance.removeObserver(this);
  200.     super.dispose();
  201.   }
  202.  
  203.   @override
  204.   void didChangeAppLifecycleState(AppLifecycleState state) async {
  205.     if (state == AppLifecycleState.resumed) {
  206.       final Uri link = await _retrieveDynamicLink();
  207.  
  208.       if (link != null) {
  209.         final FirebaseUser user = await _auth.signInWithEmailAndLink(
  210.           email: _userEmail,
  211.           link: link.toString(),
  212.         );
  213.  
  214.         if (user != null) {
  215.           _userID = user.uid;
  216.           _success = true;
  217.         } else {
  218.           _success = false;
  219.         }
  220.       } else {
  221.         _success = false;
  222.  
  223.         setState(() {});
  224.       }
  225.     }
  226.  
  227.     Future<Uri> _retrieveDynamicLink() async {
  228.       final PendingDynamicLinkData data =
  229.           await FirebaseDynamicLinks.instance.retrieveDynamicLink();
  230.       return data?.link;
  231.     }
  232.  
  233.     Future<void> _signInWithEmailAndLink() async {
  234.       _userEmail = _emailController.text;
  235.  
  236.       return await _auth.sendSignInWithEmailLink(
  237.         email: _userEmail,
  238.         url: 'localhost',
  239.         handleCodeInApp: true,
  240.         iOSBundleID: 'io.flutter.plugins.firebaseAuthExample',
  241.         androidPackageName: 'io.svgo.svgo',
  242.         androidInstallIfNotAvailable: true,
  243.         androidMinimumVersion: "1",
  244.       );
  245.     }
  246.   }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement