Skip to content

Commit c8daac5

Browse files
committed
fix close dialog using close button leads to unexpected behavior in routing
- the problem was that the browser routing doesn't match with flutter go_router routing - so i create BrowsingRoutingHelper accessing browser's window.history through dart:html to manipulate borwser routing - the problem fixed that was when click on close button in image drawer in project details view , and after that click on browser back button it opens the dialog again
1 parent 3834471 commit c8daac5

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'dart:html' as html;
2+
3+
import 'package:flutter/foundation.dart' show kIsWeb;
4+
5+
class BrowserRoutingHelper {
6+
// Push a new state to browser history without navigation
7+
static void pushState(String url, {String? title, dynamic data}) {
8+
if (kIsWeb) {
9+
html.window.history.pushState(data, title ?? '', url);
10+
}
11+
}
12+
13+
// Replace current history state
14+
static void replaceState(String url, {String? title, dynamic data}) {
15+
if (kIsWeb) {
16+
html.window.history.replaceState(data, title ?? '', url);
17+
}
18+
}
19+
20+
// Go back in browser history
21+
static void back() {
22+
if (kIsWeb) {
23+
html.window.history.back();
24+
}
25+
}
26+
27+
// Go forward in browser history
28+
static void forward() {
29+
if (kIsWeb) {
30+
html.window.history.forward();
31+
}
32+
}
33+
34+
// Go to specific position in history (negative = back, positive = forward)
35+
static void go(int delta) {
36+
if (kIsWeb) {
37+
html.window.history.go(delta);
38+
}
39+
}
40+
41+
// Get current history length
42+
static int get historyLength {
43+
if (kIsWeb) {
44+
return html.window.history.length ?? 0;
45+
}
46+
return 0;
47+
}
48+
49+
// Listen to browser back/forward button
50+
static void onPopState(void Function(html.PopStateEvent) callback) {
51+
if (kIsWeb) {
52+
html.window.onPopState.listen(callback);
53+
}
54+
}
55+
56+
// Get current URL
57+
static String get currentUrl {
58+
if (kIsWeb) {
59+
return html.window.location.href;
60+
}
61+
return '';
62+
}
63+
64+
// Get current pathname
65+
static String get currentPath {
66+
if (kIsWeb) {
67+
return html.window.location.pathname ?? '/';
68+
}
69+
return '/';
70+
}
71+
}

lib/view/projects/components/project_media_widgets.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
3+
import 'package:flutter_portfolio/routing/browser_routing_helper.dart';
34
import 'hoverable_image_container.dart';
45
import 'package:photo_view/photo_view.dart';
56
import 'multi_video_player.dart';
@@ -81,8 +82,10 @@ class _ImageGalleryState extends State<ImageGallery> {
8182
return HoverableImageContainer(
8283
imageUrl: displayImages[index],
8384
onTap: () {
84-
final initialIndex = widget.images.indexOf(displayImages[index]);
85-
context.push('/project-details/${widget.projectId}/viewer/$initialIndex');
85+
final initialIndex =
86+
widget.images.indexOf(displayImages[index]);
87+
context.push(
88+
'/project-details/${widget.projectId}/viewer/$initialIndex');
8689
},
8790
height: 120,
8891
width: 120,
@@ -325,7 +328,7 @@ class _ImageGalleryDialogState extends State<ImageGalleryDialog> {
325328
color: Colors.white,
326329
size: 20,
327330
),
328-
onPressed: () => context.pop(),
331+
onPressed: () => BrowserRoutingHelper.back(),
329332
padding: EdgeInsets.zero,
330333
),
331334
),

0 commit comments

Comments
 (0)