Skip to content

Commit

Permalink
Implemented getter to expose current url strategy for web plugins (fl…
Browse files Browse the repository at this point in the history
  • Loading branch information
The one with the braid (she/her) | Dфҿ mit dem Zopf (sie/ihr) authored Oct 13, 2021
1 parent e322012 commit 01afd64
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ Casey Rogers <[email protected]>
Pradumna Saraf <[email protected]>
Kai Yu <[email protected]>
Denis Grafov <[email protected]>
TheOneWithTheBraid <[email protected]>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:html' as html;
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
Expand All @@ -27,6 +28,9 @@ void main() {
app.main();
await tester.pumpAndSettle();

// checking whether the previously set strategy is properly preserved
expect(urlStrategy, strategy);

expect(strategy.getPath(), '/');

final NavigatorState navigator = app.navKey.currentState!;
Expand All @@ -44,7 +48,8 @@ void main() {
class TestUrlStrategy extends UrlStrategy {
/// Creates a instance of [TestUrlStrategy] with an empty string as the
/// path.
factory TestUrlStrategy() => TestUrlStrategy.fromEntry(const TestHistoryEntry(null, null, ''));
factory TestUrlStrategy() =>
TestUrlStrategy.fromEntry(const TestHistoryEntry(null, null, ''));

/// Creates an instance of [TestUrlStrategy] and populates it with a list
/// that has [initialEntry] as the only item.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef _JsSetUrlStrategy = void Function(JsUrlStrategy?);
/// A JavaScript hook to customize the URL strategy of a Flutter app.
//
// Keep this in sync with the JS name in the web engine. Find it at:
// https://github.com/flutter/engine/blob/custom_location_strategy/lib/web_ui/lib/src/engine/navigation/js_url_strategy.dart
// https://github.com/flutter/engine/blob/master/lib/web_ui/lib/src/engine/navigation/js_url_strategy.dart
//
// TODO(mdebbar): Add integration test https://github.com/flutter/flutter/issues/66852
@JS('_flutter_web_set_location_strategy')
Expand All @@ -37,8 +37,7 @@ typedef _AddPopStateListener = ui.VoidCallback Function(html.EventListener);

typedef _StringToString = String Function(String);

typedef _StateOperation = void Function(
Object state, String title, String url);
typedef _StateOperation = void Function(Object state, String title, String url);

typedef _HistoryMove = Future<void> Function(int count);

Expand Down
21 changes: 21 additions & 0 deletions packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ import 'dart:ui' as ui;
import 'js_url_strategy.dart';
import 'utils.dart';

/// Saves the current [UrlStrategy] to be accessed by [urlStrategy] or
/// [setUrlStrategy].
///
/// This is particularly required for web plugins relying on valid URL
/// encoding.
//
// Keep this in sync with the default url strategy in the web engine.
// Find it at:
// https://github.com/flutter/engine/blob/master/lib/web_ui/lib/src/engine/window.dart#L360
//
UrlStrategy? _urlStrategy = const HashUrlStrategy();

/// Returns the present [UrlStrategy] for handling the browser URL.
///
/// In case null is returned, the browser integration has been manually
/// disabled by [setUrlStrategy].
UrlStrategy? get urlStrategy => _urlStrategy;

/// Change the strategy to use for handling browser URL.
///
/// Setting this to null disables all integration with the browser history.
void setUrlStrategy(UrlStrategy? strategy) {
_urlStrategy = strategy;

JsUrlStrategy? jsUrlStrategy;
if (strategy != null) {
jsUrlStrategy = convertToJsUrlStrategy(strategy);
Expand Down Expand Up @@ -285,6 +305,7 @@ class BrowserPlatformLocation extends PlatformLocation {
static const String _defaultSearch = '';

html.Location get _location => html.window.location;

html.History get _history => html.window.history;

@override
Expand Down

0 comments on commit 01afd64

Please sign in to comment.