Advertisement
Guest User

fApp

a guest
Sep 13th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.36 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3. import 'package:cloud_firestore/cloud_firestore.dart';
  4.  
  5. final databaseReference = Firestore.instance;
  6. final CollectionReference collectionReference = databaseReference.collection('users');
  7.  
  8. void main() => runApp(MyApp());
  9.  
  10. class MyApp extends StatelessWidget {
  11.  
  12.   @override
  13.  
  14.   Widget build(BuildContext context) {
  15.     return MaterialApp(
  16.       title: 'fApp',
  17.       theme: ThemeData(
  18.         primarySwatch: Colors.blue,
  19.       ),
  20.       home: MyHomePage(title: 'fApp'),
  21.     );
  22.   }
  23. }
  24.  
  25. class MyHomePage extends StatefulWidget {
  26.  
  27.   MyHomePage({Key key, this.title}) : super(key: key);
  28.  
  29.   final String title;
  30.   @override
  31.   _MyHomePageState createState() => _MyHomePageState();
  32. }
  33.  
  34. class _MyHomePageState extends State<MyHomePage> {
  35.  
  36.   DocumentSnapshot currentDocument;
  37.  
  38.   String yourName;
  39.   int counter;
  40.  
  41.   List names = new List();
  42.   List counts = new List();
  43.  
  44.   _updateData() async {
  45.       await databaseReference
  46.           .collection('users')
  47.           .document(currentDocument.documentID)
  48.           .updateData({'count': counter});
  49.   }
  50.  
  51.  
  52.   void _incrementCounter(){
  53.     setState(() {
  54.       counter++;
  55.       _updateData();
  56.     });
  57.   }
  58.  
  59.   void _decrementCounter(){
  60.     setState(() {
  61.       counter--;
  62.       _updateData();
  63.     });
  64.   }
  65.  
  66.   @override
  67.   Widget build(BuildContext context) {
  68.  
  69.   //if (currentDocument['name'] != null && currentDocument['count'] != null){
  70.     //counter = currentDocument['count'];
  71.   //}
  72.  
  73.   void syncBase() {
  74.     Firestore.instance
  75.         .collection('users')
  76.         .document(yourName)
  77.         .get()
  78.         .then((DocumentSnapshot ds) {
  79.           counter = ds['count'];
  80.       // use ds as a snapshot
  81.     });
  82.   }
  83.  
  84.   yourName = "Fran";
  85.  
  86.   syncBase();
  87.  
  88.   print(counter);
  89.  
  90.   final topBar = AppBar(
  91.       elevation: 0.1,
  92.             backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
  93.             title: Text(counter.toString()),
  94.             centerTitle: true,
  95.   );
  96.  
  97.   final flutContent = StreamBuilder<QuerySnapshot>(
  98.       stream: Firestore.instance.collection('users').snapshots(),
  99.       builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
  100.           names.clear();
  101.           counts.clear();
  102.           //_lista.clear();
  103.         if (snapshot.hasError)
  104.           return new Text('Error: ${snapshot.error}');
  105.         if (!snapshot.hasData) return LinearProgressIndicator();
  106.         switch (snapshot.connectionState) {
  107.           //case ConnectionState.waiting: return new Text('Loading...');
  108.           default:
  109.             return new ListView(
  110.               children: snapshot.data.documents.map((DocumentSnapshot document) {
  111.                 //_lista.add(Person(document['name'], document['count']));
  112.  
  113.                 names.add(document['name']);
  114.                 counts.add(document['count']);
  115.  
  116.                 print(document['name']);
  117.  
  118.                 print(document['count']);
  119.                 if (document['name'] == yourName){
  120.                   currentDocument = document;
  121.                   print(document['name'] + " u bazi");
  122.                   print(document['count'].toString() + " u bazi");
  123.                   counter = document['count'];
  124.                 }
  125.                 return new Card(
  126.                   child: ListTile(
  127.                   title: new Text(document['name'], style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0)),
  128.                   trailing: new Text(document['count'].toString(), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0)),
  129.                   )
  130.                 );
  131.               }).toList(),
  132.             );
  133.         }
  134.       },
  135.   );
  136.  
  137.   final bottomBar = BottomAppBar(
  138.       child: Row(
  139.         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  140.         children: <Widget>[
  141.           IconButton(
  142.             icon: Icon(Icons.remove_circle),
  143.             iconSize: 50.0,
  144.             padding: const EdgeInsets.all(10.0),
  145.             onPressed: _decrementCounter,
  146.           ),
  147.           IconButton(
  148.             icon: Icon(Icons.add_circle),
  149.             iconSize: 50.0,
  150.             padding: const EdgeInsets.all(10.0),
  151.             onPressed: _incrementCounter,
  152.           ),          
  153.         ],
  154.       ),
  155.   );
  156.  
  157.  
  158.     return Scaffold(
  159.         appBar: topBar,
  160.         body: flutContent,
  161.         bottomNavigationBar: bottomBar,
  162.     );
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement