Skip to content

Commit 24f0974

Browse files
committed
feat/axis-alignment-alert
1 parent cf4832c commit 24f0974

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Add `header` and `footer` to `ShadSelect` and `ShadSelectFormField`.
44
- Fix unintentional disposal of `controller` in `ShadSelect`.
5+
- Add `mainAxisAlignment` and `crossAxisAlignment` to `ShadAlert`.
6+
- Remove assert about `icon` and `iconSrc` in `ShadAlert`, you can avoid using an icon now.
57

68
## 0.7.2
79

lib/src/components/alert.dart

+35-26
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ class ShadAlert extends StatelessWidget {
2222
this.iconSize,
2323
this.titleStyle,
2424
this.descriptionStyle,
25-
}) : variant = ShadAlertVariant.primary,
26-
assert(
27-
(icon != null) ^ (iconSrc != null),
28-
'Either icon or iconSrc must be provided',
29-
);
25+
this.mainAxisAlignment,
26+
this.crossAxisAlignment,
27+
}) : variant = ShadAlertVariant.primary;
3028

3129
const ShadAlert.destructive({
3230
super.key,
@@ -41,11 +39,9 @@ class ShadAlert extends StatelessWidget {
4139
this.iconSize,
4240
this.titleStyle,
4341
this.descriptionStyle,
44-
}) : variant = ShadAlertVariant.destructive,
45-
assert(
46-
(icon != null) ^ (iconSrc != null),
47-
'Either icon or iconSrc must be provided',
48-
);
42+
this.mainAxisAlignment,
43+
this.crossAxisAlignment,
44+
}) : variant = ShadAlertVariant.destructive;
4945

5046
const ShadAlert.raw({
5147
super.key,
@@ -61,10 +57,9 @@ class ShadAlert extends StatelessWidget {
6157
this.iconSize,
6258
this.titleStyle,
6359
this.descriptionStyle,
64-
}) : assert(
65-
(icon != null) ^ (iconSrc != null),
66-
'Either icon or iconSrc must be provided',
67-
);
60+
this.mainAxisAlignment,
61+
this.crossAxisAlignment,
62+
});
6863

6964
final ShadAlertVariant variant;
7065
final Widget? icon;
@@ -78,6 +73,8 @@ class ShadAlert extends StatelessWidget {
7873
final Size? iconSize;
7974
final TextStyle? titleStyle;
8075
final TextStyle? descriptionStyle;
76+
final MainAxisAlignment? mainAxisAlignment;
77+
final CrossAxisAlignment? crossAxisAlignment;
8178

8279
@override
8380
Widget build(BuildContext context) {
@@ -108,16 +105,19 @@ class ShadAlert extends StatelessWidget {
108105
final effectiveIconSize =
109106
iconSize ?? effectiveAlertTheme.iconSize ?? const Size.square(16);
110107

111-
final effectiveIcon = Padding(
112-
padding: effectiveIconPadding,
113-
child: icon ??
114-
ShadImage(
115-
iconSrc!,
116-
width: effectiveIconSize.width,
117-
height: effectiveIconSize.height,
118-
color: effectiveIconColor,
119-
),
120-
);
108+
final hasIcon = icon != null || iconSrc != null;
109+
final effectiveIcon = hasIcon
110+
? Padding(
111+
padding: effectiveIconPadding,
112+
child: icon ??
113+
ShadImage(
114+
iconSrc!,
115+
width: effectiveIconSize.width,
116+
height: effectiveIconSize.height,
117+
color: effectiveIconColor,
118+
),
119+
)
120+
: null;
121121

122122
final effectiveTitleStyle = titleStyle ??
123123
effectiveAlertTheme.titleStyle ??
@@ -131,13 +131,22 @@ class ShadAlert extends StatelessWidget {
131131
effectiveAlertTheme.descriptionStyle ??
132132
theme.textTheme.muted.copyWith(color: theme.colorScheme.foreground);
133133

134+
final effectiveMainAxisAlignment = mainAxisAlignment ??
135+
effectiveAlertTheme.mainAxisAlignment ??
136+
MainAxisAlignment.start;
137+
138+
final effectiveCrossAxisAlignment = crossAxisAlignment ??
139+
effectiveAlertTheme.crossAxisAlignment ??
140+
CrossAxisAlignment.start;
141+
134142
return ShadDecorator(
135143
decoration: effectiveDecoration,
136144
child: Row(
137-
crossAxisAlignment: CrossAxisAlignment.start,
145+
crossAxisAlignment: effectiveCrossAxisAlignment,
146+
mainAxisAlignment: effectiveMainAxisAlignment,
138147
textDirection: textDirection,
139148
children: [
140-
effectiveIcon,
149+
if (effectiveIcon != null) effectiveIcon,
141150
Flexible(
142151
child: Column(
143152
crossAxisAlignment: CrossAxisAlignment.start,

lib/src/theme/components/alert.dart

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class ShadAlertTheme {
1111
this.iconSize,
1212
this.titleStyle,
1313
this.descriptionStyle,
14+
this.mainAxisAlignment,
15+
this.crossAxisAlignment,
1416
});
1517

1618
final bool merge;
@@ -20,6 +22,8 @@ class ShadAlertTheme {
2022
final Size? iconSize;
2123
final TextStyle? titleStyle;
2224
final TextStyle? descriptionStyle;
25+
final MainAxisAlignment? mainAxisAlignment;
26+
final CrossAxisAlignment? crossAxisAlignment;
2327

2428
static ShadAlertTheme lerp(
2529
ShadAlertTheme a,
@@ -36,6 +40,8 @@ class ShadAlertTheme {
3640
titleStyle: TextStyle.lerp(a.titleStyle, b.titleStyle, t),
3741
descriptionStyle:
3842
TextStyle.lerp(a.descriptionStyle, b.descriptionStyle, t),
43+
mainAxisAlignment: t < 0.5 ? a.mainAxisAlignment : b.mainAxisAlignment,
44+
crossAxisAlignment: t < 0.5 ? a.crossAxisAlignment : b.crossAxisAlignment,
3945
);
4046
}
4147

@@ -47,6 +53,8 @@ class ShadAlertTheme {
4753
Size? iconSize,
4854
TextStyle? titleStyle,
4955
TextStyle? descriptionStyle,
56+
MainAxisAlignment? mainAxisAlignment,
57+
CrossAxisAlignment? crossAxisAlignment,
5058
}) {
5159
return ShadAlertTheme(
5260
merge: merge ?? this.merge,
@@ -56,6 +64,8 @@ class ShadAlertTheme {
5664
iconSize: iconSize ?? this.iconSize,
5765
titleStyle: titleStyle ?? this.titleStyle,
5866
descriptionStyle: descriptionStyle ?? this.descriptionStyle,
67+
mainAxisAlignment: mainAxisAlignment ?? this.mainAxisAlignment,
68+
crossAxisAlignment: crossAxisAlignment ?? this.crossAxisAlignment,
5969
);
6070
}
6171

@@ -69,6 +79,8 @@ class ShadAlertTheme {
6979
iconSize: other.iconSize,
7080
titleStyle: other.titleStyle,
7181
descriptionStyle: other.descriptionStyle,
82+
mainAxisAlignment: other.mainAxisAlignment,
83+
crossAxisAlignment: other.crossAxisAlignment,
7284
);
7385
}
7486

@@ -83,7 +95,9 @@ class ShadAlertTheme {
8395
other.iconColor == iconColor &&
8496
other.iconSize == iconSize &&
8597
other.titleStyle == titleStyle &&
86-
other.descriptionStyle == descriptionStyle;
98+
other.descriptionStyle == descriptionStyle &&
99+
other.mainAxisAlignment == mainAxisAlignment &&
100+
other.crossAxisAlignment == crossAxisAlignment;
87101
}
88102

89103
@override
@@ -94,6 +108,8 @@ class ShadAlertTheme {
94108
iconColor.hashCode ^
95109
iconSize.hashCode ^
96110
titleStyle.hashCode ^
97-
descriptionStyle.hashCode;
111+
descriptionStyle.hashCode ^
112+
mainAxisAlignment.hashCode ^
113+
crossAxisAlignment.hashCode;
98114
}
99115
}

0 commit comments

Comments
 (0)