Prasath S
Prasath S

Reputation: 4376

How to set different country time(Timezone) to the analog clock using Timezone library in flutter?

I want to set a different country time to AnalogClock(https://pub.dev/packages/flutter_analog_clock) using the timezone library (https://pub.dev/packages/timezone) in flutter. I tried the below method but this did not work.

This the code i tried

@override
  void initState() {
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (Timer t) => setupWorldTime());
    createRandomDoubleNumber();


  }

  setupWorldTime() {
    final detroit = getLocation('America/Detroit');
    final us = getLocation('US/Pacific');
    final tokyo = getLocation('Asia/Tokyo');


    setState(() {
      nowDetroit = new TZDateTime.now(detroit);
      nowUs = new TZDateTime.now(us);
      nowTokyo = TZDateTime.now(tokyo);
     // print('${nowDetroit.hour}:${nowDetroit.minute}:${nowDetroit.second}');

    // fm= DateFormat.yMMMMEEEEd().format(nowDetroit);


     // print('us  $nowDetroit');
      print('he  ${DateTime.now()}');
      print('he  ${nowDetroit}');
    });

  }

below the widget

child: AnalogClock(
datetime: nowUs ,
textScaleFactor: 1.8,
showAllNumbers: true,
decoration: BoxDecoration(
border: Border.all(
width: 2.0,
color: Colors.black),
color: Colors.transparent,
shape: BoxShape.circle),
),
               

Upvotes: 1

Views: 816

Answers (1)

Tanmay Pandharipande
Tanmay Pandharipande

Reputation: 655

Its working in my case. Importing these 2 statements.

import 'package:timezone/data/latest.dart' as tz;

import 'package:timezone/standalone.dart' as tz1;

void main() {
 tz.initializeTimeZones();
 runApp(MyApp());
}
//Declaring "now" as state variable. I am ignoring that part here 
//and just focusing on the problem statement.
DateTime now;
@override
  Widget build(BuildContext context) {
    var detroit = tz1.getLocation('US/Pacific');
    now = tz1.TZDateTime.now(detroit);
    print(now);
    return Scaffold(
        appBar: AppBar(
          title: Text("Clock"),
        ),
        body: Container(
          child: FlutterAnalogClock(
            dateTime: now,
            dialPlateColor: Colors.white,
            hourHandColor: Colors.black,
            minuteHandColor: Colors.black,
            secondHandColor: Colors.black,
            numberColor: Colors.black,
            borderColor: Colors.black,
            tickColor: Colors.black,
            centerPointColor: Colors.black,
            showBorder: true,
            showTicks: true,
            showMinuteHand: true,
            showSecondHand: true,
            showNumber: true,
            borderWidth: 8.0,
            hourNumberScale: .10,
            hourNumbers: ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'],
            isLive: true,
            width: 200.0,
            height: 200.0,
            decoration: const BoxDecoration(),
            child: Text('Clock'),
          ),
        ));
  }

enter image description here

Upvotes: 3

Related Questions