pick_calendar_year.dart 9.88 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 'package:one_key/widgets/v_empty_view.dart';

import 'change_nongli_yangli.dart';

typedef CustomCallBack = void Function(Object? object);
void incrementCounterYear(
  context,
  bool gongli,
  String? year,
  CustomCallBack callBack,
) {
  showDialog(
    barrierDismissible: true,
    context: context,
    builder: (context) {
// 用Scaffold返回显示的内容,能跟随主题

      return Scaffold(
          backgroundColor: Colors.transparent, // 设置透明背影

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

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

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

class _PickBodyYearState extends State<PickBodyYear> {
  String? sYear = "2000";

  // String sMonth = '6';

  // String sDay = '15';

  // String sTime = '24';

  List yearList = [];

  // List nMonth = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'];

  // List time = [
  //   '时辰未知',
  //   '0时子',
  //   '1时丑',
  //   '2时丑',
  //   '3时寅',
  //   '4时寅',
  //   '5时',
  //   '6时',
  //   '7时',
  //   '8时',
  //   '9时',
  //   '10时',
  //   '11时',
  //   '12时',
  //   '13时丑',
  //   '14时丑',
  //   '15时寅',
  //   '16时寅',
  //   '17时',
  //   '18时',
  //   '19时',
  //   '20时',
  //   '21时',
  //   '22时',
  //   '23时'
  // ];

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

  FixedExtentScrollController yearController =
      new FixedExtentScrollController(initialItem: 20);

  // FixedExtentScrollController monthController =
  //     new FixedExtentScrollController(initialItem: 5);

  // FixedExtentScrollController dayController =
  //     new FixedExtentScrollController(initialItem: 14);

  // FixedExtentScrollController timeController =
  //     new FixedExtentScrollController();

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

    super.initState();

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

// 请空之前的数据
    sYear = widget.year;
    for (var y = 1900; y < today.year + 50; y++) {
      yearList.add('$y');
    }

    // yMonth = [];

    // for (int m = 1; m < 13; m++) {
    //   yMonth.add('$m');
    // }

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

    // int count = getYangliDay(int.parse(sYear), int.parse(sMonth));

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

    // yday = [];

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

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

//     if (widget.gongli) {
//       month = yMonth;

//       day = yday;
//     } else {
//       month = nMonth;

//       int count = getNongliDay(int.parse(sYear), int.parse(sMonth));

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

//       List old = nday;

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

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

//       day = newDay;
//     }

    return Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        child: Container(
            // height: ScreenUtil().setWidth(380+100),
            decoration: BoxDecoration(color: Colors.white),

// padding: EdgeInsets.all(10),

            child: Column(children: <Widget>[
              // VEmptyView(10),
              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('关闭弹框');

                              if (Navigator.canPop(context)) {
                                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(sYear);

                              // print(sMonth);

                              // print(sDay);

                              // print(sTime);
                              widget.callBack!(sYear);
                              if (Navigator.canPop(context)) {
                                Navigator.pop(context);
                              }
                            },
                          )),
                    ],
                  )),
              VEmptyView(30),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                        flex: 1,
                        child: Container(
                            height: ScreenUtil().setWidth(360),
                            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) {
                                print('index === $index');

                                int selectYear = 1900 + index;

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

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

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

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

                                  // updateDay();

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

                              scrollController: yearController,

                              children: XZItem(yearList, '年'),
                            ))),
                  ]),
              VEmptyView(30),
            ])));
  }

//   updateDay() {
//     if (widget.gongli) {
// // 公历// 截取天数

//       print('widget.sYear ==== ${sYear}');

//       print('widget.sMonth ==== ${sMonth}');

//       print('widget.sDay ==== ${sDay}');

//       int count = getYangliDay(int.parse(sYear), int.parse(sMonth));

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

//       yday = [];

//       for (int m = 1; m < count + 1; m++) {
//         yday.add('$m');
//       }
//     } else {
//       int count = getNongliDay(int.parse(sYear), int.parse(sMonth));

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

//       List old = day;

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

//       print(old);

//       print(newDay);

//       day = newDay;
//     }
//   }

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

    for (var i = 0; i < _data.length; i++) {
      list.add(Container(
        alignment: Alignment.center,
        child: Text('${_data[i]}$type',
            style: TextStyle(
                color: MyColours.color_333333,
                fontSize: ScreenUtil().setSp(28))),
      ));
    }

    return list;
  }
}