-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtd_calendar_page.dart
320 lines (315 loc) · 10.3 KB
/
td_calendar_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../../base/example_widget.dart';
import '../annotation/demo.dart';
class TDCalendarPage extends StatelessWidget {
const TDCalendarPage({super.key});
@override
Widget build(BuildContext context) {
return Container(
color: TDTheme.of(context).grayColor2,
child: ExamplePage(
title: tdTitle(context),
desc: '按照日历形式展示数据或日期的容器。',
exampleCodeGroup: 'calendar',
children: [
ExampleModule(title: '组件类型', children: [
ExampleItem(
ignoreCode: true,
center: false,
builder: (BuildContext context) {
return const CodeWrapper(builder: _buildSimple);
},
),
]),
ExampleModule(title: '组件样式', children: [
ExampleItem(
desc: '可以自由定义想要的风格',
ignoreCode: true,
center: false,
builder: (BuildContext context) {
return const CodeWrapper(builder: _buildStyle);
},
),
ExampleItem(
desc: '不使用Popup',
ignoreCode: true,
center: false,
builder: (BuildContext context) {
return const CodeWrapper(builder: _buildBlock);
},
),
]),
],
test: const [],
),
);
}
}
@Demo(group: 'calendar')
Widget _buildSimple(BuildContext context) {
final size = MediaQuery.of(context).size;
final selected = ValueNotifier<List<int>>([DateTime.now().millisecondsSinceEpoch + 30 * 24 * 60 * 60 * 1000]);
return ValueListenableBuilder(
valueListenable: selected,
builder: (context, value, child) {
final date = DateTime.fromMillisecondsSinceEpoch(value[0]);
return TDCellGroup(
cells: [
TDCell(
title: '单个选择日历',
arrow: true,
note: '${date.year}-${date.month}-${date.day}',
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
onConfirm: (value) {
print('onConfirm:$value');
selected.value = value;
},
onClose: () {
print('onClose');
},
child: TDCalendar(
title: '请选择日期',
value: value,
height: size.height * 0.6 + 176,
onCellClick: (value, type, tdate) {
print('onCellClick:$value');
},
onCellLongPress: (value, type, tdate) {
print('onCellLongPress:$value');
},
onHeaderClick: (index, week) {
print('onHeaderClick:$week');
},
onChange: (value) {
print('onChange:$value');
},
),
);
},
),
TDCell(
title: '多个选择日历',
arrow: true,
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
child: TDCalendar(
title: '请选择日期',
type: CalendarType.multiple,
value: [DateTime.now().millisecondsSinceEpoch],
height: size.height * 0.6 + 176,
),
);
},
),
TDCell(
title: '区间选择日历',
arrow: true,
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
child: TDCalendar(
title: '请选择日期区间',
type: CalendarType.range,
value: [
DateTime.now().millisecondsSinceEpoch,
DateTime.now().add(const Duration(days: 6)).millisecondsSinceEpoch,
],
height: size.height * 0.6 + 176,
),
);
},
),
TDCell(
title: '单个选择日历和时间',
arrow: true,
note: '${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}',
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
onConfirm: (value) {
print('onConfirm:$value');
selected.value = value;
},
onClose: () {
print('onClose');
},
child: TDCalendar(
title: '请选择日期和时间',
value: value,
height: size.height * 0.92,
useTimePicker: true,
// pickerHeight: 100,
// pickerItemCount: 2,
onCellClick: (value, type, tdate) {
print('onCellClick:$value');
},
onCellLongPress: (value, type, tdate) {
print('onCellLongPress:$value');
},
onHeaderClick: (index, week) {
print('onHeaderClick:$week');
},
onChange: (value) {
print('onChange:$value');
},
),
);
},
),
TDCell(
title: '区间选择日历和时间',
arrow: true,
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
onConfirm: (value) {
print('onConfirm:$value');
},
onClose: () {
print('onClose');
},
child: TDCalendar(
title: '请选择日期和时间区间',
height: size.height * 0.92,
type: CalendarType.range,
value: [
DateTime.now().millisecondsSinceEpoch,
DateTime.now().add(const Duration(days: 3)).millisecondsSinceEpoch,
],
useTimePicker: true,
onCellClick: (value, type, tdate) {
print('onCellClick:$value');
},
onCellLongPress: (value, type, tdate) {
print('onCellLongPress:$value');
},
onHeaderClick: (index, week) {
print('onHeaderClick:$week');
},
onChange: (value) {
print('onChange:$value');
},
),
);
},
),
],
);
},
);
}
@Demo(group: 'calendar')
Widget _buildStyle(BuildContext context) {
final size = MediaQuery.of(context).size;
const map = {
1: '初一',
2: '初二',
3: '初三',
14: '情人节',
15: '元宵节',
};
return TDCellGroup(
cells: [
TDCell(
title: '自定义文案',
arrow: true,
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
child: TDCalendar(
title: '请选择日期',
height: size.height * 0.6 + 176,
minDate: DateTime(2022, 1, 1).millisecondsSinceEpoch,
maxDate: DateTime(2022, 2, 15).millisecondsSinceEpoch,
format: (day) {
day?.suffix = '¥60';
if (day?.date.month == 2) {
if (map.keys.contains(day?.date.day)) {
day?.suffix = '¥100';
day?.prefix = map[day.date.day];
day?.style = TextStyle(
fontSize: TDTheme.of(context).fontTitleMedium?.size,
height: TDTheme.of(context).fontTitleMedium?.height,
fontWeight: TDTheme.of(context).fontTitleMedium?.fontWeight,
color: TDTheme.of(context).errorColor6,
);
if (day?.typeNotifier.value == DateSelectType.selected) {
day?.style = day.style?.copyWith(color: TDTheme.of(context).fontWhColor1);
}
}
}
return null;
},
),
);
},
),
TDCell(
title: '自定义按钮',
arrow: true,
onClick: (cell) {
late final TDCalendarPopup calendar;
calendar = TDCalendarPopup(
context,
visible: true,
confirmBtn: Padding(
padding: EdgeInsets.symmetric(vertical: TDTheme.of(context).spacer16),
child: TDButton(
theme: TDButtonTheme.danger,
shape: TDButtonShape.round,
text: 'ok',
isBlock: true,
size: TDButtonSize.large,
onTap: () {
print(calendar.selected);
calendar.close();
},
),
),
child: TDCalendar(
title: '请选择日期',
value: [DateTime.now().millisecondsSinceEpoch],
height: size.height * 0.6 + 176,
),
);
},
),
TDCell(
title: '自定义日期区间',
arrow: true,
onClick: (cell) {
TDCalendarPopup(
context,
visible: true,
child: TDCalendar(
title: '请选择日期',
minDate: DateTime(2000, 1, 1).millisecondsSinceEpoch,
maxDate: DateTime(3000, 1, 1).millisecondsSinceEpoch,
value: [DateTime(2024, 10, 1).millisecondsSinceEpoch],
height: size.height * 0.6 + 176,
),
);
},
),
],
);
}
@Demo(group: 'calendar')
Widget _buildBlock(BuildContext context) {
final size = MediaQuery.of(context).size;
return TDCalendar(
title: '请选择日期',
value: [DateTime.now().millisecondsSinceEpoch],
height: size.height * 0.6 + 176,
);
}