{"id":595,"date":"2023-02-18T19:44:53","date_gmt":"2023-02-18T11:44:53","guid":{"rendered":"http:\/\/gjweb.top\/?p=595"},"modified":"2023-08-02T23:22:37","modified_gmt":"2023-08-02T15:22:37","slug":"19-flutter-%e6%97%b6%e9%97%b4%e5%92%8c%e5%9b%bd%e9%99%85%e5%8c%96","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=595","title":{"rendered":"19. Flutter \u65f6\u95f4\u548c\u56fd\u9645\u5316"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"flutter-\u83b7\u53d6\u65e5\u671f\">Flutter \u83b7\u53d6\u65e5\u671f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d = DeteTime.now();\nd.yaear \/*\u5e74*\/\nd.month \/*\u6708*\/\nd.day \/*\u65e5*\/\nd.hour \/*\u65f6*\/\nd.minuter \/*\u5206*\/\nd.second \/*\u79d2*\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u83b7\u53d6\u65f6\u95f4\u6233\">\u83b7\u53d6\u65f6\u95f4\u6233<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d = DateTime.now();<br>d.millisecondsSinceEpoch<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u65e5\u671f\u8f6c\u4e4b\u95f4\u6233\">\u65e5\u671f\u8f6c\u4e4b\u95f4\u6233<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d=DateTime(2023,08,1);<br>print(d.millisecondsSinceEpoch);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u63d0\u793a\">\u63d0\u793a<\/h3>\n\n\n\n<p>\u5728 Dart \u4e2d\u8f6c\u6362\u65f6\u95f4\u6233\u7cfb\u7edf\u8f6c\u6362\u7684\u65f6\u6beb\u79d2\u7ea7\uff0c\u90e8\u5206 SDK \u7684\u65f6\u95f4\u6233\u4e3a \u79d2\u7ea7\u9700\u8981\u505a\u4e0b\u8ba1\u7b97<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d=DateTime(2023,08,1);<br>print(d.millisecondsSinceEpoch ~\/ 1000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5b57\u7b26\u4e32\u8f6c\u65f6\u95f4\">\u5b57\u7b26\u4e32\u8f6c\u65f6\u95f4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d=DateTime.parse(\"2021-08-01 08:01:30\");<br>print(d.millisecondsSinceEpoch);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u65f6\u95f4\u6233\u8f6c\u65e5\u671f\">\u65f6\u95f4\u6233\u8f6c\u65e5\u671f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime d = DateTime(2023, 08, 1);<br>int unixtime = d.millisecondsSinceEpoch;<br>print(DateTime.fromMillisecondsSinceEpoch(unixtime));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u65f6\u95f4\u52a0\u51cf\">\u65f6\u95f4\u52a0\u51cf<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime time = DateTime.now();<br>print(time);<br>print(time.add(const Duration(minutes: 30)));<br>print(time.add(const Duration(minutes: -30)))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5f39\u51fa\u65e5\u671f\u9009\u62e9\u5668\">\u5f39\u51fa\u65e5\u671f\u9009\u62e9\u5668<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime? time = await showDatePicker(\n    context:context,\n \u00a0 \u00a0initialDate:\u5f53\u524d\u65f6\u95f4,\n \u00a0 \u00a0firsDate:DateTime(1990), \/*\u6700\u65e9\u65f6\u671f*\/\n \u00a0 \u00a0lastData:DateTime(2060), \/* \u6700\u665a\u65f6\u671f*\/\n)\nif(time != null){\n \u00a0 \u00a0print(time)\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5f39\u51fa\u65f6\u95f4\u9009\u62e9\u5668\">\u5f39\u51fa\u65f6\u95f4\u9009\u62e9\u5668<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>TimeOfDay? time = await showTimePicker(\n    context:context,\n \u00a0 \u00a0initialTime:_time, \/* \u5f53\u524d\u65f6\u95f4 *\/\n)\nif(time != null){\n \u00a0 \u00a0print(time)\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u65f6\u95f4\u6233\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u65e5\u671f\">\u65f6\u95f4\u6233\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u65e5\u671f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5b89\u88c5\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>intl: ^0.18.1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4f7f\u7528\">\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000); \/*\u5c06\u79d2\u8f6c\u6362\u4e3a\u6beb\u79d2 *\/\nDateTime localDateTime = dateTime.toLocal();\nString formattedDate = DateFormat('yyyy-MM-dd').format(localDateTime);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u56fd\u9645\u5316-flutter-localizations\">\u56fd\u9645\u5316 flutter_localizations<\/h2>\n\n\n\n<p>\u7b2c\u4e09\u65b9\u7ec4\u4ef6<\/p>\n\n\n\n<p>\u914d\u7f6e pubspec.yaml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies:<br>    flutter:<br>    \tsdk: flutter<br>    flutter_localizations:<br>    \tsdk: flutter<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e\u6839\u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter_localizations\/flutter_localizations.dart';\nMaterialApp(\n\tlocalizationsDelegates: const &#91;\n    \t\/*\u6b64\u5904*\/\n    \tGlobalMaterialLocalizations.delegate,\n    \tGlobalWidgetsLocalizations.delegate,\n    ],\n    supportedLocales: const&#91;\n    \t\/*\u6b64\u5904*\/\n   \t \tLocale('zh', 'CH'),\n    \tLocale('en', 'US'),\n\t],\n)<\/code><\/pre>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>showDatePicker() async{<br>    var date =await showDatePicker(<br>    \tcontext: context,<br>    \tinitialDate: _datetime,<br>    \tfirstDate:DateTime(1900),<br>    \tlastDate:DateTime(2050),<br>    \tlocale: Locale('zh'),<br>    );<br>    if(date==null) return;<br>    print(date);<br>    setState(() {<br>    \t_datetime=date;<br>    });<br>}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flutter \u83b7\u53d6\u65e5\u671f \u83b7\u53d6\u65f6\u95f4\u6233 \u65e5\u671f\u8f6c\u4e4b\u95f4\u6233 \u63d0\u793a \u5728 Dart \u4e2d\u8f6c\u6362\u65f6\u95f4\u6233\u7cfb\u7edf\u8f6c\u6362\u7684\u65f6\u6beb\u79d2\u7ea7\uff0c\u90e8\u5206  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-595","post","type-post","status-publish","format-standard","hentry","category-flutter"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=595"}],"version-history":[{"count":3,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":802,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions\/802"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}