• 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,
                    )
                  ],
                )
              ],
            ));
      }
    }
  • 相关阅读:
    python中scipy学习——随机稀疏矩阵及操作
    ptyhon中文本挖掘精简版
    ptyhon中文本挖掘精简版
    [python] 使用scikit-learn工具计算文本TF-IDF值
    [python] 使用scikit-learn工具计算文本TF-IDF值
    python高手的自修课
    python高手的自修课
    C/C++ scanf 函数中%s 和%c 的简单差别
    Ctags基本配置
    搭建gitserver
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/11542820.html
Copyright © 2020-2023  润新知