You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.7 KiB
102 lines
2.7 KiB
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> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Lot 1')),
|
|
body: _buildBody(context),
|
|
);
|
|
}
|
|
|
|
Widget _buildBody(BuildContext context) {
|
|
return StreamBuilder<QuerySnapshot>(
|
|
stream: Firestore.instance.collection('Lot1').snapshots(),
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData) return LinearProgressIndicator();
|
|
|
|
return _buildList(context, snapshot.data.documents);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
|
|
return ListView(
|
|
padding: const EdgeInsets.only(top: 20.0),
|
|
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
|
|
);
|
|
}
|
|
|
|
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
|
|
final record = Record.fromSnapshot(data);
|
|
|
|
return Padding(
|
|
key: ValueKey(record.stall),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.blue),
|
|
borderRadius: BorderRadius.circular(5.0),
|
|
),
|
|
child: ListTile(
|
|
title: Text("Stall #" + record.stall.toString()),
|
|
trailing: Text("Open: " + record.open.toString()),
|
|
|
|
// onTap: () => Firestore.instance.runTransaction((transaction) async {
|
|
// final freshSnapshot = await transaction.get(record.reference);
|
|
// final fresh = Record.fromSnapshot(freshSnapshot);
|
|
//
|
|
// await transaction
|
|
// .update(record.reference, {'votes': fresh.votes + 1});
|
|
// }),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
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>";
|
|
} |