Advertisement
PotiSolhdoost

Untitled

Mar 14th, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.32 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:restaurant_management/employee/employee.dart';
  3.  
  4. import '../firebase/authentication.dart';
  5. import '../firebase/db.dart';
  6.  
  7. class Signup extends StatelessWidget {
  8.   const Signup({super.key});
  9.  
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return Scaffold(
  13.       appBar: AppBar(
  14.         elevation: 0,
  15.         automaticallyImplyLeading: true,
  16.         leading: IconButton(
  17.           icon: Icon(Icons.arrow_back),
  18.           onPressed: () => Navigator.of(context).pop(),
  19.         ),
  20.         backgroundColor: const Color(0xFF68A268),
  21.         shape: const RoundedRectangleBorder(
  22.           borderRadius: BorderRadius.only(
  23.             bottomLeft: Radius.circular(50.0),
  24.             bottomRight: Radius.circular(50.0),
  25.           ),
  26.         ),
  27.         toolbarHeight: 100,
  28.         flexibleSpace: Column(
  29.           crossAxisAlignment: CrossAxisAlignment.start,
  30.           mainAxisAlignment: MainAxisAlignment.center,
  31.           children: [
  32.             Center(
  33.               child: Container(
  34.                 height: 85,
  35.                 width: 110,
  36.                 decoration: const BoxDecoration(
  37.                   image: DecorationImage(
  38.                       image: AssetImage('assets/logo.png'), fit: BoxFit.cover),
  39.                 ),
  40.               ),
  41.             )
  42.           ],
  43.         ),
  44.       ),
  45.       body: SingleChildScrollView(
  46.         child: Column(
  47.           mainAxisAlignment: MainAxisAlignment.spaceAround,
  48.           children: <Widget>[
  49.             const SizedBox(height: 50),
  50.             const Text('Restaurant',
  51.                 textAlign: TextAlign.center,
  52.                 style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30)),
  53.             Padding(
  54.               padding: const EdgeInsets.all(8.0),
  55.               child: SignupForm(),
  56.             ),
  57.           ],
  58.         ),
  59.       ),
  60.     );
  61.   }
  62. }
  63.  
  64. class SignupForm extends StatefulWidget {
  65.   SignupForm({Key? key}) : super(key: key);
  66.  
  67.   @override
  68.   _SignupFormState createState() => _SignupFormState();
  69. }
  70.  
  71. class _SignupFormState extends State<SignupForm> {
  72.   final _formKey = GlobalKey<FormState>();
  73.   TextEditingController email = TextEditingController();
  74.   TextEditingController name = TextEditingController();
  75.   TextEditingController lastName = TextEditingController();
  76.   TextEditingController password = TextEditingController();
  77.   String? selectedRole; // Variable to hold the selected role
  78.   List<String> roles = [
  79.     'Manager',
  80.     'Chef',
  81.     'Waiter',
  82.     'Cleaner'
  83.   ]; // List of roles
  84.   bool _obscureText = false;
  85.   bool agree = false;
  86.  
  87.   snapBarBuilder(String message) {
  88.     final snackBar = SnackBar(
  89.       content: Text(message),
  90.     );
  91.  
  92.     // Find the ScaffoldMessenger in the widget tree
  93.     // and use it to show a SnackBar.
  94.     ScaffoldMessenger.of(context).showSnackBar(snackBar);
  95.   }
  96.  
  97.   @override
  98.   Widget build(BuildContext context) {
  99.     var border = const OutlineInputBorder(
  100.       borderRadius: BorderRadius.all(
  101.         Radius.circular(100.0),
  102.       ),
  103.     );
  104.  
  105.     var space = const SizedBox(height: 10);
  106.     return Form(
  107.       key: _formKey,
  108.       child: SingleChildScrollView(
  109.         child: Column(
  110.           children: <Widget>[
  111.             TextFormField(
  112.               controller: name,
  113.               decoration: InputDecoration(
  114.                   prefixIcon: const Icon(Icons.person),
  115.                   labelText: 'Name',
  116.                   border: border),
  117.               validator: (value) {
  118.                 if (value!.isEmpty) {
  119.                   return 'Please enter some text';
  120.                 }
  121.                 return null;
  122.               },
  123.               keyboardType: TextInputType.emailAddress,
  124.             ),
  125.             space,
  126.             TextFormField(
  127.               controller: lastName,
  128.               decoration: InputDecoration(
  129.                   prefixIcon: const Icon(Icons.person),
  130.                   labelText: 'Last Name',
  131.                   border: border),
  132.               validator: (value) {
  133.                 if (value!.isEmpty) {
  134.                   return 'Please enter some text';
  135.                 }
  136.                 return null;
  137.               },
  138.               keyboardType: TextInputType.emailAddress,
  139.             ),
  140.  
  141.             space,
  142.  
  143.             // email
  144.             TextFormField(
  145.               controller: email,
  146.               decoration: InputDecoration(
  147.                   prefixIcon: const Icon(Icons.email_outlined),
  148.                   labelText: 'Email',
  149.                   border: border),
  150.               validator: (value) {
  151.                 if (value!.isEmpty) {
  152.                   return 'Please enter some text';
  153.                 }
  154.                 return null;
  155.               },
  156.               keyboardType: TextInputType.emailAddress,
  157.             ),
  158.  
  159.             space,
  160.  
  161.             // password
  162.             TextFormField(
  163.               controller: password,
  164.               decoration: InputDecoration(
  165.                 labelText: 'Password',
  166.                 prefixIcon: const Icon(Icons.lock_outline),
  167.                 border: border,
  168.                 suffixIcon: GestureDetector(
  169.                   onTap: () {
  170.                     setState(() {
  171.                       _obscureText = !_obscureText;
  172.                     });
  173.                   },
  174.                   child: Icon(
  175.                     _obscureText ? Icons.visibility_off : Icons.visibility,
  176.                   ),
  177.                 ),
  178.               ),
  179.               obscureText: !_obscureText,
  180.               validator: (value) {
  181.                 if (value!.isEmpty) {
  182.                   return 'Please enter some text';
  183.                 }
  184.                 return null;
  185.               },
  186.             ),
  187.             space,
  188.             // confirm passwords
  189.             TextFormField(
  190.               decoration: InputDecoration(
  191.                 labelText: 'Confirm Password',
  192.                 prefixIcon: const Icon(Icons.lock_outline),
  193.                 border: border,
  194.               ),
  195.               obscureText: true,
  196.               validator: (value) {
  197.                 if (value != password.text) {
  198.                   return 'password not match';
  199.                 }
  200.                 return null;
  201.               },
  202.             ),
  203.             space,
  204.             // name
  205.  
  206.             const SizedBox(
  207.               height: 10,
  208.             ),
  209.             DropdownButtonFormField(
  210.               decoration: InputDecoration(
  211.                 labelText: 'Select Role',
  212.                 border: border,
  213.                 prefixIcon: const Icon(Icons.person_outline),
  214.               ),
  215.               value: selectedRole,
  216.               onChanged: (String? newValue) {
  217.                 setState(() {
  218.                   selectedRole = newValue;
  219.                 });
  220.               },
  221.               validator: (value) =>
  222.                   value == null ? 'Please select a role' : null,
  223.               items: roles.map<DropdownMenuItem<String>>((String value) {
  224.                 return DropdownMenuItem<String>(
  225.                   value: value,
  226.                   child: Text(value),
  227.                 );
  228.               }).toList(),
  229.             ),
  230.  
  231.             space,
  232.             // signUP button
  233.             SizedBox(
  234.               height: 50,
  235.               width: double.infinity,
  236.               child: ElevatedButton(
  237.                 onPressed: () {
  238.                   if (_formKey.currentState!.validate()) {
  239.                     _formKey.currentState!.save();
  240.  
  241.                     AuthenticationHelper()
  242.                         .signUp(
  243.                             email: email.text.trim()!,
  244.                             password: password.text.trim()!)
  245.                         .then((result) {
  246.                       try {
  247.                         if (result) {
  248.                           if (selectedRole == 'Manager') {
  249.                             editUserInfo({'role': selectedRole});
  250.                           } else {
  251.                             String uid = AuthenticationHelper().uid;
  252.                             Employee employee = Employee(
  253.                                 name: name.text.trim(),
  254.                                 lastName: lastName.text.trim(),
  255.                                 email: email.text.trim(),
  256.                                 role: selectedRole,
  257.                                 uid: uid);
  258.                             editUserInfo(employee.toMap());
  259.                             reset_info();
  260.                           }
  261.                           snapBarBuilder('user was been added');
  262.                         }
  263.                       } catch (e) {
  264.                         snapBarBuilder(result);
  265.                       }
  266.                     });
  267.                   }
  268.                 },
  269.                 style: ElevatedButton.styleFrom(
  270.                     backgroundColor: const Color(0xFF68A268),
  271.                     shape: const RoundedRectangleBorder(
  272.                         borderRadius: BorderRadius.all(Radius.circular(24.0)))),
  273.                 child: const Text(
  274.                   'Add user',
  275.                   style: TextStyle(color: Colors.white, fontSize: 20),
  276.                 ),
  277.               ),
  278.             ),
  279.             SizedBox(
  280.               height: 10,
  281.             )
  282.           ],
  283.         ),
  284.       ),
  285.     );
  286.   }
  287.  
  288.   void reset_info() {
  289.     name.text = '';
  290.     lastName.text = '';
  291.     email.text = '';
  292.     selectedRole = null;
  293.   }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement