[英]How to limit the timing that can be picked from Cupertino date picker in Flutter?
[英]How can i hide day from cupertino date picker
我只需要從日期選擇器中提取年份和月份,那么我如何從日期選擇器中隱藏日期。
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,
)
我遇到了這個問題,嘗試了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),...
for Text('',...
然后在需要使用自定義 Picker 的地方調用它,例如:
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,
),
希望它可以幫助某人並節省我一直在為我的問題尋找簡單快捷的解決方案的時間。
您必須查看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),
),
);
}
}
flutter_cupertino_date_picker包有更新。 您現在可以指定日期格式
DatePicker.showDatePicker(context,
maxDateTime: DateTime.now(),
dateFormat:'MMMM-yyyy'
);
這是要走的路
Widget datetime() {
return CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (DateTime newdate) {
print(newdate);
},
minimumYear: 2010,
maximumYear: 2030,
mode: CupertinoDatePickerMode.date,
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.