languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

register_page.dart (5195B)


      1 import 'package:cloud_firestore/cloud_firestore.dart';
      2 import 'package:firebase_auth/firebase_auth.dart';
      3 import 'package:flutter/material.dart';
      4 import 'package:penny/components/my_button.dart';
      5 import 'package:penny/components/my_textfield.dart';
      6 import 'package:penny/components/square_tile.dart';
      7 import 'package:penny/services/auth_service.dart';
      8 
      9 class RegisterPage extends StatefulWidget {
     10   final Function()? onTap;
     11   const RegisterPage({super.key, required this.onTap});
     12 
     13   @override
     14   State<RegisterPage> createState() => _RegisterPageState();
     15 }
     16 
     17 class _RegisterPageState extends State<RegisterPage> {
     18   final emailController = TextEditingController();
     19   final passwordController = TextEditingController();
     20   final confirmPasswordController = TextEditingController();
     21 
     22   void signUserUp() async {
     23     try {
     24       if (passwordController.text != confirmPasswordController.text) {
     25         ScaffoldMessenger.of(context).showSnackBar(
     26           const SnackBar(
     27             content: Text('Passwords do not match!'),
     28           ),
     29         );
     30         return;
     31       }
     32       UserCredential userCredential =
     33           await FirebaseAuth.instance.createUserWithEmailAndPassword(
     34         email: emailController.text,
     35         password: passwordController.text,
     36       );
     37 
     38       FirebaseFirestore.instance
     39           .collection("users")
     40           .doc(userCredential.user!.email)
     41           .set({
     42         'username': emailController.text.split('@')[0],
     43         'machines_collected': 0,
     44       });
     45     } on FirebaseAuthException catch (e) {
     46       SnackBar(
     47         content: Text('Sign up failed: ${e.message}'),
     48       );
     49     }
     50   }
     51 
     52   @override
     53   Widget build(BuildContext context) {
     54     return Scaffold(
     55       backgroundColor: const Color(0xfffffc00),
     56       body: SafeArea(
     57         child: Center(
     58           child: SingleChildScrollView(
     59             child: Column(
     60               children: [
     61                 const Text(
     62                   'Penny',
     63                   style: TextStyle(
     64                     color: Colors.black,
     65                     fontSize: 32,
     66                     fontWeight: FontWeight.bold,
     67                   ),
     68                 ),
     69                 const SizedBox(height: 25),
     70 
     71                 MyTextField(
     72                   controller: emailController,
     73                   hintText: 'Email',
     74                   obscureText: false,
     75                 ),
     76 
     77                 const SizedBox(height: 10),
     78 
     79                 MyTextField(
     80                   controller: passwordController,
     81                   hintText: 'Password',
     82                   obscureText: true,
     83                 ),
     84                 const SizedBox(height: 10),
     85 
     86                 MyTextField(
     87                   controller: confirmPasswordController,
     88                   hintText: 'Confirm Password',
     89                   obscureText: true,
     90                 ),
     91                 const SizedBox(height: 10),
     92 
     93                 MyButton(
     94                   text: "Sign Up",
     95                   onTap: signUserUp,
     96                 ),
     97 
     98                 const SizedBox(height: 50),
     99 
    100                 const Padding(
    101                   padding: EdgeInsets.symmetric(horizontal: 25.0),
    102                   child: Row(
    103                     children: [
    104                       Expanded(
    105                         child: Divider(
    106                           thickness: 0.5,
    107                           color: Colors.black,
    108                         ),
    109                       ),
    110                       Padding(
    111                         padding: EdgeInsets.symmetric(horizontal: 10.0),
    112                         child: Text(
    113                           'Or continue with',
    114                           style: TextStyle(color: Colors.black),
    115                         ),
    116                       ),
    117                       Expanded(
    118                         child: Divider(
    119                           thickness: 0.5,
    120                           color: Colors.black,
    121                         ),
    122                       ),
    123                     ],
    124                   ),
    125                 ),
    126 
    127                 const SizedBox(height: 25),
    128 
    129                 Row(
    130                   mainAxisAlignment: MainAxisAlignment.center,
    131                   children: [
    132                     SquareTile(
    133                         onTap: () => AuthService().signInWithGoogle(),
    134                         imagePath: 'lib/images/google.png'),
    135                   ],
    136                 ),
    137 
    138                 const SizedBox(height: 25),
    139 
    140                 Row(
    141                   mainAxisAlignment: MainAxisAlignment.center,
    142                   children: [
    143                     const Text(
    144                       'Already have an account?',
    145                       style: TextStyle(color: Colors.black),
    146                     ),
    147                     const SizedBox(width: 4),
    148                     GestureDetector(
    149                       onTap: widget.onTap,
    150                       child: const Text(
    151                         'Sign In',
    152                         style: TextStyle(
    153                           color: Colors.blue,
    154                           fontWeight: FontWeight.bold,
    155                         ),
    156                       ),
    157                     ),
    158                   ],
    159                 )
    160               ],
    161             ),
    162           ),
    163         ),
    164       ),
    165     );
    166   }
    167 }