我只需要从日期选择器中拍摄年份和月份,那么如何从日期选择器中隐藏日期。
CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (DateTime newdate) {
print(newdate);
widget.card.expDateTime = newdate.toString();
dateCnt.text = newdate.toString().split(" ")[0];
},
minimumYear: DateTime.now().year,
minimumDate: DateTime.now(),
mode: CupertinoDatePickerMode.date,
)
答案 0 :(得分:4)
我遇到了这个问题,尝试了flutter_cupertino_date_picker软件包,但是它似乎无法仅格式化月份和年份(不包括日期),因此您需要对其进行编程以扩展功能。对我来说,更改Flutter随附的CupertinoDatePicker的版本似乎更合乎逻辑,我所做的是在以下位置复制“ /Users/your_user_name/developer/flutter/packages/flutter/lib/src/cupertino/date_picker.dart”的所有内容我在本地环境中的另一个文件称为cupertino_picker_extended.dart
,然后(因为我想有一个快速的方法)在第1182行中我更改了:Text(localizations.datePickerDayOfMonth(day),...
表示Text('',...
然后在需要使用自定义选择器的位置将其命名为:
import 'package:local_app/ui/widgets/cupertino_picker_extended.dart' as CupertinoExtended;
并使用它:
CupertinoExtended.CupertinoDatePicker(
onDateTimeChanged: (DateTime value) {
setDate('${value.month}/${value.year}', setDateFunction,
section, arrayPos);
},
initialDateTime: DateTime.now(),
mode: CupertinoExtended.CupertinoDatePickerMode.date,
),
希望它对某人有帮助,并节省了我一直在寻找解决问题的简便方法的时间。
答案 1 :(得分:1)
flutter_cupertino_date_picker软件包中已有更新。现在,您可以指定日期格式
DatePicker.showDatePicker(context,
maxDateTime: DateTime.now(),
dateFormat:'MMMM-yyyy'
);
答案 2 :(得分:0)
您必须查看flutter_cupertino_date_picker对此进行打包。您可以避免从用户那里选择日期。
像下面的示例一样,我根据需要进行编辑。我希望它能帮助您实现您想要的。
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Date Picker Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _datetime = '';
int _year = 2018;
int _month = 11;
String _lang = 'en';
String _format = 'yyyy-mm';
bool _showTitleActions = true;
TextEditingController _langCtrl = TextEditingController();
TextEditingController _formatCtrl = TextEditingController();
@override
void initState() {
super.initState();
_langCtrl.text = 'zh';
_formatCtrl.text = 'yyyy-mm';
DateTime now = DateTime.now();
_year = now.year;
_month = now.month;
}
/// Display date picker.
void _showDatePicker() {
final bool showTitleActions = false;
DatePicker.showDatePicker(
context,
showTitleActions: _showTitleActions,
minYear: 1970,
maxYear: 2020,
initialYear: _year,
initialMonth: _month,
confirm: Text(
'custom ok',
style: TextStyle(color: Colors.red),
),
cancel: Text(
'custom cancel',
style: TextStyle(color: Colors.cyan),
),
locale: _lang,
dateFormat: _format,
onChanged: (year, month,date) {
debugPrint('onChanged date: $year-$month');
if (!showTitleActions) {
_changeDatetime(year, month);
}
},
onConfirm: (year, month,date) {
_changeDatetime(year, month);
},
);
}
void _changeDatetime(int year, int month) {
setState(() {
_year = year;
_month = month;
_datetime = '$year-$month';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Date Picker Demo'),
),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
// show title actions checkbox
Row(
children: <Widget>[
Text(
'Show title actions',
style: TextStyle(fontSize: 16.0),
),
Checkbox(
value: _showTitleActions,
onChanged: (value) {
setState(() {
_showTitleActions = value;
});
},
)
],
),
// Language input field
TextField(
controller: _langCtrl,
keyboardType: TextInputType.url,
decoration: InputDecoration(
labelText: 'Language',
hintText: 'en',
hintStyle: TextStyle(color: Colors.black26),
),
onChanged: (value) {
_lang = value;
},
),
// Formatter input field
TextField(
controller: _formatCtrl,
keyboardType: TextInputType.url,
decoration: InputDecoration(
labelText: 'Formatter',
hintText: 'yyyy-mm-dd / yyyy-mmm-dd / yyyy-mmmm-dd',
hintStyle: TextStyle(color: Colors.black26),
),
onChanged: (value) {
_format = value;
},
),
// Selected date
Container(
margin: EdgeInsets.only(top: 40.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Selected Date:',
style: Theme.of(context).textTheme.subhead,
),
Container(
padding: EdgeInsets.only(left: 12.0),
child: Text(
'$_datetime',
style: Theme.of(context).textTheme.title,
),
),
],
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _showDatePicker,
tooltip: 'Show DatePicker',
child: Icon(Icons.date_range),
),
);
}
}
答案 3 :(得分:-2)
这是要走的路
Widget datetime() {
return CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (DateTime newdate) {
print(newdate);
},
minimumYear: 2010,
maximumYear: 2030,
mode: CupertinoDatePickerMode.date,
);
}