Flutter中国农历日历插件chinese_lunar_calendar的使用

发布于 1周前 作者 ionicwang 来自 Flutter

Flutter中国农历日历插件chinese_lunar_calendar的使用

Chinese Lunar Calendar

中国万年历

本项目主要参考 cnLunar,用Dart重写成一个Dart Package。

计算时间范围为1900年至2100年。

本地运行,无远程API依赖。

Dart CI License Pub Package publisher

功能

  • 生成农历日期、干支纪年、生肖、春节、节气、时辰、八字等。
  • 字符串支持简体中文与繁体中文。

安装

Run this command:

With Dart:

$ dart pub add chinese_lunar_calendar

With Flutter:

$ flutter pub add chinese_lunar_calendar

示例

以下是一个完整的示例代码,展示了如何使用 chinese_lunar_calendar 插件来获取农历日期、生肖、节气等信息:

import 'package:chinese_lunar_calendar/chinese_lunar_calendar.dart';

void main() {
  final lunarCalendar = LunarCalendar.from(
    utcDateTime: DateTime(2024, 2, 6, 22, 30).toUtc(),
  );

  print('中国标准时间:${lunarCalendar.cst}');
  print('本地时间:${lunarCalendar.localTime}');
  print('使用本地时间计算');
  print('春节: ${lunarCalendar.chineseNewYearString}');
  print('生肖: ${lunarCalendar.zodiac}');
  print(
      '阴历数字: ${lunarCalendar.lunarDate.lunarYear.number}, ${lunarCalendar.lunarDate.lunarMonth.number}, ${lunarCalendar.lunarDate.lunarDay}, 闰月:${lunarCalendar.lunarDate.lunarMonth.isLeapMonth}');
  print('阴历汉字: ${lunarCalendar.lunarDate.fullCNString}');
  print('八字: ${lunarCalendar.eightChar}');
  print('星期:${lunarCalendar.weekday.name}');
  print('月相:${lunarCalendar.moonPhase}');
  print('本年节气: ${getSolarTerms(lunarCalendar.localTime.year)}');
  print('本日节气:${lunarCalendar.localTime.getSolarTerm()}');
  print(
      '当前时辰:${lunarCalendar.twoHourPeriod.name}(${lunarCalendar.twoHourPeriod.jing?.cnName}${lunarCalendar.twoHourPeriod.jing?.unit})${lunarCalendar.ke.fullName}${lunarCalendar.ke.unitName}');

  final twoHourPeriodsString = lunarCalendar.twoHourPeriodList
      .map((e) => 
          '${e.twoHourPeriodIndex}: ${e.name} (${e.nameInHanDynasty}) ${e.meridian} ${e.isLuckyName}')
      .toList();
  print('本日时辰:$twoHourPeriodsString');
}

输出结果

以下是上述示例代码的输出结果:

中国标准时间:2024-02-07 14:30:00.000Z
本地时间:2024-02-06 22:30:00.000
使用本地时间计算
春节: 2024年2月10日
生肖: Zodiac(3, Located(兔, 兔))
阴历数字: 2023, 12, 27, 闰月:false
阴历汉字: 二零二三年 Located(, )Located(臘月, 腊月)大廿七
八字: 癸卯 丙寅 庚子 丁亥
星期:星期二
月相:MoonPhase(27, 7, Located(下蛾眉月, 下蛾眉月))
本年节气: [SolarTerm(name: Located(小寒, 小寒), index: 0, CST: 2024-01-06 04:49:00.000Z, UTC: 2024-01-05 20:49:00.000Z, Local: 2024-01-05 12:49:00.000), SolarTerm(name: Located(大寒, 大寒), index: 1, CST: 2024-01-20 22:07:00.000Z, UTC: 2024-01-20 14:07:00.000Z, Local: 2024-01-20 06:07:00.000), SolarTerm(name: Located(立春, 立春), index: 2, CST: 2024-02-04 16:27:00.000Z, UTC: 2024-02-04 08:27:00.000Z, Local: 2024-02-04 00:27:00.000), SolarTerm(name: Located(雨水, 雨水), index: 3, CST: 2024-02-19 12:13:00.000Z, UTC: 2024-02-19 04:13:00.000Z, Local: 2024-02-18 20:13:00.000), SolarTerm(name: Located(驚蟄, 惊蛰), index: 4, CST: 2024-03-05 10:23:00.000Z, UTC: 2024-03-05 02:23:00.000Z, Local: 2024-03-04 18:23:00.000), SolarTerm(name: Located(春分, 春分), index: 5, CST: 2024-03-20 11:06:00.000Z, UTC: 2024-03-20 03:06:00.000Z, Local: 2024-03-19 20:06:00.000), SolarTerm(name: Located(清明, 清明), index: 6, CST: 2024-04-04 15:02:00.000Z, UTC: 2024-04-04 07:02:00.000Z, Local: 2024-04-04 00:02:00.000), SolarTerm(name: Located(穀雨, 谷雨), index: 7, CST: 2024-04-19 22:00:00.000Z, UTC: 2024-04-19 14:00:00.000Z, Local: 2024-04-19 07:00:00.000), SolarTerm(name: Located(立夏, 立夏), index: 8, CST: 2024-05-05 08:10:00.000Z, UTC: 2024-05-05 00:10:00.000Z, Local: 2024-05-04 17:10:00.000), SolarTerm(name: Located(小滿, 小满), index: 9, CST: 2024-05-20 20:59:00.000Z, UTC: 2024-05-20 12:59:00.000Z, Local: 2024-05-20 05:59:00.000), SolarTerm(name: Located(芒種, 芒种), index: 10, CST: 2024-06-05 12:10:00.000Z, UTC: 2024-06-05 04:10:00.000Z, Local: 2024-06-04 21:10:00.000), SolarTerm(name: Located(夏至, 夏至), index: 11, CST: 2024-06-21 04:51:00.000Z, UTC: 2024-06-20 20:51:00.000Z, Local: 2024-06-20 13:51:00.000), SolarTerm(name: Located(小暑, 小暑), index: 12, CST: 2024-07-06 22:20:00.000Z, UTC: 2024-07-06 14:20:00.000Z, Local: 2024-07-06 07:20:00.000), SolarTerm(name: Located(大暑, 大暑), index: 13, CST: 2024-07-22 15:44:00.000Z, UTC: 2024-07-22 07:44:00.000Z, Local: 2024-07-22 00:44:00.000), SolarTerm(name: Located(立秋, 立秋), index: 14, CST: 2024-08-07 08:09:00.000Z, UTC: 2024-08-07 00:09:00.000Z, Local: 2024-08-06 17:09:00.000), SolarTerm(name: Located(處暑, 处暑), index: 15, CST: 2024-08-22 22:55:00.000Z, UTC: 2024-08-22 14:55:00.000Z, Local: 2024-08-22 07:55:00.000), SolarTerm(name: Located(白露, 白露), index: 16, CST: 2024-09-07 11:11:00.000Z, UTC: 2024-09-07 03:11:00.000Z, Local: 2024-09-06 20:11:00.000), SolarTerm(name: Located(秋分, 秋分), index: 17, CST: 2024-09-22 20:44:00.000Z, UTC: 2024-09-22 12:44:00.000Z, Local: 2024-09-22 05:44:00.000), SolarTerm(name: Located(寒露, 寒露), index: 18, CST: 2024-10-08 03:00:00.000Z, UTC: 2024-10-07 19:00:00.000Z, Local: 2024-10-07 12:00:00.000), SolarTerm(name: Located(霜降, 霜降), index: 19, CST: 2024-10-23 06:15:00.000Z, UTC: 2024-10-22 22:15:00.000Z, Local: 2024-10-22 15:15:00.000), SolarTerm(name: Located(立冬, 立冬), index: 20, CST: 2024-11-07 06:20:00.000Z, UTC: 2024-11-06 22:20:00.000Z, Local: 2024-11-06 14:20:00.000), SolarTerm(name: Located(小雪, 小雪), index: 21, CST: 2024-11-22 03:56:00.000Z, UTC: 2024-11-21 19:56:00.000Z, Local: 2024-11-21 11:56:00.000), SolarTerm(name: Located(大雪, 大雪), index: 22, CST: 2024-12-06 23:17:00.000Z, UTC: 2024-12-06 15:17:00.000Z, Local: 2024-12-06 07:17:00.000), SolarTerm(name: Located(冬至, 冬至), index: 23, CST: 2024-12-21 17:21:00.000Z, UTC: 2024-12-21 09:21:00.000Z, Local: 2024-12-21 01:21:00.000)]
本日节气:null
当前时辰:丁亥(二更)正三刻
本日时辰:[0: 丙子 (Located(夜半, 夜半)) Located(膽, 胆) Located(吉, 吉), 1: 丁丑 (Located(雞鳴, 鸡鸣)) Located(肝, 肝) Located(吉, 吉), 2: 戊寅 (Located(平旦, 平旦)) Located(肺, 肺) Located(兇, 凶), 3: 己卯 (Located(日出, 日出)) Located(大腸, 大肠) Located(吉, 吉), 4: 庚辰 (Located(食時, 食时)) Located(胃, 胃) Located(兇, 凶), 5: 辛巳 (Located(隅中, 隅中)) Located(脾, 脾) Located(兇, 凶), 6: 壬午 (Located(日中, 日中)) Located(心, 心) Located(吉, 吉), 7: 癸未 (Located(日昳, 日昳)) Located(小腸, 小肠) Located(兇, 凶), 8: 甲申 (Located(晡時, 晡时)) Located(膀胱, 膀胱) Located(吉, 吉), 9: 乙酉 (Located(日入, 日入)) Located(腎, 肾) Located(吉, 吉), 10: 丙戌 (Located(黃昏, 黄昏)) Located(心包, 心包) Located(兇, 凶), 11: 丁亥 (Located(人定, 人定)) Located(三焦, 三焦) Located(兇, 凶)]

通过以上示例,您可以了解如何在Flutter项目中使用 chinese_lunar_calendar 插件来获取农历日期及相关信息。希望这对您有所帮助!


更多关于Flutter中国农历日历插件chinese_lunar_calendar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter中国农历日历插件chinese_lunar_calendar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用chinese_lunar_calendar插件的示例代码。这个插件可以帮助你获取中国农历日期,并显示相关的农历信息。

首先,确保你已经在pubspec.yaml文件中添加了chinese_lunar_calendar依赖:

dependencies:
  flutter:
    sdk: flutter
  chinese_lunar_calendar: ^latest_version  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter应用中使用这个插件。以下是一个简单的示例,展示如何获取并显示当前日期的农历信息:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Chinese Lunar Calendar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Lunar? lunarDate;

  @override
  void initState() {
    super.initState();
    // 获取当前日期的农历信息
    final DateTime now = DateTime.now();
    lunarDate = Lunar.fromSolar(now.year, now.month, now.day);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chinese Lunar Calendar Demo'),
      ),
      body: Center(
        child: lunarDate != null
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'Lunar Date: ${lunarDate!.lunarYear}年${lunarDate!.lunarMonth}月${lunarDate!.lunarDay}日',
                    style: TextStyle(fontSize: 24),
                  ),
                  SizedBox(height: 20),
                  Text(
                    'Zodiac: ${lunarDate!.zodiac}',
                    style: TextStyle(fontSize: 18),
                  ),
                  SizedBox(height: 10),
                  Text(
                    'Festival: ${lunarDate!.festival ?? 'No Festival'}',
                    style: TextStyle(fontSize: 18),
                  ),
                ],
              )
            : CircularProgressIndicator(),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. pubspec.yaml文件中添加了chinese_lunar_calendar依赖。
  2. MyApp应用中,创建了一个主页面MyHomePage
  3. MyHomePageinitState方法中,使用Lunar.fromSolar方法获取当前日期的农历信息。
  4. build方法中,根据获取到的农历信息构建UI,显示农历日期、生肖以及节日信息。

注意:如果chinese_lunar_calendar插件的API有所变动,请参考最新的官方文档进行调整。

回到顶部