Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.25 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:cloud_firestore/cloud_firestore.dart';
  3. import 'package:doctorradar/pages/doctorssubmit.dart';
  4. import 'dart:async';
  5. import 'package:doctorradar/pages/doctorsarea.dart';
  6.  
  7.  
  8. String docid;
  9. bool exist=false;
  10. final Controller = TextEditingController();
  11. bool enter=false;
  12.  
  13.  
  14. class LoginPage extends StatefulWidget {
  15.   static String tag = 'login-page';
  16.   @override
  17.   LoginPageState createState() {
  18.     return new LoginPageState();
  19.   }
  20. }
  21.  
  22. class LoginPageState extends State<LoginPage> {
  23.   final DocumentReference documentReference =
  24.   Firestore.instance.document("Doctors/$docid");
  25.   StreamSubscription <DocumentSnapshot> subscription;
  26.  
  27.   Future<bool> doesNameAlreadyExist(String name, String collection,
  28.       String Field) async {
  29.     final QuerySnapshot result = await Firestore.instance
  30.         .collection(collection)
  31.         .where(Field, isEqualTo: name)
  32.         .limit(1)
  33.         .getDocuments();
  34.     final List<DocumentSnapshot> documents = result.documents;
  35.     final result1 = documents.length == 1;
  36.     setState(() {
  37.       exist = result1;
  38.     });
  39.     return result1;
  40.   }
  41.  
  42.   @override
  43.   Widget build(BuildContext context) {
  44.     final Screensize = MediaQuery
  45.         .of(context)
  46.         .size;
  47.     final logo = Hero(
  48.       tag: 'hero',
  49.       child: CircleAvatar(
  50.         backgroundColor: Colors.transparent,
  51.         radius: 48.0,
  52.         child: Image.asset('lib/images/logo.png'),
  53.       ),
  54.     );
  55.  
  56.     final email = TextFormField(
  57.       keyboardType: TextInputType.emailAddress,
  58.       autofocus: false,
  59.       decoration: InputDecoration(
  60.         hintText: 'Enter Your ID Code',
  61.         contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  62.         border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
  63.       ),
  64.       controller: Controller,
  65.     );
  66.  
  67.     final loginButton = Padding(
  68.       padding: EdgeInsets.symmetric(vertical: 16.0),
  69.       child: Material(
  70.         borderRadius: BorderRadius.circular(30.0),
  71.         shadowColor: Colors.lightBlueAccent.shade100,
  72.         elevation: 5.0,
  73.         child: MaterialButton(
  74.           minWidth: Screensize.width / 1.8,
  75.           height: 42.0,
  76.           onPressed: () {
  77.             doesNameAlreadyExist(Controller.text, "Ids", "ID").then((value){
  78.               if(value==true){
  79.                 subscription = documentReference.snapshots().listen((datasnapshot) {
  80.                   if(datasnapshot.exists){
  81.                     Navigator.push(
  82.                       context,
  83.                       MaterialPageRoute(
  84.                           builder: (context) => DoctorsArea()),
  85.                     );
  86.                   }else if(!datasnapshot.exists){
  87.                     Navigator.push(
  88.                       context,
  89.                       MaterialPageRoute(
  90.                           builder: (context) => Submit()),
  91.                     );
  92.                   }
  93.  
  94.                 });
  95.  
  96.  
  97.               } else if(value!=true){
  98.                 print("wrong creds");
  99.               }
  100.             });
  101.  
  102.           },
  103.           color: Colors.lightBlueAccent,
  104.           child: Text('Continue', style: TextStyle(color: Colors.white)),
  105.         ),
  106.       ),
  107.     );
  108.  
  109.     return Scaffold(
  110.       backgroundColor: Colors.white,
  111.       body: Center(
  112.         child: ListView(
  113.           shrinkWrap: true,
  114.           padding: EdgeInsets.only(left: 24.0, right: 24.0),
  115.           children: <Widget>[
  116.             logo,
  117.             SizedBox(height: Screensize.height / 13.3),
  118.             email,
  119.             SizedBox(height: 8.0),
  120.             SizedBox(height: 24.0),
  121.             loginButton,
  122.             FutureBuilder(
  123.               future: doesNameAlreadyExist(Controller.text, "Ids", "ID"),
  124.               builder: (context, AsyncSnapshot<bool> result) {
  125.                 if (!result.hasData)
  126.                   return Container(); // future still needs to be finished (loading)
  127.                 if (result
  128.                     .data) { // result.data is the returned bool from doesNameAlreadyExists
  129.                   return Text("Your ID exists");
  130.                 }
  131.                 else
  132.                   return Text("Your ID does not exist");
  133.               },
  134.             ),
  135.           ],
  136.         ),
  137.       ),
  138.     );
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement