Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Created December 6, 2020 05:04
Show Gist options
  • Select an option

  • Save CoderJava/205d61c9589d19996ffb7122a0eda59b to your computer and use it in GitHub Desktop.

Select an option

Save CoderJava/205d61c9589d19996ffb7122a0eda59b to your computer and use it in GitHub Desktop.
MainActivity.kt sample time zone flutter
package com.example.sample_time_zone_flutter
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.util.*
class MainActivity: FlutterActivity(), MethodChannel.MethodCallHandler {
private val channel = "sample_time_zone_flutter_channel"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"get_native_time_zone" -> {
val timezone = TimeZone.getDefault()
val timezoneName = timezone.id
val now = Date()
val offsetFromUtc = timezone.getOffset(now.time) / 1000
val resultMap = mapOf("timezone" to timezoneName, "gmt_offset" to offsetFromUtc)
result.success(resultMap)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment