-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny_container.dart
216 lines (183 loc) · 6.18 KB
/
tiny_container.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
import 'package:flutter/material.dart';
class TinyContainer extends StatelessWidget {
/// Requires the child to have exactly this width, it's assigned to external [SizedBox].
/// Defaults to : MediaQuery.of(context).size.width * 0.25.
final double maxWidth;
/// borderRadius given to [Container],
/// Defaults to: 5
final double borderRadius;
/// Requires the child to have exactly this height, it's assigned to external [SizedBox].
/// Defaults to : MediaQuery.of(context).size.height * 0.06.
final double maxHeight;
/// Minimum allowed width, it's assigned to internal [Container].
/// Defaults to : MediaQuery.of(context).size.width * 0.15.
final double width;
/// Minimum allowed height, it's assigned to internal [Container].
/// Defaults to : MediaQuery.of(context).size.height * 0.03.
final double height;
/// FontSize for [text] if given,
/// Defaults to : 14.
final double fontSize;
/// TextScaleFactor for [Text] if given,
/// Defaults to : 1.0.
final double textScaleFactor;
/// Text given to [Text] widget if child == null.
/// If Empty String it gets passed to [_fixEmptyText] to return " "
final String text;
/// Text given to [Text] widget if child == null.
/// Defaults to : "Hacen".
final String fontFamily;
/// BackgroundColor of [Container],
/// Defaults to : Color(0xFF6D6DFF).
final Color backgroundColor;
/// Color given to [Text] widget if child == null.
/// Defaults to : Colors.white.
final Color textColor;
/// Color given to [InkWell] widget if child == null.
/// Defaults to : Colors.transparent.
final Color hoverColor;
/// Color given to [InkWell] widget if child == null.
/// Defaults to : Colors.transparent.
final Color splashColor;
/// Color given to [InkWell] widget if child == null.
/// Defaults to : Colors.transparent.
final Color focusColor;
/// Color given to [InkWell] widget if child == null.
/// Defaults to : Colors.transparent.
final Color highlightColor;
/// FontWeight given to [Text] widget if child == null.
/// Defaults to : FontWeight.normal.
final FontWeight fontWeight;
/// TextDecoration given to [Text] widget if child == null,
/// Defaults to : [Null].
final TextDecoration textDecoration;
/// If non-null, [TinyContainer] is wrapped in [InkWell].
/// Defaults to : [Null].
final VoidCallback onTap;
/// BoxFit given to [FittedBox] widget if child == null.
/// Defaults to : BoxFit.scaleDown.
final BoxFit boxFit;
/// Child Widget given to the [Container], this makes any [Text] attr useless
/// Defaults to : [Null].
final Widget child;
/// AlignmentGeometry of [Container] child,
/// Defaults to : Alignment.center.
final AlignmentGeometry alignment;
/// if non-null [TinyContainer] is wrapped in Padding,
/// Defaults to : [Null].
final EdgeInsets outerPadding;
/// if non-null innerPadding is given to [Container],
/// Defaults to : [Null].
final EdgeInsets innerPadding;
/// if non-null border is given to [Container],
/// Defaults to : [Null].
final BoxBorder border;
/// if non-null boxShadow is given to [Container],
/// Defaults to : [Null].
final List<BoxShadow> boxShadow;
/// if non-null textAlign is given to [Text],
/// Defaults to : [Null].
final TextAlign textAlign;
/// This Class can be used as Const as it requires no non-const Attr.
const TinyContainer({
Key key,
this.maxWidth,
this.maxHeight,
this.width,
this.height,
this.fontSize = 14,
this.textScaleFactor = 1.0,
this.text,
this.fontFamily = "Hacen",
this.backgroundColor = const Color(0xFF6D6DFF),
this.textColor = Colors.white,
this.hoverColor: Colors.transparent,
this.highlightColor: Colors.transparent,
this.focusColor: Colors.transparent,
this.splashColor: Colors.transparent,
this.fontWeight = FontWeight.normal,
this.textDecoration,
this.onTap,
this.boxFit = BoxFit.scaleDown,
this.child,
this.alignment = Alignment.center,
this.outerPadding,
this.innerPadding,
this.border,
this.boxShadow,
this.textAlign,
this.borderRadius = 5,
}) : assert(child != null || text != null),
super(key: key);
/// This function used as Work around of if given [text] is empty, as:
/// [FittedBox] will complain about [Text] have to width/height to [boxFit].
_fixEmptyText(String text) {
if (text.isEmpty) return " ";
return text;
}
@override
Widget build(BuildContext context) {
// Child Widget;
Widget childWidget;
// We make a check if it's Widget [child] or text
if (child != null) {
childWidget = child;
} else {
childWidget = FittedBox(
fit: boxFit,
child: Text(
_fixEmptyText(text),
textScaleFactor: textScaleFactor,
textAlign: textAlign,
style: TextStyle(
// TODO: Let this use Theme Data
decoration: textDecoration,
color: textColor,
fontWeight: fontWeight,
fontSize: fontSize,
fontFamily: fontFamily,
),
),
);
}
// Our Widget
Widget finalWidget = SizedBox(
width: maxWidth ?? MediaQuery.of(context).size.width * 0.25,
height: maxHeight ?? MediaQuery.of(context).size.height * 0.06,
child: Container(
padding: innerPadding,
width: width ?? MediaQuery.of(context).size.width * 0.15,
height: height ?? MediaQuery.of(context).size.height * 0.03,
decoration: BoxDecoration(
boxShadow: boxShadow,
border: border,
color: backgroundColor,
borderRadius: BorderRadius.all(
Radius.circular(borderRadius),
),
),
alignment: alignment,
child: childWidget,
),
);
// adding Tab Feature
if (onTap != null) {
finalWidget = InkWell(
hoverColor: hoverColor,
highlightColor: hoverColor,
focusColor: hoverColor,
splashColor: hoverColor,
onTap: onTap,
child: finalWidget,
);
}
// adding OuterPadding Feature
if (outerPadding != null) {
finalWidget = Padding(
padding: outerPadding,
child: finalWidget,
);
}
return finalWidget;
}
}