导入包flutter_datetime_picker: 1.5.0
封装
import 'package:atui/jade/utils/JadeColors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class ATuiDateTimePickerDialog {
static Future<DateTime> showDatePicker(
BuildContext context, {
bool showTitleActions: true,
bool isShowDay: true,
DateTime minTime,
DateTime maxTime,
DateChangedCallback onChanged,
DateChangedCallback onConfirm,
Function onCustomDateConfirm,
DateCancelledCallback onCancel,
locale: LocaleType.en,
DateTime currentTime,
DatePickerTheme theme,
}) async {
return await Navigator.push(
context,
_DatePickerRoute(
showTitleActions: showTitleActions,
onChanged: onChanged,
onConfirm: onConfirm,
onCustomDateConfirm: onCustomDateConfirm,
onCancel: onCancel,
locale: locale,
theme: theme,
isShowDay: isShowDay,
barrierLabel:
MaterialLocalizations.of(context).modalBarrierDismissLabel,
pickerModel: DatePickerModel(
currentTime: currentTime,
maxTime: maxTime,
minTime: minTime,
locale: locale,
),
),
);
}
}
class _DatePickerRoute<T> extends PopupRoute<T> {
_DatePickerRoute({
this.showTitleActions,
this.onChanged,
this.onConfirm,
this.onCustomDateConfirm,
this.onCancel,
theme,
this.barrierLabel,
this.locale,
this.isShowDay,
RouteSettings settings,
pickerModel,
}) : this.pickerModel = pickerModel ?? DatePickerModel(),
this.theme = theme ?? DatePickerTheme(),
super(settings: settings);
final bool showTitleActions;
final DateChangedCallback onChanged;
final DateChangedCallback onConfirm;
final Function onCustomDateConfirm;
final DateCancelledCallback onCancel;
final DatePickerTheme theme;
final LocaleType locale;
final BasePickerModel pickerModel;
final bool isShowDay;
Duration get transitionDuration => const Duration(milliseconds: 200);
bool get barrierDismissible => true;
final String barrierLabel;
Color get barrierColor => Colors.black54;
AnimationController _animationController;
AnimationController createAnimationController() {
assert(_animationController == null);
_animationController =
BottomSheet.createAnimationController(navigator.overlay);
return _animationController;
}
Widget buildPage(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
Widget bottomSheet = MediaQuery.removePadding(
context: context,
removeTop: true,
child: _DatePickerComponent(
onChanged: onChanged,
locale: this.locale,
route: this,
pickerModel: pickerModel,
isShowDay: isShowDay,
),
);
return InheritedTheme.captureAll(context, bottomSheet);
}
}
class _DatePickerComponent extends StatefulWidget {
_DatePickerComponent(
{
Key key,
this.route,
this.onChanged,
this.locale,
this.pickerModel,
this.isShowDay})
: super(key: key);
final DateChangedCallback onChanged;
final _DatePickerRoute route;
final LocaleType locale;
final BasePickerModel pickerModel;
bool isShowDay;
State<StatefulWidget> createState() {
return _DatePickerState();
}
}
class _DatePickerState extends State<_DatePickerComponent> {
FixedExtentScrollController leftScrollCtrl, middleScrollCtrl, rightScrollCtrl;
List<String> _btnTitleList = ['按日','按月','自定义'];
int _selectPosition = 0;
bool _isCustomTime = false;
String _startDate;
String _endDate = '结束时间';
int _customDateType = 1; //1:开始时间 2:结束时间
void initState() {
super.initState();
refreshScrollOffset();
_startDate = '${
widget.pickerModel.finalTime().year}/${
widget.pickerModel.finalTime().month}/${
widget.pickerModel.