Skip to content

Commit

Permalink
Merge pull request harveyjavier#3 from harveyjavier/add-data-from-fir…
Browse files Browse the repository at this point in the history
…ebase-on-newsfeed

Add data from firebase on newsfeed
  • Loading branch information
harveyjavier authored Nov 1, 2019
2 parents 29683b5 + c5a04a4 commit 87f5d1a
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 39 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.bicolit"
minSdkVersion 23
minSdkVersion 21
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
1 change: 1 addition & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
/*org.gradle.jvmargs=-Xmx1024m*/
Binary file added assets/images/default-profile-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion lib/screens/edit_education.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _EditEducationState extends State<EditEducation> {
backgroundColor: Colors.black,
actions: <Widget>[
FlatButton(
child: Text("Submit", style: TextStyle(color: Colors.white),),
child: Text("Save", style: TextStyle(color: Colors.white),),
onPressed: next,
),
// IconButton(
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/edit_experience.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _EditExperienceState extends State<EditExperience> {
backgroundColor: Colors.black,
actions: <Widget>[
FlatButton(
child: Text("Submit", style: TextStyle(color: Colors.white),),
child: Text("Save", style: TextStyle(color: Colors.white),),
onPressed: next,
),
],
Expand Down
205 changes: 176 additions & 29 deletions lib/screens/news_feed.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,98 @@
import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:date_format/date_format.dart';
import 'dart:io';

import 'package:bicolit/utils/uidata.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'package:bicolit/tools/drawer.dart';
import 'package:bicolit/tools/label_icon.dart';

class NewsFeed extends StatefulWidget {
@override
State<StatefulWidget> createState() => _NewsFeedState();
}

class _NewsFeedState extends State<NewsFeed> {
GlobalKey<ScaffoldState> _globalKey = GlobalKey();
final db = Firestore.instance;
final storage = LocalStorage("data");
final _formKey = GlobalKey<FormState>();

bool _loading = true;
RefreshController _refreshController = RefreshController();
List _users = [], _posts = [];

List<Widget> buildList() {
return List.generate(_posts.length, (i) =>
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation: 2.0,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
InkWell(
onTap: () {},
child: CircleAvatar(backgroundImage: _posts[i]["user"]["profile_image"] == null
? AssetImage(UIData.defaultProfileImage)
: NetworkImage(_posts[i]["user"]["profile_image"])),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(_posts[i]["user"]["firstname"] + " " + _posts[i]["user"]["lastname"], style: Theme.of(context).textTheme.body1.apply(fontWeightDelta: 700)),
SizedBox(height: 5.0),
Text(_posts[i]["user"]["email"]),
],
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(_posts[i]["post"]),
),
SizedBox(height: 10.0),
_posts[i]["images"].length == 0 ? Image.network(_posts[i]["images"][0], fit: BoxFit.cover) : Container(),
_posts[i]["images"].length == 0 ? Container() : Divider(color: Colors.grey.shade300, height: 8.0),
FittedBox(
fit: BoxFit.contain,
child: ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
LabelIcon(
icon: Icons.favorite, iconColor: Colors.black, onPressed: () {},
label: (_posts[i]["likes"].length > 0 ? _posts[i]["likes"].length : "") + "Likes",
),
LabelIcon(
icon: Icons.comment, iconColor: Colors.black, onPressed: () {},
label: (_posts[i]["comments"].length > 0 ? _posts[i]["comments"].length : "") + "Comments",
),
Text(
formatDate(_posts[i]["created_at"].toDate(), [yyyy, " ", M, " ", d, " at ", h, ":", nn, " ", am]),
style: TextStyle(color: Colors.grey),
),
],
),
),
],
),
),
),
);
}

@override
initState() {
Expand All @@ -22,41 +101,109 @@ class _NewsFeedState extends State<NewsFeed> {
}

void onMount() {
print(storage.getItem("user_data"));
fetchData();
}

void fetchData() async {
final QuerySnapshot users_result = await db.collection("users").getDocuments();
final QuerySnapshot newsfeed_result = await db.collection("newsfeed").getDocuments();
final List<DocumentSnapshot> users_docs = users_result.documents;
final List<DocumentSnapshot> newsfeed_docs = newsfeed_result.documents;
newsfeed_docs.forEach((nfd) {
users_docs.forEach((ud) {
if (nfd.data["creator"] == ud.documentID)
{ nfd.data["user"] = ud.data; }
});
});
setState(() {
_users = users_docs;
_posts = newsfeed_docs;
_loading = false;
});
_refreshController.refreshCompleted();
}

Future<bool> _onBack() {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Confirm Exit", style: TextStyle(color: Colors.white),),
content: Text("Are sure you want to exit app?", style: TextStyle(color: Colors.white),),
actions: <Widget>[
FlatButton(
child: Text("No", style: TextStyle(color: Colors.white),),
onPressed: () => Navigator.pop(context, false),
),
FlatButton(
child: Text("Yes", style: TextStyle(color: Colors.white),),
onPressed: () => exit(0),
),
],
),
);
if (_globalKey.currentState.isDrawerOpen) {
Navigator.pop(context);
} else {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Confirm Exit", style: TextStyle(color: Colors.white),),
content: Text("Are sure you want to exit app?", style: TextStyle(color: Colors.white),),
actions: <Widget>[
FlatButton(
child: Text("No", style: TextStyle(color: Colors.white),),
onPressed: () => Navigator.pop(context, false),
),
FlatButton(
child: Text("Yes", style: TextStyle(color: Colors.white),),
onPressed: () => exit(0),
),
],
),
);
}
}

loadScreen() => Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Image.asset(
UIData.bitLogoImage,
height: 70,
width: 70,
),
SizedBox( height: 20.0 ),
SizedBox(
child: CircularProgressIndicator(
strokeWidth: 2.0,
valueColor: AlwaysStoppedAnimation<Color>(Colors.black),
),
height: 17.0,
width: 17.0,
),
SizedBox( height: 20.0 ),
]
);

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBack,
child: Scaffold(
drawer: AppDrawer(current_screen: "newsFeed"),
appBar: AppBar(
title: Text("NewsFeed"),
centerTitle: true,
),
body: Container(),
),
child: _loading
? Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[loadScreen()],
),
),
),
)
: Scaffold(
key: _globalKey,
drawer: AppDrawer(current_screen: "newsFeed"),
appBar: AppBar(
title: Text("News Feed"),
centerTitle: true,
),
body: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
onRefresh: fetchData,
child: CustomScrollView(
slivers: [SliverList(delegate: SliverChildListDelegate(buildList()))],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.edit),
backgroundColor: Colors.black,
),
),
);
}
}
}
5 changes: 4 additions & 1 deletion lib/screens/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:image_picker/image_picker.dart';
import 'package:uuid/uuid.dart';
import 'dart:io';

import 'package:bicolit/utils/uidata.dart';
import 'package:bicolit/tools/common_scaffold.dart';

class Profile extends StatefulWidget {
Expand Down Expand Up @@ -150,7 +151,9 @@ class _ProfileState extends State<Profile> {
onTap: getImage,
child: CircleAvatar(
radius: 40.0,
backgroundImage: NetworkImage("https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"),
backgroundImage: storage.getItem("user_data")["profile_image"] == null
? AssetImage(UIData.defaultProfileImage)
: NetworkImage(storage.getItem("user_data")["profile_image"]),
child: CircularProgressIndicator(
strokeWidth: 3.0,
valueColor: AlwaysStoppedAnimation<Color>(Colors.black),
Expand Down
13 changes: 7 additions & 6 deletions lib/tools/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class _AppDrawerState extends State<AppDrawer> {
WidgetsBinding.instance.addPostFrameCallback((_) => onMount());
}

void onMount() {
print(storage.getItem("user_data"));
}
void onMount() {}

@override
Widget build(BuildContext context) {
Expand All @@ -39,7 +37,9 @@ class _AppDrawerState extends State<AppDrawer> {
storage.getItem("user_data")["email"],
),
currentAccountPicture: CircleAvatar(
backgroundImage: NetworkImage(storage.getItem("user_data")["profile_image"] == null ? "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png" : storage.getItem("user_data")["profile_image"]),
backgroundImage: storage.getItem("user_data")["profile_image"] == null
? AssetImage(UIData.defaultProfileImage)
: NetworkImage(storage.getItem("user_data")["profile_image"]),
),
),
ListTile(
Expand All @@ -51,7 +51,7 @@ class _AppDrawerState extends State<AppDrawer> {
Icons.web,
color: Colors.black,
),
onTap: () { if (widget.current_screen != "newsFeed") Navigator.pushNamed(context, UIData.newsFeedRoute); },
onTap: () { widget.current_screen == "newsFeed" ? Navigator.pop(context) : Navigator.pushNamed(context, UIData.newsFeedRoute); }
),
ListTile(
title: Text(
Expand All @@ -62,7 +62,7 @@ class _AppDrawerState extends State<AppDrawer> {
Icons.person,
color: Colors.black,
),
onTap: () { Navigator.pushNamed(context, UIData.profileRoute); },
onTap: () { widget.current_screen == "profile" ? Navigator.pop(context) : Navigator.pushNamed(context, UIData.profileRoute); },
),
ListTile(
title: Text(
Expand All @@ -74,6 +74,7 @@ class _AppDrawerState extends State<AppDrawer> {
color: Colors.black,
),
onTap: () {},
//onTap: () { widget.current_screen == "about" ? Navigator.pop(context) : Navigator.pushNamed(context, UIData.aboutRoute); },
),
Divider( color: Colors.grey.shade300, height: 8.0, ),
ListTile(
Expand Down
32 changes: 32 additions & 0 deletions lib/tools/label_icon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';

class LabelIcon extends StatelessWidget {
final label;
final icon;
final iconColor;
final onPressed;

LabelIcon(
{this.label, this.icon, this.onPressed, this.iconColor = Colors.grey});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () => onPressed,
child: Row(
children: <Widget>[
Icon(
icon,
color: iconColor,
),
SizedBox(
width: 5.0,
),
Text(
label,
style: TextStyle(fontWeight: FontWeight.w700),
)
],
),
);
}
}
1 change: 1 addition & 0 deletions lib/utils/uidata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class UIData {
static const String imageDir = "assets/images";
static const String bitLogoImage = "$imageDir/bit-logo.png";
static const String bitLogoTransparentImage = "$imageDir/bit-logo-transparent.png";
static const String defaultProfileImage = "$imageDir/default-profile-image.png";

//gneric
static const String error = "Error";
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
pull_to_refresh:
dependency: "direct main"
description:
name: pull_to_refresh
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.7"
quiver:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
image_picker: 0.4.0
uuid: ^2.0.2
progress_dialog: ^1.2.0
pull_to_refresh: ^1.5.7

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit 87f5d1a

Please sign in to comment.