// screens/main_screen.dart
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';
class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> with WindowListener {
int _selectedIndex = 0;
@override
void initState() {
super.initState();
windowManager.addListener(this);
_setupWindow();
}
Future<void> _setupWindow() async {
await windowManager.setMinimumSize(const Size(800, 600));
await windowManager.setTitle('LAN Chat');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
// Sidebar
Container(
width: 300,
color: Theme.of(context).colorScheme.surface,
child: Column(
children: [
// User profile section
_buildUserProfile(),
Divider(),
// Navigation
_buildNavigation(),
Divider(),
// Chat/Group list
Expanded(
child: _buildChatList(),
),
],
),
),
// Main content area
Expanded(
child: _selectedIndex == 0
? ChatScreen()
: SettingsScreen(),
),
],
),
);
}
Widget _buildUserProfile() {
return Container(
padding: EdgeInsets.all(16),
child: Row(
children: [
CircleAvatar(
radius: 24,
child: Text('U'),
),
SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Username', style: TextStyle(fontWeight: FontWeight.bold)),
Text('Online', style: TextStyle(color: Colors.green, fontSize: 12)),
],
),
),
],
),
);
}
Widget _buildNavigation() {
return Column(
children: [
ListTile(
leading: Icon(Icons.chat),
title: Text('Chats'),
selected: _selectedIndex == 0,
onTap: () => setState(() => _selectedIndex = 0),
),
ListTile(
leading: Icon(Icons.group),
title: Text('Groups'),
selected: _selectedIndex == 1,
onTap: () => setState(() => _selectedIndex = 1),
),
ListTile(
leading: Icon(Icons.people),
title: Text('Online Users'),
selected: _selectedIndex == 2,
onTap: () => setState(() => _selectedIndex = 2),
),
],
);
}
Widget _buildChatList() {
return ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
child: Text('U$index'),
),
title: Text('User $index'),
subtitle: Text('Last message...'),
trailing: Text('10:30 AM'),
onTap: () {
// Open chat
},
);
},
);
}
}
// screens/chat_screen.dart
import 'package:flutter/material.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:file_picker/file_picker.dart';
class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _messageController = TextEditingController();
final List<Message> _messages = [];
bool _isDragging = false;
@override
Widget build(BuildContext context) {
return DropTarget(
onDragDone: (detail) async {
for (final file in detail.files) {
await _sendFile(file.path);
}
},
onDragEntered: (detail) => setState(() => _isDragging = true),
onDragExited: (detail) => setState(() => _isDragging = false),
child: Container(
color: _isDragging ? Colors.blue.withOpacity(0.1) : null,
child: Column(
children: [
// Chat header
_buildHeader(),
// Messages area
Expanded(
child: _buildMessagesList(),
),
// Input area
_buildInputArea(),
],
),
),
);
}
Widget _buildHeader() {
return Container(
height: 60,
padding: EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border(
bottom: BorderSide(color: Theme.of(context).dividerColor),
),
),
child: Row(
children: [
CircleAvatar(child: Text('U')),
SizedBox(width: 12),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Username', style: TextStyle(fontWeight: FontWeight.bold)),
Text('Online', style: TextStyle(color: Colors.green, fontSize: 12)),
],
),
Spacer(),
IconButton(
icon: Icon(Icons.videocam),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.call),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.more_vert),
onPressed: () {},
),
],
),
);
}
Widget _buildMessagesList() {
return ListView.builder(
reverse: true,
padding: EdgeInsets.all(16),
itemCount: _messages.length,
itemBuilder: (context, index) {
final message = _messages[index];
final isMe = message.senderId == 'currentUserId';
return Align(
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
padding: EdgeInsets.all(12),
constraints: BoxConstraints(maxWidth: 300),
decoration: BoxDecoration(
color: isMe ? Theme.of(context).primaryColor : Colors.grey[300],
borderRadius: BorderRadius.circular(12),
),
child: message.type == MessageType.file
? _buildFileMessage(message)
: Text(
message.content,
style: TextStyle(color: isMe ? Colors.white : Colors.black),
),
),
);
},
);
}
Widget _buildFileMessage(Message message) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.attach_file),
SizedBox(width: 8),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(message.fileName ?? 'File'),
Text('${message.fileSize ?? 0} KB', style: TextStyle(fontSize: 12)),
],
),
),
],
);
}
Widget _buildInputArea() {
return Container(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border(
top: BorderSide(color: Theme.of(context).dividerColor),
),
),
child: Row(
children: [
IconButton(
icon: Icon(Icons.attach_file),
onPressed: _pickFile,
),
Expanded(
child: TextField(
controller: _messageController,
decoration: InputDecoration(
hintText: 'Type a message...',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
),
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
),
onSubmitted: _sendMessage,
),
),
SizedBox(width: 8),
IconButton(
icon: Icon(Icons.send),
onPressed: () => _sendMessage(_messageController.text),
),
],
),
);
}
Future<void> _pickFile() async {
final result = await FilePicker.platform.pickFiles();
if (result != null) {
await _sendFile(result.files.single.path!);
}
}
Future<void> _sendFile(String filePath) async {
// Implement file sending logic
}
void _sendMessage(String text) {
if (text.trim().isEmpty) return;
// Send message through WebSocket
_messageController.clear();
}
}