diff --git a/lib/screens/ChatPage.dart b/lib/screens/ChatPage.dart index aa83c89..f08bb66 100644 --- a/lib/screens/ChatPage.dart +++ b/lib/screens/ChatPage.dart @@ -12,6 +12,8 @@ class ChatPage extends StatefulWidget { class _ChatPageState extends State { final ScrollController _scrollController = ScrollController(); + DateTime lastSeen = DateTime.now().subtract(Duration(minutes: 5)); // Example last seen time + @override void initState() { super.initState(); @@ -95,6 +97,20 @@ class _ChatPageState extends State { }, ]; + 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( @@ -107,7 +123,7 @@ class _ChatPageState extends State { 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( diff --git a/lib/screens/HomePage.dart b/lib/screens/HomePage.dart index 5266eb5..c9961da 100644 --- a/lib/screens/HomePage.dart +++ b/lib/screens/HomePage.dart @@ -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}); @@ -11,6 +11,7 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { + bool showLastSeen = true; // Privacy setting for last seen List menuItems = [ "New Group", "New Broadcast", @@ -25,49 +26,63 @@ class _HomePageState extends State { "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 }, ]; @@ -86,34 +101,46 @@ class _HomePageState extends State { children: [ Icon(Icons.search), PopupMenuButton( - 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) => - >[ - 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"), - ), - ]) + >[ + 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"), + ), + ]), ], ), ], @@ -124,36 +151,44 @@ class _HomePageState extends State { 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", + + + + ); + })), ); })); } diff --git a/lib/screens/SettingsPage.dart b/lib/screens/SettingsPage.dart new file mode 100644 index 0000000..bc53cdb --- /dev/null +++ b/lib/screens/SettingsPage.dart @@ -0,0 +1,32 @@ +import 'package:flutter/material.dart'; + +class SettingsPage extends StatelessWidget { + final bool showLastSeen; + final ValueChanged 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, + ), + ], + ), + ), + ); + } +}