Skip to content

Commit

Permalink
Fixed all the bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
avijeet108 committed Nov 2, 2021
1 parent 8242da3 commit c003f07
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 10 deletions.
Binary file added images/sunset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion lib/services/crud.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';

class Crud {
Future<void> addData(blogData) async {
FirebaseFirestore.instance
.collection("blogs")
.collection('blogs')
.add(blogData)
.catchError((e) {
print(e);
});
}

getData() async {
return await FirebaseFirestore.instance.collection('blogs').get();
}
}
20 changes: 14 additions & 6 deletions lib/views/create_blog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ class _CreateBlogState extends State<CreateBlog> {
_isLoading = true;
});

firebase_storage.Reference ref = firebase_storage.FirebaseStorage.instance
firebase_storage.Reference refer = firebase_storage
.FirebaseStorage.instance
.ref()
.child('blogImages')
.child("${randomAlphaNumeric(9)}.jpg");

firebase_storage.UploadTask task = ref.putFile(SelectedImage!);
dynamic task = await refer.putFile(SelectedImage!);

var downloadUrl = await ref.getDownloadURL();
String downloadUrl = await refer.getDownloadURL();

// print("----------------------------------------------------------");
// print(downloadUrl);

Map<String, String> blogMap = {
"imgUrl": downloadUrl,
Expand All @@ -51,10 +55,14 @@ class _CreateBlogState extends State<CreateBlog> {
"desc": desc!,
};

crud.addData(blogMap).then((value) {
Navigator.pop(context);
await crud.addData(blogMap);
Navigator.pop(context);
setState(() {
_isLoading = false;
});
} else {}
} else {
print("@@@@@@@");
}
}

@override
Expand Down
113 changes: 112 additions & 1 deletion lib/views/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:blog_stack/services/crud.dart';
import 'package:blog_stack/views/create_blog.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
Expand All @@ -7,6 +9,50 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
Crud crud = new Crud();
QuerySnapshot? blogsSnapshot;
bool fetching = true;

Widget blogsList() {
return Container(
child: Column(
children: [
ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 16),
shrinkWrap: true,
itemCount: blogsSnapshot!.docs.length,
itemBuilder: (context, index) {
print((blogsSnapshot!.docs[index].data()));
return BlogsTile(
author: ((blogsSnapshot!.docs[index].data())!
as Map)['authorName'],
imgurl:
((blogsSnapshot!.docs[index].data())! as Map)['imgUrl'],
description:
((blogsSnapshot!.docs[index].data())! as Map)['desc'],
title: ((blogsSnapshot!.docs[index].data())! as Map)['title'],
);
}),
],
),
);
}

getDetails() async {
final result = await crud.getData();
blogsSnapshot = result;
// print(blogsSnapshot);
setState(() {
fetching = false;
});
}

@override
void initState() {
super.initState();
getDetails();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -30,7 +76,15 @@ class _HomePageState extends State<HomePage> {
backgroundColor: Colors.transparent,
elevation: 0.0,
),
body: Container(),
body: fetching
? Container(
child: Center(
child: CircularProgressIndicator(),
),
)
: Container(
child: blogsList(),
),
floatingActionButton: Container(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Row(
Expand All @@ -49,3 +103,60 @@ class _HomePageState extends State<HomePage> {
);
}
}

class BlogsTile extends StatelessWidget {
String? imgurl, author, title, description;

BlogsTile({this.imgurl, this.author, this.title, this.description});

@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 16),
height: 150,
child: Stack(
children: [
ClipRRect(
child: Image.network(
imgurl!,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(6),
),
Container(
height: 150,
decoration: BoxDecoration(
color: Colors.black45.withOpacity(0.3),
borderRadius: BorderRadius.circular(6),
),
),
Container(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
title!,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w500),
),
SizedBox(
height: 4,
),
Text(
description!,
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w400),
),
SizedBox(
height: 4,
),
Text(author!),
],
),
)
],
),
);
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ flutter:
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
assets:
- images/
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
Expand Down

0 comments on commit c003f07

Please sign in to comment.