1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'url_strategy.dart';
6
7/// Function type that handles pop state events.
8typedef EventListener = dynamic Function(Object event);
9
10/// Encapsulates all calls to DOM apis, which allows the [UrlStrategy] classes
11/// to be platform agnostic and testable.
12///
13/// For convenience, the [PlatformLocation] class can be used by implementations
14/// of [UrlStrategy] to interact with DOM apis like pushState, popState, etc.
15abstract interface class PlatformLocation {
16 /// Registers an event listener for the `popstate` event.
17 ///
18 /// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
19 void addPopStateListener(EventListener fn);
20
21 /// Unregisters the given listener (added by [addPopStateListener]) from the
22 /// `popstate` event.
23 ///
24 /// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
25 void removePopStateListener(EventListener fn);
26
27 /// The `pathname` part of the URL in the browser address bar.
28 ///
29 /// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname
30 String get pathname;
31
32 /// The `query` part of the URL in the browser address bar.
33 ///
34 /// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/search
35 String get search;
36
37 /// The `hash` part of the URL in the browser address bar.
38 ///
39 /// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/hash
40 String get hash;
41
42 /// The `state` in the current history entry.
43 ///
44 /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/state
45 Object? get state;
46
47 /// Adds a new entry to the browser history stack.
48 ///
49 /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
50 void pushState(Object? state, String title, String url);
51
52 /// Replaces the current entry in the browser history stack.
53 ///
54 /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
55 void replaceState(Object? state, String title, String url);
56
57 /// Moves forwards or backwards through the history stack.
58 ///
59 /// A negative [count] value causes a backward move in the history stack. And
60 /// a positive [count] value causes a forward move.
61 ///
62 /// Examples:
63 ///
64 /// * `go(-2)` moves back 2 steps in history.
65 /// * `go(3)` moves forward 3 steps in history.
66 ///
67 /// See: https://developer.mozilla.org/en-US/docs/Web/API/History/go
68 void go(int count);
69
70 /// The base href where the Flutter app is being served.
71 ///
72 /// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
73 String? getBaseHref();
74}
75
76/// Delegates to real browser APIs to provide platform location functionality.
77class BrowserPlatformLocation implements PlatformLocation {
78 @override
79 void addPopStateListener(EventListener fn) {
80 // No-op.
81 }
82
83 @override
84 void removePopStateListener(EventListener fn) {
85 // No-op.
86 }
87
88 @override
89 String get pathname => '';
90
91 @override
92 String get search => '';
93
94 @override
95 String get hash => '';
96
97 @override
98 Object? get state => null;
99
100 @override
101 void pushState(Object? state, String title, String url) {
102 // No-op.
103 }
104
105 @override
106 void replaceState(Object? state, String title, String url) {
107 // No-op.
108 }
109
110 @override
111 void go(int count) {
112 // No-op.
113 }
114
115 @override
116 String? getBaseHref() => null;
117}
118

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com