Skip to content

Instantly share code, notes, and snippets.

@smashah
Last active January 30, 2019 22:12
Show Gist options
  • Save smashah/ec4784c7d9889f6514347473b971e0a5 to your computer and use it in GitHub Desktop.
Save smashah/ec4784c7d9889f6514347473b971e0a5 to your computer and use it in GitHub Desktop.
A firebase & ListTile implementation that disables the tile within onTap during a transaction
import 'package:baby_names/my_list_tile.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Baby Names',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Baby Names Codelabs'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Record> _items = new List<Record>();
Map<String, bool> eState = new Map<String, bool>();
_MyHomePageState() {
Firestore.instance.collection('baby').snapshots().listen((data) {
_items.removeRange(0, _items.length);
setState(() {
data.documents.forEach((doc) => _items.add(Record.fromSnapshot(doc)));
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
padding: const EdgeInsets.only(top: 20.0),
itemCount: _items.length,
itemBuilder: (context, index) => _buildListItem(context, index)));
}
Widget _buildListItem(BuildContext context, int index) {
var record = _items[index];
var e = eState[record.name] == null ? true : eState[record.name];
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
enabled: e,
onTap: () =>
Firestore.instance.runTransaction((transaction) async {
setState(() {
eState[record.name] = false;
});
final freshSnapshot =
await transaction.get(record.reference);
final fresh = Record.fromSnapshot(freshSnapshot);
await transaction
.update(record.reference, {'votes': fresh.votes + 1});
setState(() {
eState[record.name] = true;
});
})),
));
}
}
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>";
}
@smashah
Copy link
Author

smashah commented Jan 30, 2019

A tiny improvement on the firebase flutter codelab

https://codelabs.developers.google.com/codelabs/flutter-firebase/#0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment