time_zone_list 0.1.0 copy "time_zone_list: ^0.1.0" to clipboard
time_zone_list: ^0.1.0 copied to clipboard

outdated

Get time zone list from native side, include DST/GMT info

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:time_zone_list/time_zone_list.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<TimeZoneInfo> l = [];

  bool isSummer = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      var res = await TimeZoneList.getTimeZoneList(isSummer
          ? DateTime(2020, 5, 8, 8, 8, 8)
          : DateTime(2020, 12, 8, 8, 8, 8));
      // print(res);
      res.sort((a, b) => (a.offset - b.offset).inSeconds);
      l = res;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: GestureDetector(
            onTap: () {
              setState(() {
                isSummer = !isSummer;
              });
              initPlatformState();
            },
            child: Text(isSummer ? 'Summer' : 'Winter'),
          ),
        ),
        body: ListView.builder(
          padding: EdgeInsets.all(12),
          itemCount: l.length,
          itemBuilder: (context, index) => Container(
            padding: EdgeInsets.symmetric(
              vertical: 4,
              // horizontal: 12,
            ),
            child: Text(
              '$index:${l[index].toString()}',
              style: TextStyle(
                color:
                    l[index].dstOffset.inHours == 0 ? Colors.black : Colors.red,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
6
likes
0
points
121
downloads

Publisher

unverified uploader

Weekly Downloads

Get time zone list from native side, include DST/GMT info

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on time_zone_list

Packages that implement time_zone_list