rup-project/app/lib/screens/home_screen.dart

323 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'pet_registration_screen.dart';
import '../services/firestore_service.dart';
import '../models/pet_model.dart';
import '../theme/app_colors.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final FirestoreService _firestoreService = FirestoreService();
String? _userId;
Pet? _selectedPet;
@override
void initState() {
super.initState();
_userId = _firestoreService.getCurrentUserId();
}
// 반려동물 선택 시 호출
void _selectPet(Pet pet) {
setState(() {
_selectedPet = pet;
});
Navigator.pop(context); // 모달 닫기
}
// 반려동물 선택 모달 표시
void _showPetSelectionModal(List<Pet> pets) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(20.r)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 20.h),
Text(
'반려동물 선택',
style: TextStyle(
fontFamily: 'SCDream',
fontSize: 18.sp,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 20.h),
// 반려동물 리스트
Flexible(
child: ListView.builder(
shrinkWrap: true,
itemCount: pets.length,
itemBuilder: (context, index) {
final pet = pets[index];
final isSelected = pet.id == _selectedPet?.id;
return ListTile(
leading: CircleAvatar(
radius: 24.r,
backgroundColor: Colors.grey[200],
backgroundImage: pet.profileImageUrl != null
? NetworkImage(pet.profileImageUrl!)
: null,
child: pet.profileImageUrl == null
? SvgPicture.asset(
'assets/icons/profile_icon.svg',
width: 24.w,
colorFilter: ColorFilter.mode(
Colors.grey[400]!,
BlendMode.srcIn,
),
)
: null,
),
title: Text(
pet.name,
style: TextStyle(
fontFamily: 'SCDream',
fontSize: 16.sp,
fontWeight: isSelected
? FontWeight.bold
: FontWeight.normal,
color: isSelected
? AppColors.highlight
: Colors.black,
),
),
trailing: isSelected
? const Icon(Icons.check, color: AppColors.highlight)
: null,
onTap: () => _selectPet(pet),
);
},
),
),
Divider(thickness: 1, color: Colors.grey[200]),
// 반려동물 추가 버튼
ListTile(
leading: Container(
width: 48.r,
height: 48.r,
decoration: BoxDecoration(
color: Colors.grey[100],
shape: BoxShape.circle,
),
child: const Icon(Icons.add, color: Colors.black54),
),
title: Text(
'반려동물 추가하기',
style: TextStyle(
fontFamily: 'SCDream',
fontSize: 16.sp,
fontWeight: FontWeight.w500,
),
),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const PetRegistrationScreen(),
),
);
},
),
SizedBox(height: 30.h),
],
),
);
},
);
}
@override
Widget build(BuildContext context) {
if (_userId == null) {
return const Scaffold(body: Center(child: Text('로그인이 필요합니다.')));
}
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: StreamBuilder<List<Pet>>(
stream: _firestoreService.getPets(_userId!),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('오류가 발생했습니다: ${snapshot.error}'));
}
final pets = snapshot.data ?? [];
// 등록된 반려동물이 없을 때: 기존 UI 유지 (등록 버튼 강조)
if (pets.isEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.symmetric(
horizontal: 20.w,
vertical: 20.h,
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: BorderRadius.circular(8.r),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const PetRegistrationScreen(),
),
);
},
child: Padding(
padding: EdgeInsets.symmetric(vertical: 4.h),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
'assets/img/profile.png',
width: 40.w,
height: 40.h,
),
SizedBox(width: 10.w),
Text(
'반려동물 등록 +',
style: TextStyle(
fontFamily: 'SCDream',
fontWeight: FontWeight.w500,
fontSize: 15.sp,
letterSpacing: 0.45.sp,
color: const Color(0xFF1f1f1f),
),
),
],
),
),
),
),
),
Expanded(
child: Center(
child: Text(
'등록된 반려동물이 없습니다.\n새로운 가족을 등록해주세요!',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'SCDream',
fontSize: 16.sp,
color: Colors.grey[600],
),
),
),
),
],
);
}
// 등록된 반려동물이 있을 때
// 선택된 펫이 없거나 리스트에 없으면 첫 번째 펫 선택
if (_selectedPet == null ||
!pets.any((p) => p.id == _selectedPet!.id)) {
// We shouldn't update state directly in build, but for initialization it's tricky.
// Using the first pet as default display.
// Better approach: use a local variable for display, update state in callbacks.
_selectedPet = pets.first;
}
// To ensure _selectedPet is valid (e.g. after deletion), find it in the new list
final displayPet = pets.firstWhere(
(p) => p.id == _selectedPet?.id,
orElse: () => pets.first,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.symmetric(
horizontal: 20.w,
vertical: 20.h,
),
child: GestureDetector(
onTap: () => _showPetSelectionModal(pets),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
// 프로필 이미지
CircleAvatar(
radius: 20.r,
backgroundColor: Colors.grey[200],
backgroundImage: displayPet.profileImageUrl != null
? NetworkImage(displayPet.profileImageUrl!)
: null,
child: displayPet.profileImageUrl == null
? SvgPicture.asset(
'assets/icons/profile_icon.svg',
width: 20.w,
colorFilter: ColorFilter.mode(
Colors.grey[400]!,
BlendMode.srcIn,
),
)
: null,
),
SizedBox(width: 10.w),
// 이름
Text(
displayPet.name,
style: TextStyle(
fontFamily: 'SCDream',
fontWeight: FontWeight.bold,
fontSize: 18.sp,
color: Colors.black,
),
),
SizedBox(width: 4.w),
// 드롭다운 화살표
Icon(
Icons.keyboard_arrow_down,
size: 24.w,
color: Colors.black,
),
],
),
),
),
Expanded(
child: Center(
child: Text(
'안녕, ${displayPet.name}!',
style: TextStyle(
fontFamily: 'SCDream',
fontSize: 24.sp,
fontWeight: FontWeight.bold,
),
),
),
),
],
);
},
),
),
);
}
}