Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:flutter/material.dart';
- class DatabaseBasic extends StatefulWidget {
- const DatabaseBasic({Key? key}) : super(key: key);
- @override
- _DatabaseBasicState createState() => _DatabaseBasicState();
- }
- class _DatabaseBasicState extends State<DatabaseBasic> {
- TextEditingController name = TextEditingController();
- TextEditingController email = TextEditingController();
- create() async {
- try{
- await FirebaseFirestore.instance.collection('user').doc(email.text).set(
- {
- 'name': name.text,
- 'email': email.text
- }
- );
- } catch(e){
- print(e);
- }
- }
- update() async {
- try{
- await FirebaseFirestore.instance.collection('user').doc(email.text).update({
- 'name': name.text
- });
- } catch(e){
- print(e);
- }
- }
- delete() async {
- try{
- await FirebaseFirestore.instance.collection('user').doc(email.text).delete();
- } catch(e){
- print(e);
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text("Database Basic"),
- centerTitle: true,
- ),
- body: Padding(
- padding: const EdgeInsets.all(10),
- child: Column(
- children: [
- _customTextField(name, "Name"),
- SizedBox(height: 10,),
- _customTextField(email, "Email"),
- SizedBox(height: 10,),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- _customElevatedButton(onPressed: create, btnLabel: "Create"),
- SizedBox(width: 10,),
- _customElevatedButton(onPressed: update, btnLabel: "Update"),
- SizedBox(width: 10,),
- _customElevatedButton(onPressed: delete, btnLabel: "Delete"),
- ],
- ),
- SizedBox(height: 10,),
- Expanded(
- child: Container(
- child: StreamBuilder(
- stream: FirebaseFirestore.instance.collection('user').snapshots(),
- builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
- if(snapshot.hasData){
- return ListView.builder(
- shrinkWrap: true,
- physics: BouncingScrollPhysics(),
- primary: true,
- itemCount: snapshot.data!.docs.length,
- itemBuilder: (context, index){
- QueryDocumentSnapshot networkData = snapshot.data!.docs[index];
- return Card(
- child: ListTile(
- onLongPress: (){
- setState(() {
- name.text = networkData['name'];
- email.text = networkData['email'];
- });
- },
- title: Text(networkData['name']),
- subtitle: Text(networkData['email']),
- trailing: IconButton(
- onPressed: (){
- showDialog(
- context: context,
- builder: (context){
- return AlertDialog(
- title: Text("Confirm!!!"),
- content: Text("Are you sure you?"),
- actions: [
- ElevatedButton(
- onPressed: (){
- name.text = networkData['name'];
- email.text = networkData['email'];
- delete();
- name.clear();
- email.clear();
- Navigator.pop(context);
- },
- child: Text("Yes")),
- ElevatedButton(
- onPressed: (){
- Navigator.pop(context);
- },
- child: Text("No")),
- ],
- );
- }
- );
- },
- icon: Icon(Icons.delete, color: Colors.red,),
- ),
- // leading: Text(networkData['test']),
- ),
- );
- }
- );
- }
- return const Center(child: CircularProgressIndicator());
- }
- ),
- ),
- ),
- ],
- ),
- ),
- );
- }
- Widget _customTextField(TextEditingController controller, String label){
- return TextField(
- controller: controller,
- decoration: InputDecoration(
- label: Text(label),
- border: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.red, width: 2),
- borderRadius: BorderRadius.circular(15)
- ),
- enabledBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.red, width: 2),
- borderRadius: BorderRadius.circular(15)
- ),
- focusedBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.red, width: 2),
- borderRadius: BorderRadius.circular(15)
- ),
- ),
- );
- }
- /// [onPressed] : Give the function that needs to call on pressing the button.
- ///
- /// [btnLabel] : Give the name of the Button.
- Widget _customElevatedButton({required VoidCallback? onPressed, required String btnLabel}) {
- return ElevatedButton(
- onPressed: (){
- onPressed!();
- setState(() {
- name.clear();
- email.clear();
- });
- },
- child: Text(btnLabel),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement