Flutter日历展示与操作插件flexible_calendar的使用
Flutter日历展示与操作插件flexible_calendar的使用
Flexible Calendar
小部件使自定义日历变得更加容易!基于 StatefulWidget
,此小部件在 Android 和 iOS 上都能工作。
安装
在你的 pubspec.yaml
文件中添加以下依赖:
dependencies:
flexible_calendar: 1.0.0
简单使用
实现后,你可以像使用普通的子部件一样使用它。例如:
截图
选择日期
选择日期范围
自定义使用
有几个选项可以让你获得更多控制:
属性名称 | 描述 |
---|---|
initialStartDate |
初始化日历的开始日期,每次在日历中选择一个日期时都需要更新它。 |
initialEndDate |
初始化日历的结束日期。如果使用单选模式选择一个日期,可以设置 initialEndDate = null 。 |
startEndDateChange |
每次点击日期时,都会跳转到此函数以供你更新变量。 |
minimumDate |
限制可选择的最小日期。 |
maximumDate |
限制可选择的最大日期。 |
isConvertDateOfWeekToVietnamese |
默认的周几显示为 “Mon, Tue, …”,如果你希望显示为 “T2, T3, T4, …”, 请将其设置为 true 。 |
formatMonthTitle |
显示在日历顶部的标题格式。例如:DateFormat("MM, yyyy") 将显示为 07, 2021 。 |
textPreMonthTitle |
设置标题前的文字。例如:textPreMonthTitle: "Month", 将显示为 Month 07, 2021 。 |
decorationOfSelectedDay |
可以自定义所选日期的小部件装饰。 |
colorOfDayInMonth |
设置当前月份日期的颜色。 |
colorDayInAnotherMonth |
设置其他月份日期的颜色。 |
colorOfSelectedDay |
设置所选日期的颜色。 |
styleOfDay |
日历中所有日期的通用样式。 |
isSelectedOnlyOneDay |
启用单选模式,选择一个日期,默认是选择范围模式。 |
colorInRange |
设置日期范围的背景颜色(不包含起始日期和结束日期)。 |
示例代码
以下是使用 Flexible Calendar
的完整示例代码:
import 'package:flexible_calendar/flexible_calendar.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime? timeChecking;
DateTime? timeCheckout;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: () {
_showDialogCalendar(context);
},
),
);
}
_showDialogCalendar(BuildContext context) {
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
isDismissible: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topRight: Radius.circular(15), topLeft: Radius.circular(15)),
),
builder: (context) {
return GestureDetector(
onVerticalDragDown: (_) {},
child: SingleChildScrollView(
child: Container(
height: 500,
child:
FlexibleCalendarView(
// 限制开始日期
// minimumDate: isChecking || timeChecking == null ? DateTime.now() : timeChecking?.add(Duration(days: 1)),
// 限制结束日期
// maximumDate: isChecking || timeChecking == null ? DateTime.now() : timeChecking?.add(Duration(days: 1)),
initialStartDate: timeChecking,
initialEndDate: timeCheckout,
startEndDateChange: (DateTime startDateData, DateTime? endDateData) {
setState(() {
timeChecking = startDateData;
timeCheckout = endDateData;
});
},
isConvertDateOfWeekToVietnamese: true,
formatMonthTitle: DateFormat("MM, yyyy"),
textPreMonthTitle: "Tháng",
decorationOfSelectedDay: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(25)
),
colorDayInAnotherMonth: Colors.grey,
colorInRange: Colors.blue.withOpacity(0.1),
borderBackgroundStartDay: BorderRadius.only(topLeft: Radius.circular(25), bottomLeft: Radius.circular(25)),
borderBackgroundEndDay: BorderRadius.only(topRight: Radius.circular(25), bottomRight: Radius.circular(25)),
formatTitleDateOfWeek: "EEE",
isSelectedOnlyOneDay: false,
colorOfDayInMonth: Colors.black,
colorOfSelectedDay: Colors.white,
styleOfDay: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
),
),
),
);
});
}
}
更多关于Flutter日历展示与操作插件flexible_calendar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复