pinkfish / flutter_native_timezone

Apache License 2.0
78 stars 156 forks source link

Support Web? #10

Closed jasonlaw closed 3 years ago

jasonlaw commented 4 years ago

Does this support for Web? I have tried to run this in Web but it always get stuck in getting local timezone.

pinkfish commented 4 years ago

No, this is only for flutter on android/ios.

On Thu, 6 Feb 2020 at 01:31, jasonlaw notifications@github.com wrote:

Does this support for Web? I have tried to run this in Web but it always get stuck in getting local timezone.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pinkfish/flutter_native_timezone/issues/10?email_source=notifications&email_token=AATOMMM6O7PUAVXXBP4MEJTRBPKFVA5CNFSM4KQZNOWKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ILONPFA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATOMMKAPDUIDLD2AK66LH3RBPKFVANCNFSM4KQZNOWA .

rinde commented 4 years ago

Any plans to add support for web?

scopendo commented 3 years ago

Support for either mobile or web isn't too hard – requires the use of the JS package when running on web.

/// File local_time_zone.dart
export 'local_time_zone_stub.dart'
  if (dart.library.io) 'local_time_zone_mobile.dart'
  if (dart.library.js) 'local_time_zone_web.dart';
/// File local_time_zone_stub.dart
/// This is a stub for getting the user's local time zone regardless of
/// whether we're running on mobile or on the web.
///
/// Callers should import [local_time_zone.dart] instead of this file.
///
String getLocalTimeZone() => throw UnimplementedError();
/// File local_time_zone_mobile.dart
import 'package:flutter_native_timezone/flutter_native_timezone.dart';

/// Platform-specific implementation of determining the user's
/// local time zone when running on mobile.
///
/// Uses the [flutter_native_timezone] package.
///
Future<String> getLocalTimeZone() async {
  return await FlutterNativeTimezone.getLocalTimezone();
}
/// File local_time_zone_web.dart
@JS()
library local_time_zone_web;

import 'package:js/js.dart';

/// 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;
}

An example use when running on either web or mobile:

import 'path/to/local_time_zone.dart';

final localTimeZone = await getLocalTimeZone();
print('The local time zone is $localTimeZone');
pinkfish commented 3 years ago

Added this support.