自带时间插件 - flutter

import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart' as cup;
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class GoodsInfo extends StatefulWidget {

  var arguments;

  GoodsInfo({this.arguments});

  @override
  _GoodsInfoState createState() => _GoodsInfoState(arguments:this.arguments);
}

class _GoodsInfoState extends State<GoodsInfo> {

  var arguments;
  bool checkBox;
  var radioGroup;
  var radioListTileGroup;
  var switchValue;
  var test = ["a", "b" , "c" ];
  TextEditingController _userNameController = TextEditingController(); //用于实现表单的默认值, TextField的controller参数需要接收这个类
  String _userName = ""; //如果用userNameController.text 记录onChange的值, 表单会被不停的重置, 光标一直跑到首位, 所以用单个_userName记录onChange的值, 不用setState, 因为不用刷新页面
  _GoodsInfoState({this.arguments});
  DateTime pickeredDate = DateTime.now();
  TimeOfDay pickerTime = TimeOfDay(hour: 0, minute: 0);
  // _showTime() {
  //   showDatePicker(
  //       context: context,
  //       initialDate: DateTime(1937, 5, 2),
  //       firstDate: DateTime(1900),
  //       lastDate: DateTime(2000)
  //   ).then((value) {
  //     this.pickeredDate = value;
  //     // print(this.pickeredDate);
  //   });
  //   print(formatDate(this.pickeredDate, ["yyyy","-","mm","-","dd"]));
  // }

  _showTime() async{ //日期插件
    DateTime result = await showDatePicker(
        context: context,
        initialDate: DateTime(2000),
        firstDate: DateTime(1300),
        lastDate: DateTime(2200),
        locale: cup.Locale('zh'), //设置此插件的语言
    );
    setState(() {
      this.pickeredDate = result;
    });
  }

  _showTimeSecond() async{  //时间插件
    TimeOfDay result = await showTimePicker(
        context: context,
        initialTime: TimeOfDay(hour: 22, minute: 33)
    );
    setState(() {
      this.pickerTime = result;
    });
  }

  @override
  void initState() { //初始化函数, 在构造函数之后执行
    // TODO: implement initState
    super.initState();
    this._userNameController.text = "表单的默认值";
    this.switchValue = true;
  }

  @override
  Widget build(BuildContext context) {
    print(DateTime.now()); // 获取当前时间 2020-12-08 23:16:18.8777033
    print(DateTime.now().millisecondsSinceEpoch); //获取上面格式化时间的时间戳
    print(DateTime.fromMillisecondsSinceEpoch(DateTime.now().millisecondsSinceEpoch)); // 格式化时间戳
    print(formatDate(DateTime.now(), ["yyyy","-","mm","-","dd", " ", "HH", ":", "nn", ":","ss"])); // 2020-12-09 0:20:26
    return Scaffold(
      appBar: AppBar(
        title: Text("GoodsInfo"),
      ),
      body: Center(
        child: Column(
          children: [
            Row(
              children: [
                InkWell(
                  child: Row(
                    children: [
                      Text(formatDate(this.pickeredDate, ["yyyy", "-", "mm", "-", "dd"]) ),
                      Icon(Icons.arrow_drop_down),
                    ],
                  ),
                  onTap: _showTime,
                ),
                InkWell(
                  child: Row(
                    children: [
                      Text(this.pickerTime.format(context))
                    ],
                  ),
                  onTap: _showTimeSecond,
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

name: flutter_app
description: A new Flutter application.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter



  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.0
  date_format: ^1.0.6
  flutter_cupertino_date_picker: ^1.0.26+2

dev_dependencies:
  flutter_test:
    sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg
  assets:
     - images/ali.jfif


  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/routes/Route.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [
        const Locale('zh', 'CH'),
        const Locale('en', 'US')
      ],

      debugShowCheckedModeBanner: false, //去掉右上角debug图标
      initialRoute: "/", //默认路由页面,不配置这项默认为 "/"
      // home:HomeContent(), //这是配置默认页面, 但是和routs里的 "/", 配置冲突, routes存在 "/" 即为默认家路由.
      theme: ThemeData(primarySwatch: Colors.deepOrange),
      // routes: routes,
        //当属性 routes: 里面不存在相应路由时才会调用 onGenerateRoute, 所以注释掉routes:, onGenerateRoute才会生效;
      onGenerateRoute: onGenerateRoute,
    );
    }
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值