작성
·
298
3
Create_page.dart
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class CreatePage extends StatefulWidget {
final User user;
CreatePage(this.user);
@override
_CreatePageState createState() => _CreatePageState();
}
class _CreatePageState extends State<CreatePage> {
final textEditingController = TextEditingController();
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
File _image;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _buildAppbar(),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: _getImage,
child: Icon(Icons.add_a_photo),
),
);
}
Widget _buildAppbar() {
return AppBar(
actions: <Widget>[
IconButton(
icon: Icon(Icons.send),
onPressed: (){
final firebaseStorageRef = FirebaseStorage.instance
.ref()
.child('post')
.child('${DateTime
.now()
.millisecondsSinceEpoch}.png');
final task = firebaseStorageRef.putFile(
_image, SettableMetadata(contentType: 'image/png')
);
task.then((TaskSnapshot snapshot){
var downloadUrl=snapshot.ref.getDownloadURL();
downloadUrl.then((uri){
var doc = FirebaseFirestore.instance.collection('post').doc();
doc.set({
'id': doc.id,
'photoUrl': uri.toString(),
'contents': textEditingController.text,
'email':widget.user.email,
'displayName':widget.user.displayName,
'userPhotoUrl':widget.user.photoURL
});
});
}).then((value) => Navigator.pop(context));
},
)
],
);
}
Widget _buildBody() {
return SingleChildScrollView(
child: Column(
children: <Widget>[
_image == null ? Text('No Image') : Image.file(_image),
TextField(
decoration: InputDecoration(hintText: '내용을 입력하세요'),
controller: textEditingController,
)
],
),
);
}
final picker = ImagePicker();
Future _getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
//
// Future<void> _getImage() async {
// File image = await ImagePicker.pickImage(
// source: ImageSource.gallery
// );
// setState(() {
// _image = image;
// });
// }
}
=========/*pubspec.yaml*===========
cupertino_icons: ^1.0.1
image_picker: ^0.6.7+17
flutter_signin_button: ^0.2.5
google_sign_in: ^4.0.0
firebase_auth: ^0.18.4+1
firebase_core: ^0.5.3
cloud_firestore: ^0.14.4
firebase_storage: ^5.2.0
=====* 수정된 문법 *===
fireStore는 임포트 하는 방법은 같으나
호출방법은 기존
FireStore에서 FirebaseFireStore로 변경되었습니다.
상단 final FirebaseUser user
auth 사용법은
FirebaseUser user -> User user로 사용하시면 됩니다.
그 외에 document는 doc로 변경되었으며
이미지 피커같은 경우는 제 소스대로 사용하시면 됩니다.