如何在 flutter 中使用时区库将不同的国家/地区时间(时区)设置为模拟时钟?
How to set different country time(Timezone) to the analog clock using Timezone library in flutter?
我想在 flutter 中为 AnalogClock(https://pub.dev/packages/flutter_analog_clock) using the timezone library (https://pub.dev/packages/timezone) 设置不同的国家/地区时间。我尝试了以下方法,但这没有用。
这是我试过的代码
@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),
),
它适用于我的情况。
导入这 2 个语句。
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'),
),
));
}
我想在 flutter 中为 AnalogClock(https://pub.dev/packages/flutter_analog_clock) using the timezone library (https://pub.dev/packages/timezone) 设置不同的国家/地区时间。我尝试了以下方法,但这没有用。
这是我试过的代码
@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),
),
它适用于我的情况。 导入这 2 个语句。
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'),
),
));
}