Advertisement
Zanak

flutter main_page

May 18th, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:firestore_demo/item_card.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:google_fonts/google_fonts.dart';
  5. import 'item_card.dart';
  6.  
  7. class MainPage extends StatelessWidget {
  8. final TextEditingController nameController = TextEditingController();
  9. final TextEditingController ageController = TextEditingController();
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. FirebaseFirestore firestore = FirebaseFirestore.instance;
  14. CollectionReference users = firestore.collection('users');
  15. return Scaffold(
  16. appBar: AppBar(
  17. backgroundColor: Colors.blue[900],
  18. title: Text('Firestore Demo'),
  19. ),
  20. backgroundColor: Colors.white,
  21. body: Stack(
  22. children: [
  23. ListView(
  24. children: [
  25. //// VIEW DATA HERE
  26. FutureBuilder<QuerySnapshot>(
  27. future: users.get(),
  28. builder: (_, snapshot) {
  29. if (snapshot.hasData) {
  30. return Column(
  31. children: snapshot.data.docs
  32. .map((e) =>
  33. ItemCard(e.data()['name'], e.data()['age']))
  34. .toList(),
  35. );
  36. } else {
  37. return Text('Loading');
  38. }
  39. }),
  40.  
  41. SizedBox(
  42. height: 150,
  43. )
  44. ],
  45. ),
  46. Align(
  47. alignment: Alignment.bottomCenter,
  48. child: Container(
  49. padding: const EdgeInsets.symmetric(horizontal: 15),
  50. decoration: BoxDecoration(color: Colors.white, boxShadow: [
  51. BoxShadow(
  52. color: Colors.black12,
  53. offset: Offset(-5, 0),
  54. blurRadius: 15,
  55. spreadRadius: 3)
  56. ]),
  57. width: double.infinity,
  58. height: 130,
  59. child: Row(
  60. children: [
  61. SizedBox(
  62. width: MediaQuery.of(context).size.width - 160,
  63. child: Column(
  64. mainAxisAlignment: MainAxisAlignment.center,
  65. children: [
  66. TextField(
  67. style: GoogleFonts.poppins(),
  68. controller: nameController,
  69. decoration: InputDecoration(hintText: "Name"),
  70. ),
  71. TextField(
  72. style: GoogleFonts.poppins(),
  73. controller: ageController,
  74. decoration: InputDecoration(hintText: "Age"),
  75. keyboardType: TextInputType.number,
  76. ),
  77. ],
  78. ),
  79. ),
  80. Container(
  81. height: 130,
  82. width: 130,
  83. padding: const EdgeInsets.fromLTRB(15, 15, 0, 15),
  84. child: RaisedButton(
  85. shape: RoundedRectangleBorder(
  86. borderRadius: BorderRadius.circular(8)),
  87. color: Colors.blue[900],
  88. child: Text(
  89. 'Add Data',
  90. style: GoogleFonts.poppins(
  91. color: Colors.white,
  92. fontWeight: FontWeight.bold),
  93. ),
  94. onPressed: () {
  95. //// ADD DATA HERE
  96.  
  97. users.add({
  98. 'name': nameController.text,
  99. 'age': int.tryParse(ageController.text) ?? 0,
  100. });
  101.  
  102. nameController.text = '';
  103. ageController.text = ' ';
  104. }),
  105. )
  106. ],
  107. ),
  108. )),
  109. ],
  110. ));
  111. }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement