Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Charunirathnayake committed Feb 4, 2020
1 parent 950fc33 commit 4d5cbc9
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 49 deletions.
32 changes: 24 additions & 8 deletions miniproject/lib/app_screen/guideprofile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Myprofile extends StatefulWidget {
class Myprofile_State extends State<Myprofile> {
//img pass to the firestorage
File _image;

String url;
String name, address, city, passion,email;
String phonenumber;

Expand Down Expand Up @@ -260,9 +260,7 @@ print('New data added.');

}

@override
Widget build(BuildContext context) {
Future getImage() async {
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
Expand All @@ -271,19 +269,37 @@ print('New data added.');
}

Future uploadpic(BuildContext context) async {
String fileName = basename(_image.path);
/* String fileName = basename(_image.path);*/
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
FirebaseStorage.instance.ref().child("fileName");
var timeKey=DateTime.now();

final StorageUploadTask uploadTask = firebaseStorageRef.child(timeKey.toString()+".jpg").putFile(_image);


var imageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = imageUrl.toString();
print("Image Url=" + url);
saveToDatabase(url);



/*StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;*/
setState(() {
print('Profile picture is uploaded.');
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Profile picture is uploaded.'),
));
});
}
void saveToDatabase(url){

}


@override
Widget build(BuildContext context) {

return ListView(
children: <Widget>[
Column(children: <Widget>[
Expand Down
3 changes: 2 additions & 1 deletion miniproject/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:miniproject/app_screen/passion.dart';
import 'package:miniproject/map_screen/first_screen.dart';
import 'package:miniproject/map_screen/map_traveller.dart';
import 'package:miniproject/post_upload/img_upload.dart';
import 'package:miniproject/post_upload/post_home.dart';



Expand All @@ -31,7 +32,7 @@ void main(){
theme: ThemeData(
primarySwatch: Colors.brown
),
home:Upload_img(),
home:PostHome(),
debugShowCheckedModeBanner: false,
)
);
Expand Down
4 changes: 4 additions & 0 deletions miniproject/lib/post_upload/Posts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Posts {
String image, description, date, time;
Posts(this.image, this.description, this.date, this.time);
}
120 changes: 80 additions & 40 deletions miniproject/lib/post_upload/img_upload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Upload_img extends StatefulWidget {

class Upload_img_State extends State<Upload_img> {
File sampleImage;
String Url;
final formkey = GlobalKey<FormState>();
String _myvalue;

Expand All @@ -25,22 +26,60 @@ class Upload_img_State extends State<Upload_img> {
});
}

bool validateAndsave(){
final form=formkey.currentState;
if(form.validate()){
bool validateAndsave() {
final form = formkey.currentState;
if (form.validate()) {
form.save();
return true;
}
else{
} else {
return false;
}
}

Future<void> UploadStatusImage() async {
if (validateAndsave()) {
final StorageReference PostImgRef =
FirebaseStorage.instance.ref().child("Post Images");
var timeKey = DateTime.now();
final StorageUploadTask uploadTask =
PostImgRef.child(timeKey.toString() + ".jpg").putFile(sampleImage);
var ImageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
Url = ImageUrl.toString();
print("Image Url=" + Url);
saveToDatabase(Url);
gotouploadpage();
}
}

void saveToDatabase(Url) {
var dbTimeKey = DateTime.now();
var formatDate = DateFormat('MMM d,YYYY');
var formatTime = DateFormat('EEEE,hh:mm aaa');

String date = formatDate.format(dbTimeKey);
String time = formatTime.format(dbTimeKey);

DatabaseReference ref = FirebaseDatabase.instance.reference();
var data = {
"image": Url,
"description": _myvalue,
"date": date,
"time": time
};
ref.child("Posts").push().set(data);
}

void gotouploadpage() {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return Upload_img();
}));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor:Color( 0xffBA680B),
backgroundColor: Color(0xffBA680B),
title: Text("Upload Image"),
centerTitle: true,
),
Expand All @@ -51,47 +90,48 @@ class Upload_img_State extends State<Upload_img> {
onPressed: getImage,
tooltip: 'Add Image',
child: Icon(Icons.add_a_photo),
backgroundColor: Color( 0xffBA680B),
backgroundColor: Color(0xffBA680B),
),
);
}

Widget enableUpload() {
return ListView(children: <Widget>[
Container(child: Form(
key: formkey,
child: Column(
children: <Widget>[
Image.file(sampleImage, height: 330.0, width: 660.0),
SizedBox(
height: 15.0,
return ListView(
children: <Widget>[
Container(
child: Form(
key: formkey,
child: Column(
children: <Widget>[
Image.file(sampleImage, height: 330.0, width: 660.0),
SizedBox(
height: 15.0,
),
TextFormField(
decoration:
InputDecoration(labelText: 'Description About You'),
validator: (value) {
return value.isEmpty ? 'Description is required' : null;
},
onSaved: (value) {
return _myvalue = value;
},
),
SizedBox(
height: 15.0,
),
RaisedButton(
onPressed: UploadStatusImage,
elevation: 10.0,
child: Text("Add New Post"),
textColor: Colors.white,
color: Color(0xffBA680B),
)
],
),
TextFormField(
decoration: InputDecoration(labelText: 'Description About You'),
validator: (value) {
return value.isEmpty ? 'Description is required' : null;
},
onSaved: (value) {
return _myvalue = value;
},
),
SizedBox(
height: 15.0,
),
RaisedButton(onPressed: validateAndsave,
elevation: 10.0,
child: Text("Add New Post"),
textColor: Colors.white,
color: Color(0xffBA680B),


)
],
),
),
),),
],

],
);

}
}
99 changes: 99 additions & 0 deletions miniproject/lib/post_upload/post_home.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:flutter/material.dart';
import 'Posts.dart';
import 'package:firebase_database/firebase_database.dart';
class PostHome extends StatefulWidget{
@override
State<StatefulWidget> createState() {

return PostHome_State();
}

}
class PostHome_State extends State<PostHome>{
List<Posts> postList=[];
@override
void initState() {

super.initState();
DatabaseReference postsref=FirebaseDatabase.instance.reference().child("Posts");
postsref.once().then((DataSnapshot snap)
{
var KEYS=snap.value.keys;
var DATA=snap.value;
postList.clear();
for(var individualKey in KEYS){
Posts posts=Posts(
DATA[individualKey]['image'],
DATA[individualKey]['description'],
DATA[individualKey]['date'],
DATA[individualKey]['time']

);
postList.add(posts);
}
setState(() {
print('Length:$postList.length');
});

}
);
}

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(title: Text('Tour Guide'),
backgroundColor: Color(0xffBA680B),),
body: Container(
child: postList.length==0?Text('No Posts Available'):ListView.builder(
itemCount: postList.length,
itemBuilder: (_,index){
return PostsUI(postList[index].image,postList[index].description,postList[index].date,postList[index].time);
},
) ,
),
);
}

Widget PostsUI(String image,String description,String date,String time){
return Card(
elevation: 10.0,
margin: EdgeInsets.all(15.0),
child: Container(
padding: EdgeInsets.all(14.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
date,
style:Theme.of(context).textTheme.subtitle,
textAlign:TextAlign.center,
),

Text(
time,
style:Theme.of(context).textTheme.subtitle,
textAlign:TextAlign.center,
)
],
),
SizedBox(height:10.0),
Image.network(image,fit:BoxFit.cover),
SizedBox(height: 10.0,),
Text(
description,
style:Theme.of(context).textTheme.subhead,
textAlign:TextAlign.center,
)


],
),
),
);
}
}

0 comments on commit 4d5cbc9

Please sign in to comment.