gpt4 book ai didi

flutter - 如何在 Flutter 中创建自定义日历?

转载 作者:行者123 更新时间:2023-12-04 14:32:49 29 4
gpt4 key购买 nike

我想创建一个自定义日历,如下所示:
enter image description here
我怎样才能做到这一点?

最佳答案

enter image description here
您可以设计自己的自定义日历小部件如下

import 'package:flutter/material.dart';

class Calendar extends StatefulWidget {
@override
_CalendarState createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
DateTime selectedDate = DateTime.now(); // TO tracking date

int currentDateSelectedIndex = 0; //For Horizontal Date
ScrollController scrollController =
ScrollController(); //To Track Scroll of ListView

List<String> listOfMonths = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];

List<String> listOfDays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
automaticallyImplyLeading: false,
title: Text('My Calendar'),
),
body: Column(
children: [
//To Show Current Date
Container(
height: 30,
margin: EdgeInsets.only(left: 10),
alignment: Alignment.centerLeft,
child: Text(
selectedDate.day.toString() +
'-' +
listOfMonths[selectedDate.month - 1] +
', ' +
selectedDate.year.toString(),
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w800,
color: Colors.indigo[700]),
)),
SizedBox(height: 10),
//To show Calendar Widget
Container(
height: 80,
child: Container(
child: ListView.separated(
separatorBuilder: (BuildContext context, int index) {
return SizedBox(width: 10);
},
itemCount: 365,
controller: scrollController,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
setState(() {
currentDateSelectedIndex = index;
selectedDate =
DateTime.now().add(Duration(days: index));
});
},
child: Container(
height: 80,
width: 60,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.grey[400],
offset: Offset(3, 3),
blurRadius: 5)
],
color: currentDateSelectedIndex == index
? Colors.black
: Colors.white),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
listOfMonths[DateTime.now()
.add(Duration(days: index))
.month -
1]
.toString(),
style: TextStyle(
fontSize: 16,
color: currentDateSelectedIndex == index
? Colors.white
: Colors.grey),
),
SizedBox(
height: 5,
),
Text(
DateTime.now()
.add(Duration(days: index))
.day
.toString(),
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w700,
color: currentDateSelectedIndex == index
? Colors.white
: Colors.grey),
),
SizedBox(
height: 5,
),
Text(
listOfDays[DateTime.now()
.add(Duration(days: index))
.weekday -
1]
.toString(),
style: TextStyle(
fontSize: 16,
color: currentDateSelectedIndex == index
? Colors.white
: Colors.grey),
),
],
),
),
);
},
))),
],
),
));
}
}

关于flutter - 如何在 Flutter 中创建自定义日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61755268/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com