CarrotMarketApp - 채팅 화면 만들기

송민경's avatar
Apr 18, 2024
CarrotMarketApp - 채팅 화면 만들기

class ChatMessage { final String sender; final String profileImage; final String location; final String sendDate; final String message; final String? imageUri; ChatMessage({ required this.sender, required this.profileImage, required this.location, required this.sendDate, required this.message, this.imageUri, }); } // 샘플 데이터 List<ChatMessage> chatMessageList = [ ChatMessage( sender: '당근이, ', profileImage: 'https://picsum.photos/id/870/200/100?grayscale', // TODO 05장 수정 location: '대부동', sendDate: '1일전', message: 'developer 님,근처에 다양한 물품들이 아주 많이있습니다.', ), ChatMessage( sender: 'Flutter ', profileImage: 'https://picsum.photos/id/880/200/100?grayscale', // TODO 05장 수정 location: '중동', sendDate: '2일전', message: '안녕하세요 지금 다 예약 상태 인가요?', imageUri: 'https://picsum.photos/id/890/200/100?grayscale', // TODO 05장 수정 ) ];
import 'package:flutter/material.dart'; PreferredSize appBarBottomLine() { var height = 0.5; return PreferredSize( preferredSize: Size.fromHeight(height), child: Divider( thickness: height, height: height, color: Colors.grey, ), ); }
import 'package:flutter/material.dart'; class ImageContainer extends StatelessWidget { final double borderRadius; final String imageUrl; final double width; final double height; const ImageContainer({ Key? key, required this.borderRadius, required this.imageUrl, required this.width, required this.height, }) : super(key: key); @override Widget build(BuildContext context) { return ClipRRect( borderRadius: BorderRadius.circular(borderRadius), child: Image.network( imageUrl, width: width, height: height, fit: BoxFit.cover, ), ); } }
import 'package:carrot_market_ui/models/chat_message.dart'; import 'package:carrot_market_ui/screens/components/appbar_preffered_size.dart'; import 'package:flutter/material.dart'; class ChattingScreen extends StatelessWidget { const ChattingScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("채팅"), bottom: appBarBottomLine(), ), body: ListView( children: List.generate( chatMessageList.length, (index) => Container(), ), ), ); } }
notion image
import 'package:flutter/material.dart'; import '../../../models/chat_message.dart'; import '../../../theme.dart'; import '../components/image_container.dart'; class ChatContainer extends StatelessWidget { const ChatContainer({ Key? key, required this.chatMessage, }) : super(key: key); final ChatMessage chatMessage; @override Widget build(BuildContext context) { return Container( decoration: const BoxDecoration( border: Border(bottom: BorderSide(color: Colors.grey, width: 0.5)), ), height: 100, child: Padding( padding: const EdgeInsets.all(20), child: Row( children: [ ImageContainer( width: 50, height: 50, borderRadius: 25, imageUrl: chatMessage.profileImage, ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Spacer(), Text.rich( TextSpan(children: [ TextSpan(text: chatMessage.sender, style: textTheme().bodyLarge), // :TODO 05 수정 TextSpan(text: chatMessage.location), TextSpan(text: ' • ${chatMessage.sendDate}'), ]), ), const Spacer(), Text( chatMessage.message, style: textTheme().bodyLarge, // :TODO 05 수정 overflow: TextOverflow.ellipsis, ), const Spacer(), ], ), ), Visibility( visible: chatMessage.imageUri != null, child: Padding( padding: const EdgeInsets.only(left: 8.0), child: ClipRRect( borderRadius: BorderRadius.circular(10), child: ImageContainer( width: 50, height: 50, borderRadius: 8, imageUrl: chatMessage.imageUri ?? '', ), ), ), ) ], ), ), ); } }
import 'package:carrot_market_ui/models/chat_message.dart'; import 'package:carrot_market_ui/screens/chatting/chat_container.dart'; import 'package:carrot_market_ui/screens/components/appbar_preffered_size.dart'; import 'package:flutter/material.dart'; class ChattingScreen extends StatelessWidget { const ChattingScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("채팅"), bottom: appBarBottomLine(), ), body: ListView( children: List.generate( chatMessageList.length, (index) => ChatContainer(chatMessage: chatMessageList[index]), ), ), ); } }
notion image
Share article

vosw1