Skip to content

Commit

Permalink
Refactor Background widget factory methods for improved clarity and e…
Browse files Browse the repository at this point in the history
…fficiency; enhance gradient handling in tests
  • Loading branch information
lucasilverentand committed Dec 19, 2024
1 parent 4e03fe5 commit 4c76905
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
16 changes: 4 additions & 12 deletions oui/lib/src/utils/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ class Background extends StatelessWidget {
this.custom,
});

factory Background.color(Color color) => Background._(
color: color,
gradient: null,
custom: null,
);
factory Background.color(Color color) => Background._(color: color);

factory Background.gradient(Gradient gradient) => Background._(
gradient: gradient,
color: null,
custom: null,
);
factory Background.gradient(Gradient gradient) =>
Background._(gradient: gradient);

factory Background.custom(
Widget Function(BuildContext) custom, {
Expand All @@ -44,8 +37,7 @@ class Background extends StatelessWidget {
);
} else if (color != null) {
background = Container(
decoration:
BoxDecoration(color: color), // Use BoxDecoration to include color
decoration: BoxDecoration(color: color),
);
}

Expand Down
21 changes: 15 additions & 6 deletions oui/test/background_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ void main() {
(container.decoration as BoxDecoration?)?.color,
const Color(0xFFFF0000),
);
expect(find.byType(Container), findsOneWidget);
});

testWidgets('Background with gradient', (WidgetTester tester) async {
const gradient =
LinearGradient(colors: [Color(0xFFFF0000), Color(0xFF0000FF)]);
const gradient = LinearGradient(
colors: [Color(0xFFFF0000), Color(0xFF0000FF)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
);

await tester.pumpWidget(
WidgetsApp(
Expand All @@ -30,7 +34,8 @@ void main() {
);

final container = tester.widget<Container>(find.byType(Container));
expect((container.decoration as BoxDecoration?)?.gradient, gradient);
final boxDecoration = container.decoration as BoxDecoration?;
expect(boxDecoration?.gradient, gradient);
});

testWidgets('Background with custom widget', (WidgetTester tester) async {
Expand Down Expand Up @@ -68,8 +73,11 @@ void main() {

testWidgets('Background with gradient and custom widget',
(WidgetTester tester) async {
const gradient =
LinearGradient(colors: [Color(0xFFFF0000), Color(0xFF0000FF)]);
const gradient = LinearGradient(
colors: [Color(0xFFFF0000), Color(0xFF0000FF)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
);

await tester.pumpWidget(
WidgetsApp(
Expand All @@ -82,7 +90,8 @@ void main() {
);

final container = tester.widget<Container>(find.byType(Container));
expect((container.decoration as BoxDecoration?)?.gradient, gradient);
final boxDecoration = container.decoration as BoxDecoration?;
expect(boxDecoration?.gradient, gradient);
expect(find.text('Custom Widget'), findsOneWidget);
});
}

0 comments on commit 4c76905

Please sign in to comment.