main.dart
import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart'; //第三方时间组件库
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
//标题栏
appBar: AppBar(
title: Text("Flutter Demo"),
),
//内容区域
body: DatePickerPubPage(),
),
//主题
theme: ThemeData(primarySwatch: Colors.red),
);
}
}
class DatePickerPubPage extends StatefulWidget {
@override
_DatePickerPubPageState createState() => _DatePickerPubPageState();
}
class _DatePickerPubPageState extends State<DatePickerPubPage> {
DateTime _selectedDateTime = DateTime.now(); //获取当前日期
void _showDateTimePicker() {
DatePicker.showDatePicker(
context,
minDateTime: DateTime.parse("2019-05-15 09:23:10"),
maxDateTime: DateTime.parse("2020-06-03 21:11:00"),
initialDateTime: DateTime.parse(formatDate(_selectedDateTime,
[yyyy, "-", mm, "-", "dd", " ", HH, ":", nn, ":", ss])),
dateFormat: "yy年M月d日 EEE,H时:m分",
locale: DateTimePickerLocale.zh_cn,
pickerTheme: DateTimePickerTheme(
showTitle: true,
confirm: Text('确定', style: TextStyle(color: Colors.red)),
cancel: Text('取消', style: TextStyle(color: Colors.red)),
),
pickerMode: DateTimePickerMode.datetime, // show TimePicker
onCancel: () {
debugPrint('onCancel');
},
// onChange: (dateTime, List<int> index) {
// setState(() {
// _selectedDateTime = dateTime;
// });
// },
onConfirm: (dateTime, List<int> index) {
setState(() {
_selectedDateTime = dateTime;
});
},
);
}
void _showDatePicker() {
DatePicker.showDatePicker(
context,
pickerTheme: DateTimePickerTheme(
showTitle: true,
confirm: Text('确定', style: TextStyle(color: Colors.red)),
cancel: Text('取消', style: TextStyle(color: Colors.red)),
),
minDateTime: DateTime.parse("2010-05-12"), //选择器上可选择的最早时间
maxDateTime: DateTime.parse("2021-11-25"), //选择器上可选择的最晚时间
initialDateTime: _selectedDateTime, //选择器的当前选中时间
dateFormat: "yyyy-MMMM-dd", //时间格式
locale: DateTimePickerLocale.zh_cn, //国际化配置
onClose: () => print("----- onClose -----"),
onCancel: () => print('onCancel'),
// onChange: (dateTime, List<int> index) {
// setState(() {
// _selectedDateTime = dateTime;
// });
// },
onConfirm: (dateTime, List<int> index) {
setState(() {
_selectedDateTime = dateTime;
});
},
);
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () {
_showDatePicker();
},
child: Row(
children: <Widget>[
Text(formatDate(_selectedDateTime, [yyyy, "-", mm, "-", dd])),
Icon(Icons.arrow_drop_down)
],
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () {
_showDateTimePicker();
},
child: Row(
children: <Widget>[
Text(formatDate(_selectedDateTime,
[yyyy, "-", mm, "-", dd, " ", HH, ":", nn])),
Icon(Icons.arrow_drop_down)
],
),
)
],
)
],
);
}
}
flutter_cupertino_date_picker: ^1.0.2
pubspec.yaml
name: flutter_app
description: A new Flutter application.
# 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.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
date_format: ^1.0.6
flutter_cupertino_date_picker: ^1.0.2
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
intl: ^0.15.8
# 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
assets:
- images/2.0x/demo.jpeg
- images/3.0x/demo.jpeg
- images/4.0x/demo.jpeg
- images/demo.jpeg
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# 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
显示效果: