Skip to content

Commit

Permalink
Add in web support to the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkfish committed May 12, 2021
1 parent 816757c commit 6abc64a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 59 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 1 addition & 58 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,78 +1,21 @@
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:
sdk: flutter
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
60 changes: 60 additions & 0 deletions lib/flutter_native_timezone_web.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic> 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;
}

5 changes: 5 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ environment:
flutter: ">=1.12.0"

dependencies:
flutter_web_plugins:
sdk: flutter
flutter:
sdk: flutter

Expand All @@ -27,3 +29,6 @@ flutter:
pluginClass: FlutterNativeTimezonePlugin
macos:
pluginClass: FlutterNativeTimezonePlugin
web:
pluginClass: FlutterNativeTimezonePlugin
fileName: flutter_native_timezone_web.dart

0 comments on commit 6abc64a

Please sign in to comment.