Advertisement
Guest User

Untitled

a guest
Feb 13th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:flutter/material.dart';
  3.  
  4. class Item {
  5. Item({this.nome, this.checked = false, this.uid});
  6. String nome;
  7. String uid;
  8. bool checked;
  9. }
  10.  
  11. class Relatorios extends StatefulWidget {
  12. @override
  13. _RelatoriosState createState() => _RelatoriosState();
  14. }
  15.  
  16. class _RelatoriosState extends State<Relatorios> {
  17. final List<Item> itens = [
  18. Item(nome: "Matheus"),
  19. Item(nome: "JĂșlio"),
  20. Item(nome: "Caio")
  21. ];
  22.  
  23. _listenToData(){
  24. Firestore.instance.collection("usuarios")
  25. .where("convite", isEqualTo: "yWiGl").snapshots().listen((snap){
  26. itens.clear();
  27. setState(() {
  28. snap.documents.forEach((d) {
  29. itens.add(Item(nome:
  30. d.data["nome"], uid: d.data["uid"]));
  31. });
  32. });
  33. });
  34. }
  35.  
  36. @override
  37. void initState() {
  38. _listenToData();
  39. super.initState();
  40. }
  41.  
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. body: ListView.builder(
  46. itemCount: itens.length,
  47. itemBuilder: (_, int index) {
  48. Item ad = itens[index];
  49. return ItemLista(item: itens[index]);
  50. },
  51. ),
  52. floatingActionButton: FloatingActionButton(
  53. child: Icon(Icons.save),
  54. onPressed: salvar,
  55. ));
  56. }
  57.  
  58. void salvar() {
  59. List<Item> itensMarcados = List.from(itens.where((item) => item.checked));
  60.  
  61. itensMarcados.forEach((item) {
  62. print(item.nome);
  63. });
  64. }
  65. }
  66.  
  67. class ItemLista extends StatefulWidget {
  68. const ItemLista({ Key key, this.item }) : super(key: key);
  69.  
  70. final Item item;
  71.  
  72. @override
  73. _ItemListaState createState() => _ItemListaState();
  74. }
  75.  
  76. class _ItemListaState extends State<ItemLista> {
  77.  
  78.  
  79. @override
  80. Widget build(BuildContext context) {
  81. return CheckboxListTile(
  82. title: Text(widget.item.nome),
  83. value: widget.item.checked,
  84. onChanged: (bool value){
  85. setState((){
  86. widget.item.checked = value;
  87. print(widget.item.nome);
  88. print(widget.item.uid);
  89. });
  90. },
  91. );
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement