format_button.dart
1.91 KB
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
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:table_calendar/src/shared/utils.dart' show CalendarFormat;
class FormatButton extends StatelessWidget {
final CalendarFormat calendarFormat;
final ValueChanged<CalendarFormat> onTap;
final TextStyle textStyle;
final BoxDecoration decoration;
final EdgeInsets padding;
final bool showsNextFormat;
final Map<CalendarFormat, String> availableCalendarFormats;
const FormatButton({
super.key,
required this.calendarFormat,
required this.onTap,
required this.textStyle,
required this.decoration,
required this.padding,
required this.showsNextFormat,
required this.availableCalendarFormats,
});
@override
Widget build(BuildContext context) {
final child = Container(
decoration: decoration,
padding: padding,
child: Text(
_formatButtonText,
style: textStyle,
),
);
final platform = Theme.of(context).platform;
return !kIsWeb &&
(platform == TargetPlatform.iOS || platform == TargetPlatform.macOS)
? CupertinoButton(
onPressed: () => onTap(_nextFormat()),
padding: EdgeInsets.zero,
child: child,
)
: InkWell(
borderRadius:
decoration.borderRadius?.resolve(Directionality.of(context)),
onTap: () => onTap(_nextFormat()),
child: child,
);
}
String get _formatButtonText => showsNextFormat
? availableCalendarFormats[_nextFormat()]!
: availableCalendarFormats[calendarFormat]!;
CalendarFormat _nextFormat() {
final formats = availableCalendarFormats.keys.toList();
int id = formats.indexOf(calendarFormat);
id = (id + 1) % formats.length;
return formats[id];
}
}