123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'YSTools.dart';
- class YSDatePicker extends StatefulWidget {
- final ValueSetter<String> choose;
- final int type;
- const YSDatePicker({Key? key, required this.choose, this.type = 1}) : super(key: key);
- @override
- YSDatePickerState createState() => YSDatePickerState();
- }
- class YSDatePickerState extends State<YSDatePicker> {
- String birthday = '';
- @override
- void initState() {
- DateTime now = DateTime.now();
- if(widget.type==1){
- birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}';
- }else if(widget.type==2){
- birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')}';
- }else if(widget.type==3){
- birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')} ${now.hour.toString().padLeft(2,'0')}:${now.minute.toString().padLeft(2,'0')}';
- }
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- color: Colors.transparent,
- height: 280,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Container(
- height: 40,
- padding: const EdgeInsets.only(left: 20,right: 20),
- decoration: const BoxDecoration(
- color: Colors.white,
- border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE)))
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- GestureDetector(
- child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
- onTap: (){
- Navigator.pop(context);
- },
- ),
- GestureDetector(
- child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
- onTap: (){
- widget.choose(birthday);
- Navigator.pop(context);
- },
- )
- ],
- ),
- ),
- Container(
- height: 240,
- padding: const EdgeInsets.all(10),
- decoration: const BoxDecoration(
- color: Colors.white,
- ),
- child: CupertinoDatePicker(
- initialDateTime: DateTime.now(),
- onDateTimeChanged: (date) {//+' '+date.hour.toString().padLeft(2,'0')+':'+date.minute.toString().padLeft(2,'0')
- if(widget.type==1){
- birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}';
- }else if(widget.type==2){
- birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}';
- }else if(widget.type==3){
- birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')} ${date.hour.toString().padLeft(2,'0')}:${date.minute.toString().padLeft(2,'0')}';
- }
- },
- mode: widget.type==3?CupertinoDatePickerMode.dateAndTime:CupertinoDatePickerMode.date,
- ),
- ),
- ],
- ),
- );
- }
- }
- class YSMonthPicker extends StatefulWidget {
- final ValueSetter valueSetter;
- const YSMonthPicker({Key? key, required this.valueSetter}) : super(key: key);
- @override
- YSMonthPickerState createState() => YSMonthPickerState();
- }
- class YSMonthPickerState extends State<YSMonthPicker> {
- late FixedExtentScrollController _yearController;
- late FixedExtentScrollController _monthController;
- final List _yearArray = [];
- String _yearStr = '';
- final List _monthArray = [];
- String _monthStr = '';
- @override
- void initState() {
- int yNumber= 2000;
- for(int i=0;i<40;i++){
- yNumber++;
- _yearArray.add({'title':'$yNumber年','value':'$yNumber'});
- }
- int mNumber= 0;
- for(int i=0;i<12;i++){
- mNumber++;
- _monthArray.add({'title':'$mNumber月','value':'$mNumber'});
- }
- DateTime now = DateTime.now();
- _yearStr = '${now.year}';
- _monthStr = '${now.month}'.padLeft(2,'0');
- int yearIndex = _yearArray.indexWhere((element) => element['value']=='${now.year}');
- int monthIndex = _monthArray.indexWhere((element) => element['value']=='${now.month}');
- _yearController = FixedExtentScrollController(initialItem: yearIndex);
- _monthController = FixedExtentScrollController(initialItem: monthIndex);
- super.initState();
- }
- @override
- void dispose() {
- _yearController.dispose();
- _monthController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return ClipRRect(
- borderRadius: const BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
- child: Container(
- color: Colors.transparent,
- height: hsp(300),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Container(
- height: hsp(50),
- padding: const EdgeInsets.only(left: 20,right: 20),
- decoration: const BoxDecoration(
- color: Colors.white,
- border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE))),
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- GestureDetector(
- child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
- onTap: (){
- Navigator.pop(context);
- },
- ),
- Text('选择年月',style: TextStyle(fontSize: zsp(16),color: const Color(0xFF323233)),),
- GestureDetector(
- child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
- onTap: (){
- Map value = {'title':'$_yearStr年$_monthStr月','value':'$_yearStr年$_monthStr月'};
- widget.valueSetter(value);
- Navigator.pop(context);
- },
- )
- ],
- ),
- ),
- Container(
- height: hsp(250),
- padding: const EdgeInsets.all(10),
- decoration: const BoxDecoration(
- color: Colors.white,
- ),
- child: LayoutBuilder(
- builder: (context,conSize){
- return Row(
- children: [
- SizedBox(
- width: conSize.maxWidth/2,
- child: CupertinoPicker(
- onSelectedItemChanged: (int value) {
- _yearStr = _yearArray[value]['value'];
- },
- itemExtent: hsp(50),
- scrollController: _yearController,
- children: _yearArray.map((e) => Container(
- alignment: Alignment.center,
- child: Text(e['title']),
- )).toList(),
- ),
- ),
- SizedBox(
- width: conSize.maxWidth/2,
- child: CupertinoPicker(
- onSelectedItemChanged: (int value) {
- _monthStr = '${_monthArray[value]['value']}'.padLeft(2,'0');
- },
- itemExtent: hsp(50),
- scrollController: _monthController,
- children: _monthArray.map((e) => Container(
- alignment: Alignment.center,
- child: Text(e['title']),
- )).toList(),
- ),
- ),
- ],
- );
- },
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
|