Skip to content

Commit e4f8a8f

Browse files
authored
Add a copy with method to FeedbackThemeData (#312)
* Add a copy with method to FeedbackThemeData * Make brightness overridable * Actually set the class property * Add simple test for copyWith
1 parent b9b7875 commit e4f8a8f

2 files changed

Lines changed: 101 additions & 3 deletions

File tree

feedback/lib/src/theme/feedback_theme.dart

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class FeedbackThemeData {
4141
this.bottomSheetDescriptionStyle = _defaultBottomSheetDescriptionStyle,
4242
this.bottomSheetTextInputStyle = _defaultBottomSheetTextInputStyle,
4343
this.sheetIsDraggable = true,
44+
Brightness? brightness,
4445
Color? dragHandleColor,
4546
ColorScheme? colorScheme})
4647
:
@@ -50,8 +51,9 @@ class FeedbackThemeData {
5051
// ignore: prefer_is_empty
5152
drawColors.length > 0,
5253
'There must be at least one color to draw with',
53-
),
54-
brightness = ThemeData.estimateBrightnessForColor(feedbackSheetColor) {
54+
) {
55+
this.brightness =
56+
brightness ??= ThemeData.estimateBrightnessForColor(feedbackSheetColor);
5557
final bool isDark = brightness == Brightness.dark;
5658
this.dragHandleColor =
5759
dragHandleColor ?? (isDark ? Colors.black26 : Colors.white38);
@@ -71,6 +73,7 @@ class FeedbackThemeData {
7173
color: Colors.white,
7274
),
7375
sheetIsDraggable: sheetIsDraggable,
76+
brightness: Brightness.dark,
7477
);
7578

7679
/// Create a light version of the [FeedbackThemeData]
@@ -81,10 +84,11 @@ class FeedbackThemeData {
8184
feedbackSheetColor: _lightGrey,
8285
bottomSheetDescriptionStyle: _defaultBottomSheetDescriptionStyle,
8386
sheetIsDraggable: sheetIsDraggable,
87+
brightness: Brightness.light,
8488
);
8589

8690
/// Brightness of the theme based on the [background] color
87-
final Brightness brightness;
91+
late final Brightness brightness;
8892

8993
/// The background of the feedback view.
9094
final Color background;
@@ -122,6 +126,39 @@ class FeedbackThemeData {
122126

123127
/// [ColorScheme] on the feedback UI
124128
late final ColorScheme colorScheme;
129+
130+
/// Creates a copy of the current [FeedbackThemeData] with the given
131+
/// optional fields replaced with the given values.
132+
FeedbackThemeData copyWith({
133+
Color? background,
134+
Color? feedbackSheetColor,
135+
double? feedbackSheetHeight,
136+
Color? activeFeedbackModeColor,
137+
List<Color>? drawColors,
138+
TextStyle? bottomSheetDescriptionStyle,
139+
TextStyle? bottomSheetTextInputStyle,
140+
bool? sheetIsDraggable,
141+
Color? dragHandleColor,
142+
Brightness? brightness,
143+
ColorScheme? colorScheme,
144+
}) {
145+
return FeedbackThemeData(
146+
background: background ?? this.background,
147+
feedbackSheetColor: feedbackSheetColor ?? this.feedbackSheetColor,
148+
feedbackSheetHeight: feedbackSheetHeight ?? this.feedbackSheetHeight,
149+
activeFeedbackModeColor:
150+
activeFeedbackModeColor ?? this.activeFeedbackModeColor,
151+
drawColors: drawColors ?? this.drawColors,
152+
bottomSheetDescriptionStyle:
153+
bottomSheetDescriptionStyle ?? this.bottomSheetDescriptionStyle,
154+
bottomSheetTextInputStyle:
155+
bottomSheetTextInputStyle ?? this.bottomSheetTextInputStyle,
156+
sheetIsDraggable: sheetIsDraggable ?? this.sheetIsDraggable,
157+
dragHandleColor: dragHandleColor ?? this.dragHandleColor,
158+
brightness: brightness ?? this.brightness,
159+
colorScheme: colorScheme ?? this.colorScheme,
160+
);
161+
}
125162
}
126163

127164
/// Provides an instance of [FeedbackThemeData] for all descendants.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:feedback/feedback.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
test('copyWith overwrites all properties without changing original', () {
7+
final theme = FeedbackThemeData(
8+
background: Colors.grey,
9+
feedbackSheetColor: Colors.white,
10+
feedbackSheetHeight: .25,
11+
activeFeedbackModeColor: Colors.blue,
12+
drawColors: [Colors.orange, Colors.green],
13+
bottomSheetDescriptionStyle: const TextStyle(color: Colors.black),
14+
bottomSheetTextInputStyle: const TextStyle(color: Colors.white),
15+
sheetIsDraggable: true,
16+
brightness: Brightness.light,
17+
dragHandleColor: Colors.white,
18+
colorScheme: const ColorScheme.light(),
19+
);
20+
21+
final copy = theme.copyWith(
22+
background: Colors.red,
23+
feedbackSheetColor: Colors.blue,
24+
feedbackSheetHeight: .5,
25+
activeFeedbackModeColor: Colors.green,
26+
drawColors: [Colors.red, Colors.blue],
27+
bottomSheetDescriptionStyle: const TextStyle(color: Colors.green),
28+
bottomSheetTextInputStyle: const TextStyle(color: Colors.yellow),
29+
sheetIsDraggable: false,
30+
brightness: Brightness.dark,
31+
dragHandleColor: Colors.black,
32+
colorScheme: const ColorScheme.dark(),
33+
);
34+
35+
// ensure overrides apply
36+
expect(copy.background, Colors.red);
37+
expect(copy.feedbackSheetColor, Colors.blue);
38+
expect(copy.feedbackSheetHeight, .5);
39+
expect(copy.activeFeedbackModeColor, Colors.green);
40+
expect(copy.drawColors, [Colors.red, Colors.blue]);
41+
expect(copy.bottomSheetDescriptionStyle, const TextStyle(color: Colors.green));
42+
expect(copy.bottomSheetTextInputStyle, const TextStyle(color: Colors.yellow));
43+
expect(copy.sheetIsDraggable, false);
44+
expect(copy.brightness, Brightness.dark);
45+
expect(copy.dragHandleColor, Colors.black);
46+
expect(copy.colorScheme, const ColorScheme.dark());
47+
48+
// ensure original theme is not modified
49+
expect(theme.background, Colors.grey);
50+
expect(theme.feedbackSheetColor, Colors.white);
51+
expect(theme.feedbackSheetHeight, .25);
52+
expect(theme.activeFeedbackModeColor, Colors.blue);
53+
expect(theme.drawColors, [Colors.orange, Colors.green]);
54+
expect(theme.bottomSheetDescriptionStyle, const TextStyle(color: Colors.black));
55+
expect(theme.bottomSheetTextInputStyle, const TextStyle(color: Colors.white));
56+
expect(theme.sheetIsDraggable, true);
57+
expect(theme.brightness, Brightness.light);
58+
expect(theme.dragHandleColor, Colors.white);
59+
expect(theme.colorScheme, const ColorScheme.light());
60+
});
61+
}

0 commit comments

Comments
 (0)