Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.80 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:rezervisanje/globals.dart' as globals;
  3. import 'dart:async';
  4. import 'package:cloud_firestore/cloud_firestore.dart';
  5.  
  6. class ReserveTable extends StatefulWidget {
  7.   @override
  8.   _ReserveTableState createState() => _ReserveTableState();
  9. }
  10.  
  11. class _ReserveTableState extends State<ReserveTable> {
  12.   var sum;
  13.   var step = 0;
  14.   var hours = 0;
  15.   var minutes = 0;
  16.   DateTime times;
  17.   final List<LoadList> terminlist = new List();
  18.   final List<LoadList> items = new List();
  19.   final formKey = GlobalKey<FormState>();
  20.   Future<int> firebaseQuery;
  21.   @override
  22.   void initState() {
  23.     super.initState();
  24.     firebaseQuery = Firestore.instance
  25.         .collection('salons')
  26.         .document(globals.salonkey)
  27.         .get()
  28.         .then((document) {
  29.       setState(() {
  30.         globals.shiftstart = document.data['firstshiftstart'];
  31.         globals.shiftend = document.data['firstshiftend'];
  32.       });
  33.     });
  34.   }
  35.  
  36.   @override
  37.   Widget build(BuildContext context) {
  38.     if (globals.day == null) {
  39.       globals.day = DateTime(
  40.           DateTime.now().year, DateTime.now().month, DateTime.now().day);
  41.     }
  42.     times = globals.day.add(new Duration(days: -1));
  43.     print("Day ;  " + globals.day.toString());
  44.     print('times : ' + times.toString());
  45.     return StreamBuilder(
  46.       stream: Firestore.instance
  47.           .collection('termins')
  48.           .where('time', isLessThanOrEqualTo: globals.day)
  49.           .where('time', isGreaterThanOrEqualTo: times)
  50.           .where('employeeID', isEqualTo: globals.employeeID)
  51.           .snapshots(),
  52.       builder: (BuildContext context, snapshot) {
  53.         if (snapshot.hasError) {
  54.           return new Center(child: Text(snapshot.error));
  55.         } else {
  56.           if (snapshot.hasData) {
  57.             step = 0;
  58.             sum = snapshot.data.documents.length;
  59.             for (int i = 0; i < sum; i++) {
  60.               DateTime date = snapshot.data.documents[i]['time'].toDate();
  61.  
  62.               terminlist.add(new LoadList(date.hour, date.minute, true));
  63.             }
  64.             return new FutureBuilder(
  65.               future: firebaseQuery,
  66.               builder: (BuildContext context, AsyncSnapshot document) {
  67.                 switch (document.connectionState) {
  68.                   case ConnectionState.none:
  69.                   case ConnectionState.waiting:
  70.                     return new Center(child: new CircularProgressIndicator());
  71.                   default:
  72.                     if (document.hasError)
  73.                       return new Center(child: Text(document.error));
  74.                     else {
  75.                       items.clear();
  76.                       print("HOSSZ: " + terminlist.length.toString());
  77.                       minutes = 0;
  78.                       hours = 0;
  79.                       int div = ((globals.shiftend - globals.shiftstart) *
  80.                               60 /
  81.                               globals.requiredtime)
  82.                           .round();
  83.                       hours = globals.shiftstart;
  84.                       for (var i = 0; i < div; i++) {
  85.                         if (minutes == 60) {
  86.                           minutes = 0;
  87.                           hours = hours + 1;
  88.                         }
  89.                         if (sum != 0 && sum != null) {
  90.                           if (terminlist[step].hours == hours &&
  91.                               terminlist[step].minutes == minutes &&
  92.                               step < sum) {
  93.                             items.add(new LoadList(terminlist[step].hours,
  94.                                 terminlist[step].minutes, true));
  95.                             step = step + 1;
  96.                           } else
  97.                             items.add(new LoadList(hours, minutes, false));
  98.                         } else {
  99.                           items.add(new LoadList(hours, minutes, false));
  100.                         }
  101.                         minutes += globals.requiredtime;
  102.                       }
  103.                       print(items.length);
  104.                       return new Scaffold(
  105.                           appBar: new AppBar(
  106.                             title: new Text(globals.employee == ""
  107.                                 ? "Rezervisanje"
  108.                                 : globals.employee),
  109.                             backgroundColor: Colors.deepOrange,
  110.                           ),
  111.                           drawer: new Drawer(
  112.                               child: new ListView(children: <Widget>[
  113.                             new ListTile(
  114.                                 title: new Text("Zatvori"),
  115.                                 trailing: new Icon(Icons.arrow_upward),
  116.                                 onTap: () {
  117.                                   Navigator.of(context).pop();
  118.                                 })
  119.                           ])),
  120.                           body: Card(
  121.                               child: Padding(
  122.                             padding: EdgeInsets.all(8.0),
  123.                             child: Form(
  124.                                 key: formKey,
  125.                                 child: new ListView.builder(
  126.                                     scrollDirection: Axis.vertical,
  127.                                     itemCount: items.length,
  128.                                     itemBuilder: (context, index) {
  129.                                       new Divider(
  130.                                         height: 15.0,
  131.                                         color: Colors.black,
  132.                                       );
  133.                                       return new Card(
  134.                                         color: items[index].res
  135.                                             ? Colors.deepOrange
  136.                                             : Colors.greenAccent,
  137.                                         child: new Column(
  138.                                           children: <Widget>[
  139.                                             new Column(
  140.                                               children: <Widget>[
  141.                                                 new Container(
  142.                                                     child:
  143.                                                         items[index].minutes ==
  144.                                                                 0
  145.                                                             ? new Text(
  146.                                                                 items[index]
  147.                                                                         .hours
  148.                                                                         .toString() +
  149.                                                                     ':' +
  150.                                                                     items[index]
  151.                                                                         .minutes
  152.                                                                         .toString() +
  153.                                                                     '0',
  154.                                                                 style:
  155.                                                                     new TextStyle(
  156.                                                                         fontSize:
  157.                                                                             22),
  158.                                                               )
  159.                                                             : new Text(
  160.                                                                 items[index]
  161.                                                                         .hours
  162.                                                                         .toString() +
  163.                                                                     ':' +
  164.                                                                     items[index]
  165.                                                                         .minutes
  166.                                                                         .toString(),
  167.                                                                 style:
  168.                                                                     new TextStyle(
  169.                                                                         fontSize:
  170.                                                                             22),
  171.                                                               )),
  172.                                                 new Container(
  173.                                                   height: 15.0,
  174.                                                 ),
  175.                                                 new Text(items[index].res
  176.                                                     ? "Rezervisano"
  177.                                                     : "Slobodno"),
  178.                                                 new Divider(
  179.                                                   height: 15.0,
  180.                                                   color: Colors.black,
  181.                                                 ),
  182.                                               ],
  183.                                             )
  184.                                           ],
  185.                                         ),
  186.                                       );
  187.                                     })),
  188.                           )));
  189.                     }
  190.                 }
  191.               },
  192.             );
  193.           } else {
  194.             return new Text("Pokusaj ponovo!");
  195.           }
  196.         }
  197.       },
  198.     );
  199.   }
  200. }
  201.  
  202. class LoadList {
  203.   final hours;
  204.   final minutes;
  205.   final res;
  206.   LoadList(this.hours, this.minutes, this.res);
  207. }
  208. //when nothing is in snapshot,in listbuilder are only green field,and then when I add some data in firebase it gives me range error,rebuilding solves it
  209. //when at building the snapshot is not empty its working fine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement