26Flutter 日期 和时间戳/格式化日期库/flutter异步/ 官方自带日期组件showDatePicker、时间组件showTimePicker以及国际化
/*
一、Flutter日期和时间戳
日期转换成时间戳
var now=newDateTime.now();
print(now.millisecondsSinceEpoch); //单位毫秒,13位时间戳。
时间戳转换成日期:
var now=new DateTime.now();
var a=now.millisecondsSinceEpoch;//时间戳
print(DateTime.fromMillisecondsSinceEpoch(a));
二、Flutter第三方库date_format的使用:
https://pub.dev/packages/date_format
三、调用flutter自带日期组件和时间组:
*/
pubspec.yaml
flutter_localizations:
sdk: flutter
main.dart
import 'package:flutter/material.dart'; import 'package:flutter_demo/pages/Search.dart'; import 'routes/Routes.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return MaterialApp( localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate ], supportedLocales: [ const Locale('zh','CH'), const Locale('en','US') ], debugShowCheckedModeBanner: false, //去掉debug图标: initialRoute: '/', onGenerateRoute: onGenerateRoute); } }
DatePicker.dart
import 'package:flutter/material.dart'; import 'package:date_format/date_format.dart'; class DatePickerDemo extends StatefulWidget { DatePickerDemo({Key key}) : super(key: key); _DatePickerDemoState createState() => _DatePickerDemoState(); } class _DatePickerDemoState extends State<DatePickerDemo> { var now = DateTime.now(); DateTime _nowDate = DateTime.now(); var _nowTime=TimeOfDay(hour: 12,minute: 20); // String time; _showDatePicker() async { // showDatePicker( // context: context, // initialDate: _nowDate, // firstDate: DateTime(1980), // lastDate: DateTime(2100) // ).then((result){ // print(result); // }); var result = await showDatePicker( context: context, initialDate: _nowDate, firstDate: DateTime(1980), lastDate: DateTime(2100), locale: Locale('zh') ); print(result); setState(() { this._nowDate = result; }); } _showTimePicker() async{ var result=await showTimePicker( context: context, initialTime:_nowTime ); setState(() { this._nowTime=result; }); } @override void initState() { // TODO: implement initState super.initState(); // print(778899); // // // print(now.millisecondsSinceEpoch); //1568786998995 // print(DateTime.fromMillisecondsSinceEpoch(1568786998995)); //2019-09-18 14:09:58.995 // print(formatDate(DateTime.now(),[yyyy,'年',mm,'月',dd])); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('DatePicker')), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Row( children: <Widget>[ InkWell( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text("${formatDate(_nowDate, [ yyyy, '年', mm, '月', dd, '日' ])}"), Icon(Icons.arrow_drop_down) ], ), onTap: _showDatePicker, ), InkWell( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text("${_nowTime.format(context)}"), Icon(Icons.arrow_drop_down) ], ), onTap: _showTimePicker, ) ], ) ], )); } }