|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:provider/provider.dart'; |
| 3 | +import 'package:task_management_app/features/chatbot/view/widgets/chat_header.dart'; |
| 4 | +import 'package:task_management_app/features/chatbot/view/widgets/day_separator.dart'; |
| 5 | +import 'package:task_management_app/features/chatbot/view/widgets/message_composer.dart'; |
| 6 | +import 'package:task_management_app/features/chatbot/view/widgets/message_tile.dart'; |
| 7 | +import 'package:task_management_app/features/chatbot/view/widgets/typing_indicator.dart'; |
| 8 | + |
| 9 | +import '../viewmodel/chatbot_viewmodel.dart'; |
| 10 | + |
| 11 | +class ChatBotView extends StatelessWidget { |
| 12 | + const ChatBotView({super.key, this.userAvatarUrl}); |
| 13 | + |
| 14 | + final String? userAvatarUrl; |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return ChangeNotifierProvider( |
| 19 | + create: (_) => ChatBotViewModel(), |
| 20 | + child: _ChatBotViewBody(userAvatarUrl: userAvatarUrl), |
| 21 | + ); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class _ChatBotViewBody extends StatefulWidget { |
| 26 | + const _ChatBotViewBody({this.userAvatarUrl}); |
| 27 | + |
| 28 | + final String? userAvatarUrl; |
| 29 | + |
| 30 | + @override |
| 31 | + State<_ChatBotViewBody> createState() => _ChatBotViewBodyState(); |
| 32 | +} |
| 33 | + |
| 34 | +class _ChatBotViewBodyState extends State<_ChatBotViewBody> { |
| 35 | + final TextEditingController _controller = TextEditingController(); |
| 36 | + final ScrollController _scrollController = ScrollController(); |
| 37 | + |
| 38 | + void _scrollToBottom() { |
| 39 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 40 | + if (_scrollController.hasClients) { |
| 41 | + _scrollController.animateTo( |
| 42 | + _scrollController.position.maxScrollExtent, |
| 43 | + duration: const Duration(milliseconds: 300), |
| 44 | + curve: Curves.easeOut, |
| 45 | + ); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + void dispose() { |
| 52 | + _controller.dispose(); |
| 53 | + _scrollController.dispose(); |
| 54 | + super.dispose(); |
| 55 | + } |
| 56 | + |
| 57 | + Future<void> _sendMessage(ChatBotViewModel viewModel) async { |
| 58 | + final text = _controller.text.trim(); |
| 59 | + if (text.isEmpty || viewModel.isLoading) return; |
| 60 | + |
| 61 | + _controller.clear(); |
| 62 | + _scrollToBottom(); |
| 63 | + |
| 64 | + await viewModel.sendMessage(text); |
| 65 | + if (!mounted) return; |
| 66 | + |
| 67 | + _scrollToBottom(); |
| 68 | + } |
| 69 | + |
| 70 | + @override |
| 71 | + Widget build(BuildContext context) { |
| 72 | + final theme = Theme.of(context); |
| 73 | + final scheme = theme.colorScheme; |
| 74 | + |
| 75 | + return Scaffold( |
| 76 | + backgroundColor: theme.scaffoldBackgroundColor, |
| 77 | + body: SafeArea( |
| 78 | + child: Column( |
| 79 | + children: [ |
| 80 | + const ChatHeader(), |
| 81 | + Divider(height: 1, color: scheme.outline.withValues(alpha: 0.4)), |
| 82 | + Expanded( |
| 83 | + child: Consumer<ChatBotViewModel>( |
| 84 | + builder: (context, viewModel, _) { |
| 85 | + return ListView.builder( |
| 86 | + controller: _scrollController, |
| 87 | + padding: const EdgeInsets.symmetric( |
| 88 | + horizontal: 16, |
| 89 | + vertical: 14, |
| 90 | + ), |
| 91 | + itemCount: viewModel.messages.length + 1, |
| 92 | + itemBuilder: (context, index) { |
| 93 | + if (index == 0) { |
| 94 | + return const Padding( |
| 95 | + padding: EdgeInsets.only(bottom: 16), |
| 96 | + child: DaySeparator(label: 'Today'), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + final message = viewModel.messages[index - 1]; |
| 101 | + return MessageTile( |
| 102 | + message: message, |
| 103 | + userAvatarUrl: widget.userAvatarUrl, |
| 104 | + ); |
| 105 | + }, |
| 106 | + ); |
| 107 | + }, |
| 108 | + ), |
| 109 | + ), |
| 110 | + Consumer<ChatBotViewModel>( |
| 111 | + builder: (context, viewModel, _) { |
| 112 | + if (!viewModel.isLoading) return const SizedBox.shrink(); |
| 113 | + return const Padding( |
| 114 | + padding: EdgeInsets.fromLTRB(16, 0, 16, 12), |
| 115 | + child: TypingIndicator(), |
| 116 | + ); |
| 117 | + }, |
| 118 | + ), |
| 119 | + Consumer<ChatBotViewModel>( |
| 120 | + builder: (context, viewModel, _) { |
| 121 | + return MessageComposer( |
| 122 | + controller: _controller, |
| 123 | + isSending: viewModel.isLoading, |
| 124 | + onSend: () => _sendMessage(viewModel), |
| 125 | + ); |
| 126 | + }, |
| 127 | + ), |
| 128 | + ], |
| 129 | + ), |
| 130 | + ), |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments