flutter-app/flutter_app/lib/main.dart

197 lines
5.0 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Names',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() {
return _MyHomePageState();
}
}
class _MyHomePageState extends State<MyHomePage> {
final db = Firestore.instance.collection('lots');
static int lot = 25;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(lot.toString())),
body:
// _buildLot(context, db),
_buildBody(
context, db.document("Lot" + lot.toString()).collection('stalls'), 4),
floatingActionButton: FloatingActionButton(
child: Text("Next Lot"),
onPressed: () {
setState(() {
lot++;
});
},
),
);
}
}
Widget _buildLot(BuildContext context, CollectionReference lotDB) {
return StreamBuilder<QuerySnapshot>(
stream: lotDB.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildLotList(context, snapshot.data.documents);
},
);
}
Widget _buildLotList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
children:
snapshot.map((data) => _buildListLotItem(context, data)).toList(),
);
}
Widget _buildListLotItem(BuildContext context, DocumentSnapshot data) {
final record = LotRecord.fromSnapshot(data);
return Text(record.stall.toString());
}
Widget _(BuildContext context, CollectionReference stallDB, int col) {
return StreamBuilder<QuerySnapshot>(
stream: stallDB.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents, col);
},
);
}
Widget _buildBody(BuildContext context, CollectionReference stallDB, int col) {
return StreamBuilder<QuerySnapshot>(
stream: stallDB.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents, col);
},
);
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot,
int col) {
return GridView.count(
primary: false,
padding: const EdgeInsets.all(60),
crossAxisSpacing: 5.0,
crossAxisCount: col,
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
return
Card(
key: ValueKey(record.stall),
color: (record.open ? Colors.green[200] : Colors.red[100]),
child: Center(
child: Column(
children: [
ListTile(
title: Text("Stall " + record.stall.toString(),
style: TextStyle(fontWeight: FontWeight.w500)),
subtitle: Text(record.open.toString()),
),
],
),
),
);
}
class LotRecord {
final int stall;
final int passID;
final bool open;
final bool handicap;
final DocumentReference reference;
LotRecord.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['handicap'] != null),
assert(map['stall'] != null),
assert(map['passID'] != null),
assert(map['open'] != null),
stall = map['stall'],
passID = map['passID'],
open = map['open'],
handicap = map['handicap'];
LotRecord.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$stall>";
}
class lotRecord {
final int stall;
final int passID;
final bool open;
final bool handicap;
final DocumentReference reference;
lotRecord.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['handicap'] != null),
assert(map['stall'] != null),
assert(map['passID'] != null),
assert(map['open'] != null),
stall = map['stall'],
passID = map['passID'],
open = map['open'],
handicap = map['handicap'];
lotRecord.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$stall>";
}
class Record {
final int stall;
final int passID;
final bool open;
final bool handicap;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['handicap'] != null),
assert(map['stall'] != null),
assert(map['passID'] != null),
assert(map['open'] != null),
stall = map['stall'],
passID = map['passID'],
open = map['open'],
handicap = map['handicap'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$stall>";
}