icon.dart
4.11 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
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
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
import '../../pdf.dart';
import 'basic.dart';
import 'font.dart';
import 'geometry.dart';
import 'text.dart';
import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';
/// A description of an icon fulfilled by a font glyph.
@immutable
class IconData {
/// Creates icon data.
const IconData(
this.codePoint, {
this.matchTextDirection = false,
});
/// The Unicode code point at which this icon is stored in the icon font.
final int codePoint;
/// Whether this icon should be automatically mirrored in right-to-left
/// environments.
final bool matchTextDirection;
}
/// Defines the color, opacity, and size of icons.
@immutable
class IconThemeData {
/// Creates an icon theme data.
const IconThemeData({this.color, this.opacity, this.size, this.font});
/// Creates an icon them with some reasonable default values.
const IconThemeData.fallback(this.font)
: color = PdfColors.black,
opacity = 1.0,
size = 24.0;
/// Creates a copy of this icon theme but with the given fields replaced with
/// the new values.
IconThemeData copyWith(
{PdfColor? color, double? opacity, double? size, Font? font}) {
return IconThemeData(
color: color ?? this.color,
opacity: opacity ?? this.opacity,
size: size ?? this.size,
font: font ?? this.font,
);
}
/// The default color for icons.
final PdfColor? color;
/// An opacity to apply to both explicit and default icon colors.
final double? opacity;
/// The default size for icons.
final double? size;
/// The font to use
final Font? font;
}
/// A graphical icon widget drawn with a glyph from a font described in
/// an [IconData] such as material's predefined [IconData]s in [Icons].
class Icon extends StatelessWidget {
/// Creates an icon.
Icon(
this.icon, {
this.size,
this.color,
this.textDirection,
this.font,
}) : super();
/// The icon to display. The available icons are described in [Icons].
final IconData icon;
/// The size of the icon in logical pixels.
final double? size;
/// The color to use when drawing the icon.
final PdfColor? color;
/// The text direction to use for rendering the icon.
final TextDirection? textDirection;
/// Font to use to draw the icon
final Font? font;
@override
Widget build(Context context) {
final textDirection = this.textDirection ?? Directionality.of(context);
final iconTheme = Theme.of(context).iconTheme;
final iconSize = size ?? iconTheme.size;
final iconColor = color ?? iconTheme.color!;
final iconOpacity = iconColor.alpha;
final iconFont = font ?? iconTheme.font;
Widget iconWidget = RichText(
textDirection: textDirection,
text: TextSpan(
text: String.fromCharCode(icon.codePoint),
style: TextStyle.defaultStyle().copyWith(
color: iconColor,
fontSize: iconSize,
fontNormal: iconFont,
),
),
);
if (icon.matchTextDirection) {
switch (textDirection) {
case TextDirection.rtl:
iconWidget = Transform(
transform: Matrix4.identity()..scale(-1.0, 1.0, 1.0),
alignment: Alignment.center,
child: iconWidget,
);
break;
case TextDirection.ltr:
break;
}
}
if (iconOpacity < 1.0) {
iconWidget = Opacity(
opacity: iconOpacity,
child: iconWidget,
);
}
return iconWidget;
}
}