From 6abc64a2d8731b0ec9c3438919c174aeee0df840 Mon Sep 17 00:00:00 2001 From: David Bennett Date: Wed, 12 May 2021 12:40:07 +0000 Subject: [PATCH] Add in web support to the plugin. --- .idea/vcs.xml | 1 - example/pubspec.yaml | 59 +-------------------------- lib/flutter_native_timezone_web.dart | 60 ++++++++++++++++++++++++++++ pubspec.yaml | 5 +++ 4 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 lib/flutter_native_timezone_web.dart diff --git a/.idea/vcs.xml b/.idea/vcs.xml index bb0d495..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,5 @@ - \ No newline at end of file diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 30f66cf..2d8aa90 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,24 +1,11 @@ name: flutter_native_timezone_example description: A new example flutter project. - -# The following line prevents the package from being accidentally published to -# pub.dev using `pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev -# The following defines the version and build number for your application. -# A version number is three numbers separated by dots, like 1.2.43 -# followed by an optional build number separated by a +. -# Both the version and the builder number may be overridden in flutter -# build by specifying --build-name and --build-number, respectively. -# In Android, build-name is used as versionName while build-number used as versionCode. -# Read more about Android versioning at https://developer.android.com/studio/publish/versioning -# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. -# Read more about iOS versioning at -# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html version: 1.0.0+1 environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: @@ -26,53 +13,9 @@ dependencies: flutter_native_timezone: path: .. - - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^1.0.2 - dev_dependencies: flutter_test: sdk: flutter -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter. flutter: - - # The following line ensures that the Material Icons font is - # included with your application, so that you can use the icons in - # the material Icons class. uses-material-design: true - - # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware. - - # For details regarding adding assets from package dependencies, see - # https://flutter.dev/assets-and-images/#from-packages - - # To add custom fonts to your application, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts from package dependencies, - # see https://flutter.dev/custom-fonts/#from-packages diff --git a/lib/flutter_native_timezone_web.dart b/lib/flutter_native_timezone_web.dart new file mode 100644 index 0000000..9f45ab4 --- /dev/null +++ b/lib/flutter_native_timezone_web.dart @@ -0,0 +1,60 @@ +import 'dart:async'; +import 'dart:html' as html; + +import 'package:flutter/services.dart'; +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; +import 'package:js/js.dart'; + +/// +/// The plugin class for the web, acts as the plugin inside bits +/// and connects to the js world. +/// +class FlutterNativeTimezonePlugin { + static void registerWith(Registrar registrar) { + final MethodChannel channel = MethodChannel( + 'flutter_native_timezone', + const StandardMethodCodec(), + registrar.messenger); + final FlutterNativeTimezonePlugin instance = FlutterNativeTimezonePlugin(); + channel.setMethodCallHandler(instance.handleMethodCall); + } + + Future handleMethodCall(MethodCall call) async { + switch (call.method) { + case 'getLocalTimezone': + return _getLocalTimeZone(); + case 'getAvailableTimezones': + return [ _getLocalTimeZone() ]; + default: + throw PlatformException( + code: 'Unimplemented', + details: "The flutter_native_timezone plugin for web doesn't implement " + "the method '${call.method}'"); + } + } + + /// Platform-specific implementation of determining the user's + /// local time zone when running on the web. + /// + String _getLocalTimeZone() { + return jsDateTimeFormat() + .resolvedOptions() + .timeZone; + } +} + +@JS('Intl.DateTimeFormat') +external _JSDateTimeFormat jsDateTimeFormat(); + +@JS() +abstract class _JSDateTimeFormat { + @JS() + external _JSResolvedOptions resolvedOptions(); +} + +@JS() +abstract class _JSResolvedOptions { + @JS() + external String get timeZone; +} + diff --git a/pubspec.yaml b/pubspec.yaml index 85d26a7..900614f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,6 +8,8 @@ environment: flutter: ">=1.12.0" dependencies: + flutter_web_plugins: + sdk: flutter flutter: sdk: flutter @@ -27,3 +29,6 @@ flutter: pluginClass: FlutterNativeTimezonePlugin macos: pluginClass: FlutterNativeTimezonePlugin + web: + pluginClass: FlutterNativeTimezonePlugin + fileName: flutter_native_timezone_web.dart