62 lines
1.7 KiB
Dart
62 lines
1.7 KiB
Dart
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class SCMobileLoginContext {
|
|
static const MethodChannel _channel = MethodChannel(
|
|
'com.org.yumiparty/mobile_context',
|
|
);
|
|
|
|
static Future<Map<String, dynamic>> loginBodyFields() async {
|
|
return <String, dynamic>{
|
|
'mobileLang': mobileLang,
|
|
'mobileZone': await mobileZone(),
|
|
};
|
|
}
|
|
|
|
static String get mobileLang {
|
|
final locales = ui.PlatformDispatcher.instance.locales;
|
|
final locale =
|
|
locales.isNotEmpty
|
|
? locales.first
|
|
: ui.PlatformDispatcher.instance.locale;
|
|
final languageTag = locale.toLanguageTag().trim();
|
|
if (languageTag.isNotEmpty) {
|
|
return languageTag;
|
|
}
|
|
return locale.languageCode.trim();
|
|
}
|
|
|
|
static Future<String> mobileZone() async {
|
|
try {
|
|
final zone = await _channel.invokeMethod<String>('getTimeZoneIdentifier');
|
|
final value = zone?.trim() ?? '';
|
|
if (value.isNotEmpty) {
|
|
return value;
|
|
}
|
|
} on PlatformException catch (e) {
|
|
debugPrint('get mobile timezone failed: ${e.code} ${e.message}');
|
|
} catch (e) {
|
|
debugPrint('get mobile timezone failed: $e');
|
|
}
|
|
|
|
return _fallbackZone();
|
|
}
|
|
|
|
static String _fallbackZone() {
|
|
final now = DateTime.now();
|
|
final zoneName = now.timeZoneName.trim();
|
|
if (zoneName.contains('/')) {
|
|
return zoneName;
|
|
}
|
|
|
|
final offset = now.timeZoneOffset;
|
|
final sign = offset.isNegative ? '-' : '+';
|
|
final absoluteOffset = offset.abs();
|
|
final hours = absoluteOffset.inHours.toString().padLeft(2, '0');
|
|
final minutes = (absoluteOffset.inMinutes % 60).toString().padLeft(2, '0');
|
|
return 'UTC$sign$hours:$minutes';
|
|
}
|
|
}
|