Skip to content

Commit c1cd0fa

Browse files
committed
[Bug Fix] BottomSheet & Edit Button Issue.
1 parent e2307c4 commit c1cd0fa

File tree

4 files changed

+76
-33
lines changed

4 files changed

+76
-33
lines changed

lib/helper/my_date_util.dart

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:intl/intl.dart';
12
import 'package:flutter/material.dart';
23

34
class MyDateUtil {
@@ -8,22 +9,44 @@ class MyDateUtil {
89
return TimeOfDay.fromDateTime(date).format(context);
910
}
1011

12+
// // for getting formatted time for sent & read
13+
// static String getMessageTime(
14+
// {required BuildContext context, required String time}) {
15+
16+
// final DateTime sent = DateTime.fromMillisecondsSinceEpoch(int.parse(time));
17+
// final DateTime now = DateTime.now();
18+
19+
// final formattedTime = TimeOfDay.fromDateTime(sent).format(context);
20+
// if (now.day == sent.day &&
21+
// now.month == sent.month &&
22+
// now.year == sent.year) {
23+
// return formattedTime;
24+
// }
25+
26+
// return now.year == sent.year
27+
// ? '$formattedTime - ${sent.day} ${_getMonth(sent)}'
28+
// : '$formattedTime - ${sent.day} ${_getMonth(sent)} ${sent.year}';
29+
// }
30+
1131
// for getting formatted time for sent & read
12-
static String getMessageTime(
13-
{required BuildContext context, required String time}) {
32+
// [Bux Fix] Avoid bug due to context not mounted when keyboard is open in chat & bottom sheet opens
33+
static String getMessageTime({required String time}) {
1434
final DateTime sent = DateTime.fromMillisecondsSinceEpoch(int.parse(time));
1535
final DateTime now = DateTime.now();
1636

17-
final formattedTime = TimeOfDay.fromDateTime(sent).format(context);
37+
final String formattedTime = DateFormat('h:mm a').format(sent);
38+
1839
if (now.day == sent.day &&
1940
now.month == sent.month &&
2041
now.year == sent.year) {
2142
return formattedTime;
2243
}
2344

24-
return now.year == sent.year
25-
? '$formattedTime - ${sent.day} ${_getMonth(sent)}'
26-
: '$formattedTime - ${sent.day} ${_getMonth(sent)} ${sent.year}';
45+
final String formattedDate = now.year == sent.year
46+
? '${sent.day} ${_getMonth(sent)}'
47+
: '${sent.day} ${_getMonth(sent)} ${sent.year}';
48+
49+
return '$formattedTime - $formattedDate';
2750
}
2851

2952
//get last message time (used in chat user card)

lib/widgets/message_card.dart

+36-27
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,16 @@ class _MessageCardState extends State<MessageCard> {
192192
icon: const Icon(Icons.copy_all_rounded,
193193
color: Colors.blue, size: 26),
194194
name: 'Copy Text',
195-
onTap: () async {
195+
onTap: (ctx) async {
196196
await Clipboard.setData(
197197
ClipboardData(text: widget.message.msg))
198198
.then((value) {
199-
//for hiding bottom sheet
200-
Navigator.pop(context);
199+
if (ctx.mounted) {
200+
//for hiding bottom sheet
201+
Navigator.pop(ctx);
201202

202-
Dialogs.showSnackbar(context, 'Text Copied!');
203+
Dialogs.showSnackbar(ctx, 'Text Copied!');
204+
}
203205
});
204206
})
205207
:
@@ -208,17 +210,19 @@ class _MessageCardState extends State<MessageCard> {
208210
icon: const Icon(Icons.download_rounded,
209211
color: Colors.blue, size: 26),
210212
name: 'Save Image',
211-
onTap: () async {
213+
onTap: (ctx) async {
212214
try {
213215
log('Image Url: ${widget.message.msg}');
214216
await GallerySaver.saveImage(widget.message.msg,
215217
albumName: 'We Chat')
216218
.then((success) {
217-
//for hiding bottom sheet
218-
Navigator.pop(context);
219-
if (success != null && success) {
220-
Dialogs.showSnackbar(
221-
context, 'Image Successfully Saved!');
219+
if (ctx.mounted) {
220+
//for hiding bottom sheet
221+
Navigator.pop(ctx);
222+
if (success != null && success) {
223+
Dialogs.showSnackbar(
224+
ctx, 'Image Successfully Saved!');
225+
}
222226
}
223227
});
224228
} catch (e) {
@@ -239,11 +243,13 @@ class _MessageCardState extends State<MessageCard> {
239243
_OptionItem(
240244
icon: const Icon(Icons.edit, color: Colors.blue, size: 26),
241245
name: 'Edit Message',
242-
onTap: () {
243-
//for hiding bottom sheet
244-
Navigator.pop(context);
246+
onTap: (ctx) {
247+
if (ctx.mounted) {
248+
_showMessageUpdateDialog(ctx);
245249

246-
_showMessageUpdateDialog();
250+
//for hiding bottom sheet
251+
// Navigator.pop(ctx);
252+
}
247253
}),
248254

249255
//delete option
@@ -252,10 +258,10 @@ class _MessageCardState extends State<MessageCard> {
252258
icon: const Icon(Icons.delete_forever,
253259
color: Colors.red, size: 26),
254260
name: 'Delete Message',
255-
onTap: () async {
261+
onTap: (ctx) async {
256262
await APIs.deleteMessage(widget.message).then((value) {
257263
//for hiding bottom sheet
258-
Navigator.pop(context);
264+
if (ctx.mounted) Navigator.pop(ctx);
259265
});
260266
}),
261267

@@ -270,27 +276,27 @@ class _MessageCardState extends State<MessageCard> {
270276
_OptionItem(
271277
icon: const Icon(Icons.remove_red_eye, color: Colors.blue),
272278
name:
273-
'Sent At: ${MyDateUtil.getMessageTime(context: context, time: widget.message.sent)}',
274-
onTap: () {}),
279+
'Sent At: ${MyDateUtil.getMessageTime(time: widget.message.sent)}',
280+
onTap: (_) {}),
275281

276282
//read time
277283
_OptionItem(
278284
icon: const Icon(Icons.remove_red_eye, color: Colors.green),
279285
name: widget.message.read.isEmpty
280286
? 'Read At: Not seen yet'
281-
: 'Read At: ${MyDateUtil.getMessageTime(context: context, time: widget.message.read)}',
282-
onTap: () {}),
287+
: 'Read At: ${MyDateUtil.getMessageTime(time: widget.message.read)}',
288+
onTap: (_) {}),
283289
],
284290
);
285291
});
286292
}
287293

288294
//dialog for updating message content
289-
void _showMessageUpdateDialog() {
295+
void _showMessageUpdateDialog(final BuildContext ctx) {
290296
String updatedMsg = widget.message.msg;
291297

292298
showDialog(
293-
context: context,
299+
context: ctx,
294300
builder: (_) => AlertDialog(
295301
contentPadding: const EdgeInsets.only(
296302
left: 24, right: 24, top: 20, bottom: 10),
@@ -326,7 +332,7 @@ class _MessageCardState extends State<MessageCard> {
326332
MaterialButton(
327333
onPressed: () {
328334
//hide alert dialog
329-
Navigator.pop(context);
335+
Navigator.pop(ctx);
330336
},
331337
child: const Text(
332338
'Cancel',
@@ -336,9 +342,12 @@ class _MessageCardState extends State<MessageCard> {
336342
//update button
337343
MaterialButton(
338344
onPressed: () {
339-
//hide alert dialog
340-
Navigator.pop(context);
341345
APIs.updateMessage(widget.message, updatedMsg);
346+
//hide alert dialog
347+
Navigator.pop(ctx);
348+
349+
//for hiding bottom sheet
350+
Navigator.pop(ctx);
342351
},
343352
child: const Text(
344353
'Update',
@@ -353,15 +362,15 @@ class _MessageCardState extends State<MessageCard> {
353362
class _OptionItem extends StatelessWidget {
354363
final Icon icon;
355364
final String name;
356-
final VoidCallback onTap;
365+
final Function(BuildContext) onTap;
357366

358367
const _OptionItem(
359368
{required this.icon, required this.name, required this.onTap});
360369

361370
@override
362371
Widget build(BuildContext context) {
363372
return InkWell(
364-
onTap: () => onTap(),
373+
onTap: () => onTap(context),
365374
child: Padding(
366375
padding: EdgeInsets.only(
367376
left: mq.width * .05,

pubspec.lock

+8
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ packages:
504504
url: "https://pub.dev"
505505
source: hosted
506506
version: "0.2.1+1"
507+
intl:
508+
dependency: "direct main"
509+
description:
510+
name: intl
511+
sha256: "00f33b908655e606b86d2ade4710a231b802eec6f11e87e4ea3783fd72077a50"
512+
url: "https://pub.dev"
513+
source: hosted
514+
version: "0.20.1"
507515
leak_tracker:
508516
dependency: transitive
509517
description:

pubspec.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ dependencies:
5959
# Google Gemini Ai Integration
6060
google_generative_ai: ^0.4.3
6161

62+
# Date Time Formatting
63+
intl: ^0.20.1
64+
6265
dev_dependencies:
6366
flutter_test:
6467
sdk: flutter

0 commit comments

Comments
 (0)