Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/screens/ChatPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ChatPage extends StatefulWidget {

class _ChatPageState extends State<ChatPage> {
final ScrollController _scrollController = ScrollController();
DateTime lastSeen = DateTime.now().subtract(Duration(minutes: 5)); // Example last seen time

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -95,6 +97,20 @@ class _ChatPageState extends State<ChatPage> {
},
];

String getLastSeen() {
final now = DateTime.now();
final difference = now.difference(lastSeen);
if (difference.inMinutes < 1) {
return "Last seen just now";
} else if (difference.inMinutes < 60) {
return "Last seen ${difference.inMinutes} mins ago";
} else if (difference.inHours < 24) {
return "Last seen ${difference.inHours} hrs ago";
} else {
return "Last seen on ${lastSeen.day}/${lastSeen.month}/${lastSeen.year}";
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -107,7 +123,7 @@ class _ChatPageState extends State<ChatPage> {
backgroundImage: AssetImage(widget.image),
),
title: Text(widget.name),
subtitle: const Text("Online"),
subtitle: Text(getLastSeen()),
trailing: SizedBox(
width: MediaQuery.of(context).size.width * 0.2,
child: const Row(
Expand Down
149 changes: 92 additions & 57 deletions lib/screens/HomePage.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ignore_for_file: file_names

import 'package:flutter/material.dart';
import 'package:test_app/screens/ChatPage.dart';

import 'package:hello/screens/ChatPage.dart';
import 'package:hello/screens/SettingsPage.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});

Expand All @@ -11,6 +11,7 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
bool showLastSeen = true; // Privacy setting for last seen
List menuItems = [
"New Group",
"New Broadcast",
Expand All @@ -25,49 +26,63 @@ class _HomePageState extends State<HomePage> {
"Message": "I have reached Lanka",
"time": "11:11",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "10:30 AM",
"isOnline": false
},
{
"name": "Lakshman",
"Message": "Bhrata, mujhe aadesh dein, sabko mai akele maar dunga!",
"time": "11:11",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "11:00 AM",
"isOnline": true
},
{
"name": "Bharat",
"Message": "Kila fateh karke aaiye! Rajya aapka intezar kar raha hai!",
"time": "11:15",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "11:10 AM",
"isOnline": false
},
{
"name": "Shatrughna",
"Message": "ALl the best bhrata shree!",
"time": "11:12",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "11:00 AM",
"isOnline": true
},
{
"name": "Hanuman",
"Message": "Jai Shree Ram. On your command, my lord!",
"time": "11:11",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "11:10 AM",
"isOnline": true
},
{
"name": "Sita",
"Message": "I am waiting, Come soon!",
"time": "11:11",
"readStatus": true,
"sent": false
"sent": false,
"lastSeen": "11:00 AM",
"isOnline": false
},
{
"name": "Dashrath",
"Message": "All the best!",
"time": "11:20",
"readStatus": true,
"sent": true
"sent": true,
"lastSeen": "11:15 AM",
"isOnline": true
},
];

Expand All @@ -86,34 +101,46 @@ class _HomePageState extends State<HomePage> {
children: [
Icon(Icons.search),
PopupMenuButton<String>(
onSelected: (value) => {print(value)},
onSelected: (value) {
if (value == "Settings") {
Navigator.push(context, MaterialPageRoute(builder: (context) =>
SettingsPage(
showLastSeen: showLastSeen,
onShowLastSeenChanged: (value) {
setState(() {
showLastSeen = value;
});
},
),),);
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
PopupMenuItem(
value: "New Group",
child: Text("New Group"),
),
PopupMenuItem(
value: "New Broadcast",
child: Text("New Broadcast"),
),
PopupMenuItem(
value: "Linked devices",
child: Text("Linked devices"),
),
PopupMenuItem(
value: "Starred message",
child: Text("Starred message"),
),
PopupMenuItem(
value: "Payment",
child: Text("Payment"),
),
PopupMenuItem(
value: "Settings",
child: Text("Settings"),
),
])
<PopupMenuEntry<String>>[
PopupMenuItem(
value: "New Group",
child: Text("New Group"),
),
PopupMenuItem(
value: "New Broadcast",
child: Text("New Broadcast"),
),
PopupMenuItem(
value: "Linked devices",
child: Text("Linked devices"),
),
PopupMenuItem(
value: "Starred message",
child: Text("Starred message"),
),
PopupMenuItem(
value: "Payment",
child: Text("Payment"),
),
PopupMenuItem(
value: "Settings",
child: Text("Settings"),
),
]),
],
),
],
Expand All @@ -124,36 +151,44 @@ class _HomePageState extends State<HomePage> {
itemBuilder: (context, index) {
return ListTile(
title: Text(chats[index]["name"]),
subtitle: chats[index]["sent"]
? Row(
children: [
const Icon(Icons.done_all),
Text(
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (chats[index]["isOnline"])
Text("Online", style: TextStyle(color: Colors.green)),
if (!chats[index]["isOnline"] && showLastSeen)
Text("Last seen at ${chats[index]["lastSeen"]}"),
Row(
children: [
if (chats[index]["sent"]) Icon(Icons.done_all),
Expanded(
child: Text(
chats[index]["Message"],
style: const TextStyle(
overflow: TextOverflow.ellipsis),
style: TextStyle(overflow: TextOverflow.ellipsis),
),
],
)
: Text(
chats[index]["Message"],
style: const TextStyle(overflow: TextOverflow.ellipsis),
),
),
],
),
],
),
leading: ClipRRect(
borderRadius: BorderRadius.circular(40),
child: index % 2 == 0
? Image.asset("assets/srk.png")
: Image.asset("assets/srk1.png"),
? Image.asset("assets/images/img1.png")
: Image.asset("assets/images/img1.png"),
),
trailing: Text(chats[index]["time"]),
onTap: () => Navigator.push(context,
MaterialPageRoute(builder: (context) {
return ChatPage(
name: chats[index]["name"],
image:
index % 2 == 0 ? "assets/srk.png" : "assets/srk1.png",
);
})),
return ChatPage(
name: chats[index]["name"],
image:
index % 2 == 0 ? "assets/images/img1.png" : "assets/images/img2.png",



);
})),
);
}));
}
Expand Down
32 changes: 32 additions & 0 deletions lib/screens/SettingsPage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';

class SettingsPage extends StatelessWidget {
final bool showLastSeen;
final ValueChanged<bool> onShowLastSeenChanged;

SettingsPage({
required this.showLastSeen,
required this.onShowLastSeenChanged,
});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Settings'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
SwitchListTile(
title: Text('Show Last Seen'),
value: showLastSeen,
onChanged: onShowLastSeenChanged,
),
],
),
),
);
}
}