/*
 * @Author: XianKaiQun
 * @Date: 2020-04-28 11:07:24
 * @LastEditors  : WuWei
 * @LastEditTime : 2021-05-28 17:00:47
 * @Descripttion: 任务管理-年份选择
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

showYearPicker(context, Function(dynamic) onConfirm, {initialYear}) {
  var now = DateTime.now();

  var list = [];

  ///起始2019年
  for (var i = 0; list.length < now.year - 2018; i++)
    list.add({
      'year': now.year - i,
    });

  Map current = {
    'year': null,
  };
  var _initialYear = initialYear ?? current['year'];
  var index = list.indexWhere(
    (f) => (f['year'] == _initialYear),
  );
  showModalBottomSheet(
    context: context,
    backgroundColor: Colors.transparent,
    builder: (context) {
      final style = WTheme.of(context);
      final colorScheme = style.colorScheme;
      return WisBottomModal(
        title: Text('年份筛选'),
        body: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Wisdom(
              height: 200.pt,
              maxHeight: 200.pt,
              child: CupertinoPicker(
                scrollController: FixedExtentScrollController(
                  initialItem: index,
                ),
                backgroundColor: Colors.transparent,
                itemExtent: 50.pt,
                onSelectedItemChanged: (i) {
                  current = list[i];
                },
                diameterRatio: 9999,
                children: [
                  for (var item in list)
                    Center(
                      child: Text('${item['year']}年'),
                    )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.all(15.pt),
              child: MaterialButton(
                height: 44.pt,
                color: colorScheme.primary,
                textColor: Colors.white,
                onPressed: () {
                  Navigator.pop(context);
                  onConfirm(current['year']);
                },
                child: Text('确定'),
              ),
            ),
          ],
        ),
      );
    },
  );
}
