Skip to content

Commit

Permalink
Merge branch 'feature_simple_chat' into 'master'
Browse files Browse the repository at this point in the history
Feature simple chat

See merge request domib97/notes_app2!4
  • Loading branch information
domib97 committed May 13, 2024
2 parents 2af7892 + 68bc2a6 commit 09a3622
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 77 deletions.
146 changes: 146 additions & 0 deletions lib/chat_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chats',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const ChatScreen(),
);
}
}

class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
const ChatScreen({super.key});
}

class _Message {
final String text;
final bool isMine;
final DateTime timestamp;

_Message({required this.text, required this.isMine, required this.timestamp});
}

class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _messageController = TextEditingController();
final List<_Message> _messages = [];

void _handleSendMessage() {
if (_messageController.text.isNotEmpty) {
setState(() {
_messages.add(_Message(
text: _messageController.text,
isMine: true,
timestamp: DateTime.now(),
));
_messageController.clear();
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Chats'),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: _messages.length,
itemBuilder: (context, index) {
final isMine = _messages[index].isMine;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 4.0),
child: Row(
mainAxisAlignment: isMine ? MainAxisAlignment.end : MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
if (!isMine)
const Padding(
padding: EdgeInsets.only(right: 8),
child: Text(
'Partner:',
style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
),
),
Column(
crossAxisAlignment: isMine ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: isMine ? Colors.blue : Colors.green,
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
child: Text(
_messages[index].text,
style: const TextStyle(color: Colors.white),
),
),
const SizedBox(height: 5),
Text(
'${_messages[index].timestamp.hour.toString().padLeft(2, '0')}:${_messages[index].timestamp.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 10, color: Colors.black),
),
],
),
if (isMine)
const Padding(
padding: EdgeInsets.only(left: 8),
child: Text(
'Me',
style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
),
),
],
),
);
},
),
),
const Divider(),
Container(
padding: const EdgeInsets.all(16),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
controller: _messageController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Type a message...',
),
),
),
const SizedBox(width: 8),
FloatingActionButton(
onPressed: _handleSendMessage,
tooltip: 'Send',
splashColor: Colors.green,
backgroundColor: Colors.green,
hoverColor: Colors.pink,
child: const Icon(Icons.send_rounded),
),
],
),
),
],
),
);
}
}
15 changes: 15 additions & 0 deletions lib/inbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';

class NotificationsPage extends StatelessWidget {
const NotificationsPage({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: const Text('Inbox', style: TextStyle(fontSize: 24)).animate().fade().scale().tint(color: Colors.pink),
),
);
}
}
39 changes: 22 additions & 17 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'note_page.dart';
import 'search_page.dart';
import 'notifications_page.dart';
import 'chat_page.dart';
import 'inbox.dart';
import 'settings_page.dart';

void main() {
runApp(MyApp());
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
int _selectedIndex = 0;
static List<Widget> _widgetOptions = [
NotePage(),
SearchPage(),
NotificationsPage(),
SettingsPage(),
static final List<Widget> _widgetOptions = [
const NotePage(),
const ChatScreen(),
const NotificationsPage(),
const SettingsPage(),
];

void _onItemTapped(int index) {
Expand All @@ -40,26 +43,28 @@ class _MyAppState extends State<MyApp> {
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
icon: FaIcon(FontAwesomeIcons.house),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
icon: FaIcon(FontAwesomeIcons.solidMessage),
label: 'Chats',
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notifications',
icon: FaIcon(FontAwesomeIcons.solidBell),
label: 'Inbox',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
icon: FaIcon(FontAwesomeIcons.gear),
label: 'Settings',
),
],
currentIndex: _selectedIndex,
unselectedItemColor: Colors.black,
selectedItemColor: Colors.pink,
onTap: _onItemTapped,
currentIndex: _selectedIndex,
selectedItemColor: Colors.green,
unselectedItemColor: Colors.grey,
unselectedFontSize: 9.0,
showUnselectedLabels: true,
),
),
debugShowCheckedModeBanner: false,
Expand Down
Loading

0 comments on commit 09a3622

Please sign in to comment.