Skip to content

Commit 8972754

Browse files
committed
color [nfc]: Add ColorExtension.withFadedAlpha
And use it everywhere we were using `.withValues(alpha:` to fade a color variable, except for one place in `lib/widgets/emoji_reaction.dart` where the variable is semi-transparent in dark mode to begin with. We'll do that as a non-NFC change in a later commit. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 157418c commit 8972754

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

lib/widgets/action_sheet.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '../model/internal_link.dart';
1313
import '../model/narrow.dart';
1414
import 'actions.dart';
1515
import 'clipboard.dart';
16+
import 'color.dart';
1617
import 'dialog.dart';
1718
import 'icons.dart';
1819
import 'inset_shadow.dart';
@@ -145,8 +146,8 @@ abstract class MessageActionSheetMenuItemButton extends StatelessWidget {
145146
foregroundColor: designVariables.contextMenuItemText,
146147
splashFactory: NoSplash.splashFactory,
147148
).copyWith(backgroundColor: WidgetStateColor.resolveWith((states) =>
148-
designVariables.contextMenuItemBg.withValues(
149-
alpha: states.contains(WidgetState.pressed) ? 0.20 : 0.12))),
149+
designVariables.contextMenuItemBg.withFadedAlpha(
150+
states.contains(WidgetState.pressed) ? 0.20 : 0.12))),
150151
onPressed: () => _handlePressed(context),
151152
child: Text(label(zulipLocalizations),
152153
style: const TextStyle(fontSize: 20, height: 24 / 20)
@@ -168,8 +169,8 @@ class MessageActionSheetCancelButton extends StatelessWidget {
168169
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7)),
169170
splashFactory: NoSplash.splashFactory,
170171
).copyWith(backgroundColor: WidgetStateColor.resolveWith((states) =>
171-
designVariables.contextMenuCancelBg.withValues(
172-
alpha: states.contains(WidgetState.pressed) ? 0.20 : 0.15))),
172+
designVariables.contextMenuCancelBg.withFadedAlpha(
173+
states.contains(WidgetState.pressed) ? 0.20 : 0.15))),
173174
onPressed: () {
174175
Navigator.pop(context);
175176
},

lib/widgets/color.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,21 @@ extension ColorExtension on Color {
3838
((g * 255.0).round() & 0xff) << 8 |
3939
((b * 255.0).round() & 0xff) << 0;
4040
}
41+
42+
/// Makes a copy of this color with [a] multiplied by `factor`.
43+
///
44+
/// `factor` must be between 0 and 1, inclusive.
45+
///
46+
/// To fade a color variable from [DesignVariables], [ContentTheme], etc.,
47+
/// use this instead of calling [withValues] with `factor` passed as `alpha`,
48+
/// which simply replaces the color's [a] instead of multiplying by it.
49+
/// Using [withValues] gives the same result for an opaque color,
50+
/// but a wrong result for a semi-transparent color,
51+
/// and we want our color variables to be free to change
52+
/// without breaking things.
53+
Color withFadedAlpha(double factor) {
54+
assert(factor >= 0);
55+
assert(factor <= 1);
56+
return withValues(alpha: a * factor);
57+
}
4158
}

test/widgets/color_test.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'dart:ui';
2-
31
import 'package:checks/checks.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_checks/flutter_checks.dart';
44
import 'package:test/scaffolding.dart';
55
import 'package:zulip/widgets/color.dart';
66

@@ -14,5 +14,27 @@ void main() {
1414
check(Color(testCase).argbInt).equals(testCase);
1515
}
1616
});
17+
18+
const color = Color.fromRGBO(100, 200, 100, 0.5);
19+
20+
test('withFadedAlpha smoke', () {
21+
check(color.withFadedAlpha(0.5))
22+
.isSameColorAs(color.withValues(alpha: 0.25));
23+
});
24+
25+
test('withFadedAlpha opaque color', () {
26+
const color = Colors.black;
27+
28+
check(color.withFadedAlpha(0.5))
29+
.isSameColorAs(color.withValues(alpha: 0.5));
30+
});
31+
32+
test('withFadedAlpha factor > 1 fails', () {
33+
check(() => color.withFadedAlpha(1.1)).throws<AssertionError>();
34+
});
35+
36+
test('withFadedAlpha factor < 0 fails', () {
37+
check(() => color.withFadedAlpha(-0.1)).throws<AssertionError>();
38+
});
1739
});
1840
}

0 commit comments

Comments
 (0)