Last active
September 17, 2018 06:34
-
-
Save Abdullamhd/d6c52f578fc62964b1d82116e9363041 to your computer and use it in GitHub Desktop.
the whole widget rebuild when i press like button in flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:foodgram/firebase_auth.dart'; | |
import 'package:foodgram/preview_post.dart'; | |
import 'package:foodgram/random_asset.dart'; | |
import 'package:foodgram/user.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
import 'push_id_generator.dart'; | |
class UserFeedView extends StatefulWidget { | |
@override | |
_UserFeedViewState createState() => _UserFeedViewState(); | |
} | |
class _UserFeedViewState extends State<UserFeedView> { | |
User user ; | |
@override | |
void initState() { | |
// TODO: implement initState | |
super.initState(); | |
getCurrentUser((firebaseUser){ | |
if(firebaseUser != null){ | |
setState(() { | |
user = User( | |
uid: firebaseUser.uid, | |
userName: firebaseUser.displayName | |
); | |
}); | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.orangeAccent.shade100, | |
appBar: AppBar( | |
title: Text( | |
'Foodgram', | |
style: TextStyle( | |
fontWeight: FontWeight.w200, | |
), | |
), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.lock), | |
onPressed: () { | |
FirebaseAuth.instance.signOut(); | |
}, | |
) | |
], | |
), | |
body: Center( | |
child: user == null ? Text('please login') : PostListView( | |
user: user, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
backgroundColor: Colors.brown, | |
onPressed: selectImageFromGallaray, | |
child: Icon( | |
Icons.add, | |
color: Colors.white, | |
), | |
), | |
); | |
} | |
selectImageFromGallaray() async { | |
var imageFile = await ImagePicker.pickImage(source: ImageSource.camera); | |
displayPostPreviewScreen(imageFile); | |
} | |
void displayPostPreviewScreen(File imageFile) { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (BuildContext context) { | |
return PreviewPost(imageFile: imageFile, user: user); | |
}), | |
); | |
} | |
} | |
class PostListView extends StatefulWidget { | |
final User user ; | |
PostListView({ | |
this.user | |
}); | |
@override | |
_UserListViewState createState() => _UserListViewState(); | |
} | |
class _UserListViewState extends State<PostListView> { | |
@override | |
Widget build(BuildContext context) { | |
return new StreamBuilder<QuerySnapshot>( | |
stream: Firestore.instance.collection('posts').snapshots(), | |
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { | |
if (!snapshot.hasData) return new Text('no post available'); | |
return new ListView( | |
children: snapshot.data.documents.map((DocumentSnapshot document) { | |
return createListItem(document); | |
}).toList(), | |
); | |
}, | |
); | |
} | |
Widget createListItem(DocumentSnapshot document) { | |
return Padding( | |
padding: EdgeInsets.all(8.0), | |
child: Container( | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular(10.0), | |
boxShadow: [ | |
BoxShadow( | |
color: Colors.black.withOpacity(.3), | |
blurRadius: 5.0, | |
spreadRadius: 4.0, | |
) | |
]), | |
child: ClipRRect( | |
borderRadius: BorderRadius.circular(10.0), | |
child: Material( | |
child: Stack( | |
children: <Widget>[ | |
Image.asset( | |
randomAsset(), | |
fit: BoxFit.cover, | |
), | |
Positioned( | |
right: 0.0, | |
left: 0.0, | |
bottom: 0.0, | |
child: Container( | |
padding: EdgeInsets.all(20.0), | |
decoration: BoxDecoration( | |
gradient: LinearGradient( | |
begin: Alignment.topCenter, | |
end: Alignment.bottomCenter, | |
colors: [Colors.transparent, Colors.black])), | |
child: Row( | |
children: <Widget>[ | |
Expanded( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start | |
, | |
children: <Widget>[ | |
Text( | |
document['title'], | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 20.0, | |
fontWeight: FontWeight.w300), | |
), | |
Text( | |
'secondary title', | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 15.0, | |
fontWeight: FontWeight.w300), | |
), | |
], | |
), | |
), | |
IconButton( | |
onPressed: (){ | |
print('on pressed on like button'); | |
makeLike(document); | |
}, | |
icon: Icon(Icons.favorite,color: Colors.white,size: 20.0,) , | |
), | |
Text(document['likeCount']==0 ? '' : document['likeCount'].toString() , | |
style: TextStyle( | |
color: Colors.white , | |
fontSize: 20.0 | |
), | |
) | |
], | |
), | |
), | |
) | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
void makeLike(DocumentSnapshot document) { | |
final postID = document.documentID ; // currentPostID | |
final userID = widget.user.uid ; | |
// a unique ID which prevent user make multiple likes in one post :D | |
final likeID = postID + '__' + userID ; // | |
final firestore = Firestore.instance ; | |
Map<String,dynamic> map = Map(); | |
map.putIfAbsent('userId', ()=> userID); | |
map.putIfAbsent('postId', ()=> postID); | |
firestore.collection('likes').document(likeID) | |
.setData(map) | |
.then((avoid){ | |
// like success | |
print('like success '); | |
}).catchError((error){ | |
// error | |
print('like error'); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment