-
Notifications
You must be signed in to change notification settings - Fork 0
rlatkddbs99 edited this page Mar 21, 2023
·
6 revisions
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../model/profile.dart';
class AuthController extends GetxController{
var idcontroller = TextEditingController(); //id받기
var pwController = TextEditingController(); //pw받기
final Rxn<User> user = Rxn<User>(); //Auth에서 만들어주는 User
final Rxn<Profile> profile = Rxn<Profile>(); //정보변경 위함
fetchProfile(String uId) async{ //개개인의 uId로 정보 변경
var res = await FirebaseFirestore.instance.collection('profile').doc(uId).get();
print(res.data());
var data = res.data()!;
profile(Profile(culture: data['culture'], job: data['job'], age: data['age'])); //원하는 정보로 변경
}
@override //유저상태 업데이트 되는 지 캐칭
onInit(){
super.onInit();
//유저상태 바뀌면 뭐할건지
FirebaseAuth.instance.userChanges().listen((value) {
//유저 값 갱신
user(value);
if(value != null){
fetchProfile(value.uid);
Get.toNamed("/main");
}else { //유저가 없는 상태, login으로 가
Get.toNamed("/login");
}
});
}
handleLoginButton() async{
var res = await FirebaseAuth.instance.signInWithEmailAndPassword( //이메일 비밀번호로 로그인
email: idcontroller.text, password: pwController.text);
}
}
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:project1/model/movie_message.dart';
import '../../controller/movie_controller.dart';
import '../widget/logo.dart';
import 'movieReply_page.dart';
class MoViePage extends GetView<MovieController> {
const MoViePage({super.key});
static const String route = '/movie';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0,
title: Text("movie"),
leading: InkWell(child: Icon(Icons.arrow_back),onTap:(){
Navigator.pop(context);
}),
actions: [
InkWell(child: Icon(Icons.edit),onTap: () {
Get.toNamed("/edit");
},)
],
),
backgroundColor: Color(0XffA5A9FF),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Logo(),
SizedBox(height: 50,),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 300,
height: 100,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage("assets/images/movie.jpeg"))
),
),
StreamBuilder<List<MovieMessage>>(
stream: streamMessages(),
builder: (context, asyncSnapshot) {
if(!asyncSnapshot.hasData){
return const Center(child: CircularProgressIndicator(),);
}else if(asyncSnapshot.hasError){
return const Center(child: CircularProgressIndicator(),);
}else {
List<MovieMessage> messages = asyncSnapshot.data!;
return SizedBox(
height: 400,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () {
Get.to(MovieReplyPage());
},
title: Text(messages[index].content),
subtitle: Text(messages[index].sendDate.toDate().toLocal().toString().substring(5,16)),
trailing: Icon(Icons.arrow_right),
);
})),
//controller.editWidget()
],
),
);
}
},
),
controller.editWidget(),
],
)
],
),
),
);
}
Stream<List<MovieMessage>> streamMessages(){
try{
final Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance.collection(
'chatrooms/XeXVNgcrPflF18on7Dg8/messages').orderBy('sendDate').snapshots();
return snapshots.map((querySnapshot){
List<MovieMessage> messages = [];
querySnapshot.docs.forEach((element) {
messages.add(
MovieMessage.fromMap(
id:element.id,
map:element.data() as Map<String, dynamic>
)
);
});
return messages;
});
}catch(ex){//오류 발생 처리
print("오류");
}
return Stream.error(Error);
}
}
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:project1/controller/movie_controller.dart';
import 'package:project1/model/movie_reply.dart';
import '../widget/logo.dart';
class MovieReplyPage extends StatefulWidget {
const MovieReplyPage({super.key});
@override
State<MovieReplyPage> createState() => _MovieReplyPageState();
}
class _MovieReplyPageState extends State<MovieReplyPage> {
TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0,
title: Text("movie"),
leading: InkWell(child: Icon(Icons.arrow_back),onTap:(){
Navigator.pop(context);
}),
actions: [
InkWell(child: Icon(Icons.edit),onTap: () {
Get.toNamed("/edit");
},)
],
),
backgroundColor: Color(0XffA5A9FF),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Logo(),
SizedBox(height: 50,),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
StreamBuilder<List<MovieReply>>(
stream: streamMessages(),
builder: (context, asyncSnapshot) {
if(!asyncSnapshot.hasData){
return const Center(child: CircularProgressIndicator(),);
}else if(asyncSnapshot.hasError){
return const Center(child: CircularProgressIndicator(),);
}else {
List<MovieReply> messages = asyncSnapshot.data!;
return Container(
height: 500,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(messages[index].content),
subtitle: Text(messages[index].sendDate.toDate().toLocal().toString().substring(5,16)),
);
})),
],
),
);
}
},
),
getInputWidget()
],
)
],
),
)
);
}
Widget getInputWidget() {
return Container(
height: 60,
width: double.infinity,
decoration: BoxDecoration(boxShadow: const [
BoxShadow(color: Colors.black12, offset: Offset(0, -2), blurRadius: 3)
], ),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 15,vertical: 8),
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: TextField(
controller: controller,
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 15),
labelText: "내용을 입력하세요..",
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Colors.blue,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Colors.black26,
width: 1.0,
),
),
),
),
),
SizedBox(width: 10,),
RawMaterialButton(
onPressed: _onPressedSendButton, //전송버튼을 누를때 동작시킬 메소드
constraints: BoxConstraints(
minWidth: 0,
minHeight: 0
),
elevation: 2,
shape: CircleBorder(),
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(Icons.send),
),
)
],
),
),
);
}
void _onPressedSendButton(){
try{
MovieReply messageModel = MovieReply(content: controller.text,sendDate: Timestamp.now());
FirebaseFirestore firestore = FirebaseFirestore.instance;
firestore.collection('chatrooms2/OvQK5w14xBvTVnPBasfA/messages').add(messageModel.toMap());
}catch(ex){
print("하");
}
}
Stream<List<MovieReply>> streamMessages(){
try{
final Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance.collection('chatrooms2/OvQK5w14xBvTVnPBasfA/messages').orderBy('sendDate').snapshots();
return snapshots.map((querySnapshot){
List<MovieReply> messages = [];
querySnapshot.docs.forEach((element) {
messages.add(
MovieReply.fromMap(
id:element.id,
map:element.data() as Map<String, dynamic>
)
);
});
return messages;
});
}catch(ex){//오류 발생 처리
print("오류");
}
return Stream.error(Error);
}
}