Created
March 11, 2020 15:16
-
-
Save ugik/8735b8e021a159fe9ae37ac0af41a69e to your computer and use it in GitHub Desktop.
Flutter Voting App main.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | |
debugShowCheckedModeBanner: false, | |
title: 'Votes', | |
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(centerTitle: true, title: Text('Votes')), | |
body: _buildBody(context), | |
); | |
} | |
Widget _buildBody(BuildContext context) { | |
return StreamBuilder<QuerySnapshot>( | |
stream: Firestore.instance | |
.collection('candidate') | |
.orderBy('votes', descending: true) | |
.orderBy('name') | |
.snapshots(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) return LinearProgressIndicator(); | |
return _buildList(context, snapshot.data.documents); | |
}, | |
); | |
} | |
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) { | |
String topElement = snapshot.elementAt(0).data.values.elementAt(0); | |
return ListView( | |
padding: const EdgeInsets.only(top: 20.0), | |
children: snapshot | |
.map((data) => _buildListItem(context, data, topElement)) | |
.toList(), | |
); | |
} | |
Widget _buildListItem( | |
BuildContext context, DocumentSnapshot data, String topElement) { | |
final record = Record.fromSnapshot(data); | |
return Padding( | |
key: ValueKey(record.name), | |
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), | |
child: Container( | |
decoration: BoxDecoration( | |
border: record.name == topElement | |
? Border.all(color: Colors.grey) | |
: Border.all(color: Colors.lightGreen), | |
color: record.name == topElement ? Colors.lightGreen : Colors.white, | |
borderRadius: BorderRadius.circular(5.0), | |
), | |
child: ListTile( | |
title: Text( | |
record.name, | |
style: new TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold), | |
), | |
trailing: Text( | |
record.votes.toString(), | |
style: new TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold), | |
), | |
onTap: () => record.reference.updateData({'votes': record.votes + 1}), | |
), | |
), | |
); | |
} | |
} | |
class Record { | |
final String name; | |
final int votes; | |
final DocumentReference reference; | |
Record.fromMap(Map<String, dynamic> map, {this.reference}) | |
: assert(map['name'] != null), | |
assert(map['votes'] != null), | |
name = map['name'], | |
votes = map['votes']; | |
Record.fromSnapshot(DocumentSnapshot snapshot) | |
: this.fromMap(snapshot.data, reference: snapshot.reference); | |
@override | |
String toString() => "Record<$name:$votes>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow