flutter 自定义日历组件
·
项目内需要实现日历弹窗,使用自带的showdatepicker效果不是很满意,最终通过借鉴https://blog.csdn.net/qq_22492233/article/details/121678380并进行调整实现了效果,可以当作页面组件使用,也可以插入弹窗内使用,下面是代码
class MeetReservePage extends StatefulWidget {
const MeetReservePage({super.key});
@override
State<StatefulWidget> createState() {
return _MeetReservePage();
}
}
class _MeetReservePage extends State<MeetReservePage> {
bool isClose = false;
final FocusNode focusNode = FocusNode();
final controller = Get.put(MeetReserveController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: DiyUiUtils.BaseAppBar(
onTap: () async {
if (!isClose) {
isClose = true;
await BoostNavigator.instance.pop();
await Future.delayed(const Duration(milliseconds: 2000));
isClose = false;
}
},
titleWidget: Text('预约会议'.tl),
center: true,
actions: [
InkWell(
onTap: () {
ToastUtil.showText(text: '资源查询');
},
child: Container(
width: 30.px,
height: 30.px,
color: AppColors.color_19be6b,
),
)
]),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 10.px),
child: SingleChildScrollView(
child: Column(
children: [
meetThemeTitle(),
SizedBox(
height: 10.px,
),
meetTime(context),
SizedBox(
height: 10.px,
),
meetTimeDuration(),
SizedBox(
height: 10.px,
),
meetType(),
SizedBox(
height: 10.px,
),
meetResource(),
SizedBox(
height: 10.px,
),
Row(
children: [
Text('入会密码'),
Checkbox(value: false, onChanged: (value) {}),
Text('与会者密码'),
],
),
],
),
),
),
bottomNavigationBar: Row(
children: [
SizedBox(
width: 10.px,
),
Expanded(
child: InkWell(
child: DiyUiUtils.baseContanier(
Text(
'取消',
style: TextStyle(
color: AppColors.color_666666, fontSize: 18.px),
),
radius: 5.px,
height: 50.px,
borderWidth: 1.px,
alignment: Alignment.center,
color: AppColors.white),
),
),
SizedBox(
width: 10.px,
),
Expanded(
child: InkWell(
onTap: () async {
await BoostNavigator.instance.push("MeetDetailsPage");
BoostNavigator.instance.pop();
},
child: DiyUiUtils.baseContanier(
Text(
'预约会议',
style: TextStyle(color: AppColors.white, fontSize: 18.px),
),
radius: 5.px,
height: 50.px,
alignment: Alignment.center,
color: AppColors.color_3C90D2),
),
),
SizedBox(
width: 10.px,
),
],
),
);
}
///会议资源
Widget meetResource() {
return Row(
children: [
Text('会议资源'),
SizedBox(
width: 10.px,
),
Expanded(
child: Builder(builder: (context) {
return Obx(() => InkWell(
onTap: () {
final RenderBox button =
context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context)
.overlay!
.context
.findRenderObject()! as RenderBox;
Offset offset = Offset(0.0, button.size.height);
RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(offset, ancestor: overlay),
button.localToGlobal(
button.size.bottomRight(Offset.zero) + offset,
ancestor: overlay),
),
Offset.zero & overlay.size,
);
showMenu(
context: context,
position: position,
constraints: BoxConstraints(
maxWidth: context.size?.width ?? 0.0,
maxHeight: 200),
items: List.generate(
controller.meetSrcList.length,
(index) => PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.px),
onTap: () {
controller.meetSrc.value =
controller.meetSrcList[index];
},
child: Container(
width: context.size?.width,
color: AppColors.white,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px),
child: Text(
controller.meetSrcList[index],
style: TextStyle(fontSize: 14.px),
),
))));
},
child: DiyUiUtils.baseContanier(
Row(
children: [
Text(
controller.meetSrc.value,
style: TextStyle(fontSize: 14.px),
)
],
),
radius: 10.px,
borderColor: AppColors.color_666666,
borderWidth: 1.px,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px)),
));
}),
)
],
);
}
///会议类型
Widget meetType() {
return Row(
children: [
Text('会议类型'),
SizedBox(
width: 10.px,
),
Expanded(
child: Builder(builder: (context) {
return Obx(() => InkWell(
onTap: () {
final RenderBox button =
context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context)
.overlay!
.context
.findRenderObject()! as RenderBox;
Offset offset = Offset(0.0, button.size.height);
RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(offset, ancestor: overlay),
button.localToGlobal(
button.size.bottomRight(Offset.zero) + offset,
ancestor: overlay),
),
Offset.zero & overlay.size,
);
showMenu(
context: context,
position: position,
constraints: BoxConstraints(
maxWidth: context.size?.width ?? 0.0,
maxHeight: 200),
items: List.generate(
controller.meetTypeList.length,
(index) => PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.px),
onTap: () {
controller.meetType.value =
controller.meetTypeList[index];
},
child: Container(
width: context.size?.width,
color: AppColors.white,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px),
child: Text(
controller.meetTypeList[index],
style: TextStyle(fontSize: 14.px),
),
))));
},
child: DiyUiUtils.baseContanier(
Row(
children: [
Text(
controller.meetType.value,
style: TextStyle(fontSize: 14.px),
)
],
),
radius: 10.px,
borderColor: AppColors.color_666666,
borderWidth: 1.px,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px)),
));
}),
)
],
);
}
///会议时长
Widget meetTimeDuration() {
return Row(
children: [
Text('会议时长'),
SizedBox(
width: 10.px,
),
Expanded(
child: Builder(builder: (context) {
return Obx(() => InkWell(
onTap: () {
final RenderBox button =
context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context)
.overlay!
.context
.findRenderObject()! as RenderBox;
Offset offset = Offset(0.0, button.size.height);
RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(offset, ancestor: overlay),
button.localToGlobal(
button.size.bottomRight(Offset.zero) + offset,
ancestor: overlay),
),
Offset.zero & overlay.size,
);
showMenu(
context: context,
position: position,
constraints: BoxConstraints(
maxWidth: context.size?.width ?? 0.0,
maxHeight: 200),
items: List.generate(
controller.timeLongList.length,
(index) => PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.px),
onTap: () {
controller.timeLong.value =
controller.timeLongList[index];
},
child: Container(
width: context.size?.width,
color: AppColors.white,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px),
child: Text(
controller.timeLongList[index],
style: TextStyle(fontSize: 14.px),
),
))));
},
child: DiyUiUtils.baseContanier(
Row(
children: [
Text(
controller.timeLong.value,
style: TextStyle(fontSize: 14.px),
)
],
),
radius: 10.px,
borderColor: AppColors.color_666666,
borderWidth: 1.px,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px)),
));
}),
)
],
);
}
///会议时间
Widget meetTime(BuildContext context) {
return Row(
children: [
Text('会议时间'),
SizedBox(
width: 10.px,
),
Expanded(
flex: 2,
child: Obx(() => InkWell(
onTap: () async {
final result = await showDialog(
context: context,
builder: (con) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 20.px),
alignment: Alignment.center,
child: DiyDatePicker(date: controller.meetDate.value,),
);
});
if(result!=null){
controller.meetDate.value = result;
}
// showDatePicker(
// context: context,
// initialDate: DateTime.now(),
// firstDate: DateTime(1970, 1, 1),
// lastDate: DateTime(2060, 12, 31),
// helpText: '',
// cancelText: '取消',
// confirmText: '确认',
// initialEntryMode: DatePickerEntryMode.calendarOnly,
// builder: (context, child) {
// return Theme(
// data: ThemeData(
// primarySwatch: Colors.green,
// ),
// child: child!);
// }).then((value) {
// if (value != null) {
// var data = DateTime.parse(value.toString());
//
// print('data===============$data====${data.weekday}');
// }
// });
},
child: DiyUiUtils.baseContanier(
Row(
children: [
Expanded(
child: Text(
controller.meetDate.value,
style: TextStyle(fontSize: 14.px),
)),
Container(
width: 30,
height: 20.px,
color: AppColors.black,
)
],
),
radius: 10.px,
borderColor: AppColors.color_666666,
borderWidth: 1.px,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px)),
)),
),
SizedBox(
width: 10.px,
),
Expanded(
flex: 1,
child: Builder(builder: (context) {
return Obx(() => InkWell(
onTap: () {
final RenderBox button =
context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context)
.overlay!
.context
.findRenderObject()! as RenderBox;
Offset offset = Offset(0.0, button.size.height);
RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(offset, ancestor: overlay),
button.localToGlobal(
button.size.bottomRight(Offset.zero) + offset,
ancestor: overlay),
),
Offset.zero & overlay.size,
);
showMenu(
context: context,
position: position,
constraints: BoxConstraints(
maxWidth: context.size?.width ?? 0.0,
maxHeight: 200),
items: List.generate(
controller.timeList.length,
(index) => PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.px),
onTap: () {
ToastUtil.showText(text: 'text$index');
controller.time.value =
controller.timeList[index];
},
child: Container(
width: context.size?.width,
color: AppColors.white,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px),
child: Text(
controller.timeList[index],
style: TextStyle(fontSize: 14.px),
),
))));
},
child: DiyUiUtils.baseContanier(
Row(
children: [
Text(
controller.time.value,
style: TextStyle(fontSize: 14.px),
)
],
),
radius: 10.px,
borderColor: AppColors.color_666666,
borderWidth: 1.px,
padding: EdgeInsets.symmetric(
vertical: 4.px, horizontal: 10.px)),
));
}),
)
],
);
}
///会议主题
Widget meetThemeTitle() {
return Row(
children: [
Text('会议主题'),
SizedBox(
width: 10.px,
),
Expanded(
child: TextField(
controller: controller.textController,
keyboardType: TextInputType.text,
focusNode: focusNode,
contextMenuBuilder: DiyUiUtils.contextMenuBuilder,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
counterText: '',
hintText: 'ta的会议'.tl,
border: const OutlineInputBorder(borderSide: BorderSide.none),
contentPadding: EdgeInsets.symmetric(
// horizontal: 1.px,
vertical: 0.px,
),
filled: true,
fillColor: AppColors.white,
hintStyle: TextStyle(
fontSize: 16.px,
color: AppColors.color_CFCFCF,
),
),
maxLines: 1,
maxLength: 1000,
style: TextStyle(
fontSize: 16.px,
color: Colors.black87,
),
),
)
],
);
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)