pick_calendar_day.dart 11.5 KB
import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/res/colors.dart';

import 'change_nongli_yangli.dart';

typedef CustomCallBack = void Function(Object object);

void incrementCounterDay(
  context,
  bool gongli,
  String year,
  String month,
  String day,
  CustomCallBack callBack,
) {
  showDialog(
    barrierDismissible: true,
    context: context,
    builder: (context) {
// 用Scaffold返回显示的内容,能跟随主题

      return GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Scaffold(
              backgroundColor: Colors.transparent, // 设置透明背影

              body: Stack(children: <Widget>[
                PickBodyDay(
                    gongli: gongli,
                    callBack: callBack,
                    year: year,
                    month: month,
                    day: day)
              ])));
    },
  );
}

class PickBodyDay extends StatefulWidget {
  final bool gongli;
  final CustomCallBack callBack;
  final String year;
  final String month;
  final String day;
  PickBodyDay(
      {Key key, this.gongli, this.callBack, this.year, this.month, this.day})
      : super(key: key);

  @override
  _PickBodyDayState createState() => _PickBodyDayState();
}

class _PickBodyDayState extends State<PickBodyDay> {
  // String sYear = "2000";

  // String sMonth = '6';

  String sDay = '15';

  List day = [], yday = [];

  List nday = [
    '初一',
    '初二',
    '初三',
    '初四',
    '初五',
    '初六',
    '初七',
    '初八',
    '初九',
    '初十',
    '十一',
    '十二',
    '十三',
    '十四',
    '十五',
    '十六',
    '十七',
    '十八',
    '十九',
    '二十',
    '廿一',
    '廿二',
    '廿三',
    '廿四',
    '廿五',
    '廿六',
    '廿七',
    '廿八',
    '廿九',
    '三十',
    '三十一'
  ];

  FixedExtentScrollController dayController =
      new FixedExtentScrollController(initialItem: 14);

  @override
  void initState() {
// TODO: implement initState

    super.initState();

// 获取当天的日期,年份默认选择的是2000年,月份默认是6月,日期默认是15日,时间默认是不详
    dayController =
        new FixedExtentScrollController(initialItem: int.parse(widget.day) - 1);
    sDay = widget.day;
    // DateTime today = DateTime.now();

// 请空之前的数据

// 获取默认年份和月份之下的天数

    int count = getYangliDay(int.parse(widget.year), int.parse(widget.month));

    print('count---$count');

    yday = [];

    for (int m = 1; m < count + 1; m++) {
      yday.add('$m');
    }
    // updateDay();
  }

  @override
  Widget build(BuildContext context) {
// 切换农历和阳历的时候更换月份和天的称呼

    if (widget.gongli) {
      day = yday;
    } else {
      int count = getNongliDay(int.parse(widget.year), int.parse(widget.month));

      print('农历的天数==${count}');

      List old = nday;

      List newDay = old.sublist(0, count);

// 初始化当前的选中的月份的天数

      day = newDay;
    }

    return Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        child: Container(
            // height: 300,
            decoration: BoxDecoration(color: Colors.white),

// padding: EdgeInsets.all(10),

            child: Column(children: <Widget>[
              Container(
                  // padding: EdgeInsets.symmetric(hori ScreenUtil().setWidth(30)),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              width: ScreenUtil().setWidth(1),
                              color: MyColours.color_E0E0E0))),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: GestureDetector(
                            child: Container(
                                color: Colors.white,
                                padding: EdgeInsets.symmetric(
                                    horizontal: ScreenUtil().setWidth(40)),
                                alignment: Alignment.centerLeft,
                                height: ScreenUtil().setWidth(90),
                                child: Text('取消',
                                    style: TextStyle(
                                        color: MyColours.color_333333))),
                            onTap: () {
                              print('关闭弹框');

                              Navigator.pop(context);
                            },
                          )),
                      Expanded(
                          flex: 1,
                          child: GestureDetector(
                            child: Container(
                              color: Colors.white,
                              padding: EdgeInsets.symmetric(
                                  horizontal: ScreenUtil().setWidth(40)),
                              alignment: Alignment.centerRight,
                              height: ScreenUtil().setWidth(90),
                              child: Text('确定',
                                  style:
                                      TextStyle(color: MyColours.color_199095)),
                            ),
                            onTap: () {
                              print('确定 关闭弹框');

                              // print(sMonth);

                              // print(sMonth);

                              print(sDay);

                              // print(sTime);
                              widget.callBack(sDay);
                              Navigator.pop(context);
                            },
                          )),
                    ],
                  )),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
//                     Expanded(
//                         flex: 1,
//                         child: Container(
//                             height: 200,
//                             child: CupertinoPicker(
//                               useMagnifier: true,

//                               magnification: 1.2,

//                               squeeze: 1,

//                               diameterRatio: 1.5,

//                               looping: false,

//                               backgroundColor: Colors.transparent, //选择器背景色

//                               itemExtent: 35, //item的高度

//                               onSelectedItemChanged: (index) {
//                                 print('index === $index');

//                                 int selectYear = 1980 + index;

// // 更改年的时候,查询选中的年和月的天数

//                                 print("选中年 = ${selectYear}");

//                                 setState(() {
//                                   sYear = '$selectYear';

// // 更换年的时候检测是阳历还是农历

//                                   updateDay();

// // monthController.jumpToItem(3);
//                                 });
//                               },

//                               scrollController: yearController,

//                               children: XZItem(year, '年'),
//                             ))),
//                     Expanded(
//                         flex: 1,
//                         child: Container(
//                             height: 200,
//                             child: CupertinoPicker(
//                               useMagnifier: true,

//                               magnification: 1.2,

//                               squeeze: 1,

//                               diameterRatio: 1.5,

//                               looping: false,

//                               backgroundColor: Colors.transparent, //选择器背景色

//                               itemExtent: 35, //item的高度

//                               onSelectedItemChanged: (index) {
// //选中item的位置索引

//                                 print("月index = $index}");

//                                 setState(() {
//                                   sMonth = '${index + 1}';
//                                 });

//                                 sMonth = '${index + 1}';

//                                 updateDay();
//                               },

//                               scrollController: monthController,

//                               children: XZItem(month, '月'),
//                             ))),
                    Expanded(
                        flex: 1,
                        child: Container(
                            height: 200,
                            child: CupertinoPicker(
                              useMagnifier: true,

                              magnification: 1.3,

                              squeeze: 1,

                              diameterRatio: 1.5,

                              looping: false,

                              backgroundColor: Colors.transparent, //选择器背景色

                              itemExtent: ScreenUtil().setWidth(70), //item的高度
                              selectionOverlay: SizedBox(),

                              onSelectedItemChanged: (index) {
//选中item的位置索引

                                print("日index = $index}");

                                setState(() {
                                  sDay = '${index + 1}';
                                });

                                sDay = '${index + 1}';
                              },

                              scrollController: dayController,

                              children: XZItem(day, '日'),
                            ))),
//                     Expanded(
//                         flex: 1,
//                         child: Container(
//                             height: 200,
//                             child: CupertinoPicker(
//                               useMagnifier: true,

//                               magnification: 1.2,

//                               squeeze: 1,

//                               diameterRatio: 1.5,

//                               looping: false,

//                               backgroundColor: Colors.transparent, //选择器背景色

//                               itemExtent: 35, //item的高度

//                               onSelectedItemChanged: (index) {
// //选中item的位置索引

//                                 print("index = $index}");

//                                 setState(() {
//                                   if (index == 0) {
//                                     sTime = '24';
//                                   } else {
//                                     sTime = '${index - 1}';
//                                   }
//                                 });
//                               },

//                               scrollController: timeController,

//                               children: XZItem(time, ''),
//                             ))),
                  ])
            ])));
  }

  List<Widget> XZItem(_data, type) {
    List<Widget> list = new List();

    for (var i = 0; i < _data.length; i++) {
      list.add(Text('${_data[i]}$type',
          style: TextStyle(color: Colors.black, fontSize: 14)));
    }

    return list;
  }
}