Notes
Notes - notes.io |
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:lab9/user.dart';
import 'package:file_picker/file_picker.dart';
import 'dart:typed_data';
class PostPage extends StatefulWidget {
@override
State<PostPage> createState() => _PostPageState();
}
class _PostPageState extends State<PostPage> {
final TextEditingController postCon = TextEditingController();
final TextEditingController imageUrlCon = TextEditingController();
Uint8List? imageByte;
String? imageName;
void pickImage() async{
final result = await FilePickerWeb.platform.pickFiles(type: FileType.image);
if(result != null && result.files.first.bytes != null){
setState(() {
imageByte = result.files.first.bytes;
imageName = result.files.first.name;
});
}
}
void postSome() async {
final user = FirebaseAuth.instance.currentUser;
final ref = FirebaseStorage.instance.ref().child('posts/$imageName');
await ref.putData(imageByte!);
final imageUrl = await ref.getDownloadURL();
if (user == null || postCon.text.trim().isEmpty) return;
final userData = await FirebaseFirestore.instance.collection('users').doc(user.uid).get();
await FirebaseFirestore.instance.collection('posts').add({
'postText': postCon.text.trim(),
'image': imageUrlCon.text.trim(),
'userName': userData['name'],
'userUid': user.uid,
'timestamp': Timestamp.now(),
});
postCon.clear();
imageUrlCon.clear();
}
@override
void dispose() {
postCon.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final currentUserUid = FirebaseAuth.instance.currentUser?.uid;
return Scaffold(
appBar: AppBar(
title: Text("Posts"),
actions: [
IconButton(
icon: Icon(Icons.account_circle),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (_) => UserPage()),
);
},
)
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
TextField(
controller: postCon,
decoration: InputDecoration(hintText: "Write something..."),
),
// TextField(
// controller: imageUrlCon,
// decoration: InputDecoration(hintText: "Paste image URL (optional)"),
// ),
ElevatedButton(
onPressed: pickImage,
child: Icon(Icons.image)
),
if (imageByte != null)Image.memory(imageByte !, height: 150,),
Row(
children: [
Spacer(),
IconButton(icon: Icon(Icons.send), onPressed: postSome),
],
),
],
),
),
Expanded(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.orderBy('timestamp', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
return Center(child: Text("No posts found"));
}
final posts = snapshot.data!.docs;
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final postData = posts[index].data() as Map;
postData.containsKey('postText');
final isMe = postData['userUid'] == currentUserUid;
final hasText = postData.containsKey('postText') && postData['postText'].toString().isNotEmpty;
final hasImage = postData.containsKey('image') && postData['image'].toString().isNotEmpty;
final timestamp = postData['timestamp'] as Timestamp?;
final dateTime = timestamp?.toDate();
return Container(
margin: EdgeInsets.symmetric(vertical: 6, horizontal: 10),
child: Align(
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
child: Column(
crossAxisAlignment: isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: [
Text(
'${postData['userName']} | $dateTime',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey[700]),
),
if (hasText)
Container(
margin: EdgeInsets.only(top: 4),
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: isMe ? Colors.blue[100] : Colors.grey[300],
borderRadius: BorderRadius.circular(12),
),
child: Text(postData['postText']),
),
if (hasImage)
Container(
margin: EdgeInsets.only(top: 4),
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: isMe ? Colors.blue[50] : Colors.grey[200],
borderRadius: BorderRadius.circular(12),
),
child: Image.network(postData['image']),
),
],
),
),
);
}
);
},
),
),
],
),
);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
