flutter封装日历选择器(单日选择)

简单封装:

引入库:table_calendar

dart 复制代码
import 'package:generated/l10n.dart';
import 'package:jade/utils/JadeColors.dart';
import 'package:jade/utils/Utils.dart';
import 'package:util/easy_loading_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:table_calendar/table_calendar.dart';


class CustomCalendarSelector extends StatefulWidget{
  final DateTime dateTime;
  const CustomCalendarSelector({this.dateTime});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CustomCalendarSelectorState();
  }
}
class _CustomCalendarSelectorState extends State<CustomCalendarSelector>{
  CalendarController _calendarController;
  DateTime _selectDateTime;
  @override
  void initState() {
    super.initState();
    _calendarController = CalendarController();
    _selectDateTime = widget.dateTime;
  }

  @override
  void dispose() {
    _calendarController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    //
    return Container(
      height: Utils().screenWidth(context)*1.4,
      child: Column(
        children: [
          Expanded(
              child: TableCalendar(
                  calendarController: _calendarController,
                  startDay: DateTime.now(),
                  endDay: DateTime(DateTime.now().year+ 1,12,31),
                  calendarStyle: const CalendarStyle(
                    weekendStyle: TextStyle(color: Color(0xffff415b)),
                    todayColor: Colors.black12,
                    selectedColor: Color(0xff44aab0),
                  ),
                  headerStyle: HeaderStyle(
                    centerHeaderTitle: true,
                    leftChevronVisible: true,
                    rightChevronVisible: true,
                    formatButtonVisible: false,
                  ),
                  initialSelectedDay:widget.dateTime??DateTime(DateTime.now().year,
                      DateTime.now().month,DateTime.now().day+1),
                  onUnavailableDaySelected: (){
                    esLoadingToast('请选择可选日期之内的时间');
                  },
                  onDaySelected: (DateTime dateTime, List events, List holidays){
                    if(dateTime.day == DateTime.now().day){
                      esLoadingToast('请选择当期日期之后的时间');
                      return;
                    }
                    _selectDateTime = dateTime;
                  },
                  onHeaderTapped:(DateTime dateTime){}
              ),
          ),
          Container(
            margin: EdgeInsets.only(top: 20.h, left: 80.w),
            child: Row(
              children: <Widget>[
                Image.asset(
                  'images/cinema/buy/cinema_buy_icon_q.png',
                  width: 35.w,
                  height: 35.h,
                ),
                SizedBox(
                  width: 10.w,
                ),
                Text(
                  '从当日起的5个工作日内无法上刊',
                  style: TextStyle(
                      fontSize: 24.sp, color: JadeColors.green_3),
                )
              ],
            ),
          ),
          Container(
              margin: EdgeInsets.only(top: 20.h, left: 80.w, right: 80.w,bottom: 40.w),
              width: double.infinity,
              height: 85.h,
              child: TextButton(
                  style: ButtonStyle(
                      minimumSize: MaterialStateProperty.all(Size(300, 38)),
                      backgroundColor: MaterialStateProperty.all(
                          JadeColors.green_3.withOpacity(1.0))),
                  onPressed: () async {
                    if (DateTime.now().isAfter(_selectDateTime)) {
                      esLoadingToast('请选择当期日期之后的时间');
                      return;
                    }
                    Navigator.pop(context, _selectDateTime);
                  },
                  child: Text(
                    S.current.baocun,
                    style: TextStyle(fontSize: 32.sp, color: Colors.white),
                  ))
          )
        ],
      ),
    );
  }
}

引用:

dart 复制代码
_showScheduledDateBottom() {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(25.w),
                topRight: Radius.circular(25.w))),
        builder: (BuildContext context) {
          return CustomCalendarSelector(dateTime: time);
        }).then((value) {
      if (value == null) return;
      print('${time.year}-${time.month}-${time.day}');
    });
  }
相关推荐
AiFlutter1 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
m0_748247801 天前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者1 天前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者1 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig2 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜2 天前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭2 天前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_3 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强3 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie3 天前
flutter 快速实现侧边栏
前端·javascript·flutter