Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- import 'package:cloud_firestore/cloud_firestore.dart';
- final databaseReference = Firestore.instance;
- final CollectionReference collectionReference = databaseReference.collection('users');
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'fApp',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'fApp'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- DocumentSnapshot currentDocument;
- String yourName;
- int counter;
- List names = new List();
- List counts = new List();
- _updateData() async {
- await databaseReference
- .collection('users')
- .document(currentDocument.documentID)
- .updateData({'count': counter});
- }
- void _incrementCounter(){
- setState(() {
- counter++;
- _updateData();
- });
- }
- void _decrementCounter(){
- setState(() {
- counter--;
- _updateData();
- });
- }
- @override
- Widget build(BuildContext context) {
- //if (currentDocument['name'] != null && currentDocument['count'] != null){
- //counter = currentDocument['count'];
- //}
- void syncBase() {
- Firestore.instance
- .collection('users')
- .document(yourName)
- .get()
- .then((DocumentSnapshot ds) {
- counter = ds['count'];
- // use ds as a snapshot
- });
- }
- yourName = "Fran";
- syncBase();
- print(counter);
- final topBar = AppBar(
- elevation: 0.1,
- backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
- title: Text(counter.toString()),
- centerTitle: true,
- );
- final flutContent = StreamBuilder<QuerySnapshot>(
- stream: Firestore.instance.collection('users').snapshots(),
- builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
- names.clear();
- counts.clear();
- //_lista.clear();
- if (snapshot.hasError)
- return new Text('Error: ${snapshot.error}');
- if (!snapshot.hasData) return LinearProgressIndicator();
- switch (snapshot.connectionState) {
- //case ConnectionState.waiting: return new Text('Loading...');
- default:
- return new ListView(
- children: snapshot.data.documents.map((DocumentSnapshot document) {
- //_lista.add(Person(document['name'], document['count']));
- names.add(document['name']);
- counts.add(document['count']);
- print(document['name']);
- print(document['count']);
- if (document['name'] == yourName){
- currentDocument = document;
- print(document['name'] + " u bazi");
- print(document['count'].toString() + " u bazi");
- counter = document['count'];
- }
- return new Card(
- child: ListTile(
- title: new Text(document['name'], style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0)),
- trailing: new Text(document['count'].toString(), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0)),
- )
- );
- }).toList(),
- );
- }
- },
- );
- final bottomBar = BottomAppBar(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- IconButton(
- icon: Icon(Icons.remove_circle),
- iconSize: 50.0,
- padding: const EdgeInsets.all(10.0),
- onPressed: _decrementCounter,
- ),
- IconButton(
- icon: Icon(Icons.add_circle),
- iconSize: 50.0,
- padding: const EdgeInsets.all(10.0),
- onPressed: _incrementCounter,
- ),
- ],
- ),
- );
- return Scaffold(
- appBar: topBar,
- body: flutContent,
- bottomNavigationBar: bottomBar,
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement