import 'package:flutter/material.dart'; import 'package:rezervisanje/globals.dart' as globals; import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; class ReserveTable extends StatefulWidget { @override _ReserveTableState createState() => _ReserveTableState(); } class _ReserveTableState extends State { var sum; var step = 0; var hours = 0; var minutes = 0; DateTime times; final List terminlist = new List(); final List items = new List(); final formKey = GlobalKey(); Future firebaseQuery; @override void initState() { super.initState(); firebaseQuery = Firestore.instance .collection('salons') .document(globals.salonkey) .get() .then((document) { setState(() { globals.shiftstart = document.data['firstshiftstart']; globals.shiftend = document.data['firstshiftend']; }); }); } @override Widget build(BuildContext context) { if (globals.day == null) { globals.day = DateTime( DateTime.now().year, DateTime.now().month, DateTime.now().day); } times = globals.day.add(new Duration(days: -1)); print("Day ; " + globals.day.toString()); print('times : ' + times.toString()); return StreamBuilder( stream: Firestore.instance .collection('termins') .where('time', isLessThanOrEqualTo: globals.day) .where('time', isGreaterThanOrEqualTo: times) .where('employeeID', isEqualTo: globals.employeeID) .snapshots(), builder: (BuildContext context, snapshot) { if (snapshot.hasError) { return new Center(child: Text(snapshot.error)); } else { if (snapshot.hasData) { step = 0; sum = snapshot.data.documents.length; for (int i = 0; i < sum; i++) { DateTime date = snapshot.data.documents[i]['time'].toDate(); terminlist.add(new LoadList(date.hour, date.minute, true)); } return new FutureBuilder( future: firebaseQuery, builder: (BuildContext context, AsyncSnapshot document) { switch (document.connectionState) { case ConnectionState.none: case ConnectionState.waiting: return new Center(child: new CircularProgressIndicator()); default: if (document.hasError) return new Center(child: Text(document.error)); else { items.clear(); print("HOSSZ: " + terminlist.length.toString()); minutes = 0; hours = 0; int div = ((globals.shiftend - globals.shiftstart) * 60 / globals.requiredtime) .round(); hours = globals.shiftstart; for (var i = 0; i < div; i++) { if (minutes == 60) { minutes = 0; hours = hours + 1; } if (sum != 0 && sum != null) { if (terminlist[step].hours == hours && terminlist[step].minutes == minutes && step < sum) { items.add(new LoadList(terminlist[step].hours, terminlist[step].minutes, true)); step = step + 1; } else items.add(new LoadList(hours, minutes, false)); } else { items.add(new LoadList(hours, minutes, false)); } minutes += globals.requiredtime; } print(items.length); return new Scaffold( appBar: new AppBar( title: new Text(globals.employee == "" ? "Rezervisanje" : globals.employee), backgroundColor: Colors.deepOrange, ), drawer: new Drawer( child: new ListView(children: [ new ListTile( title: new Text("Zatvori"), trailing: new Icon(Icons.arrow_upward), onTap: () { Navigator.of(context).pop(); }) ])), body: Card( child: Padding( padding: EdgeInsets.all(8.0), child: Form( key: formKey, child: new ListView.builder( scrollDirection: Axis.vertical, itemCount: items.length, itemBuilder: (context, index) { new Divider( height: 15.0, color: Colors.black, ); return new Card( color: items[index].res ? Colors.deepOrange : Colors.greenAccent, child: new Column( children: [ new Column( children: [ new Container( child: items[index].minutes == 0 ? new Text( items[index] .hours .toString() + ':' + items[index] .minutes .toString() + '0', style: new TextStyle( fontSize: 22), ) : new Text( items[index] .hours .toString() + ':' + items[index] .minutes .toString(), style: new TextStyle( fontSize: 22), )), new Container( height: 15.0, ), new Text(items[index].res ? "Rezervisano" : "Slobodno"), new Divider( height: 15.0, color: Colors.black, ), ], ) ], ), ); })), ))); } } }, ); } else { return new Text("Pokusaj ponovo!"); } } }, ); } } class LoadList { final hours; final minutes; final res; LoadList(this.hours, this.minutes, this.res); } //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 //when at building the snapshot is not empty its working fine