Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Background widget constructors to use private constructor for improved encapsulation #40

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion oui/lib/src/app/oui_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OuiApp extends StatelessWidget {
this.config = const OuiConfig(),
}) : _registry = OuiScreenRegistry(root, null) {
_routerInformationParser = OuiRouteInformationParser(_registry);
_router = OuiRouter(_registry);
_router = OuiRouter();
}

@override
Expand Down
2 changes: 0 additions & 2 deletions oui/lib/src/router/oui_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ class OuiPath {
/// Returns a new [OuiPath] with the given [segments] appended to the end.
/// If [segments] is empty, returns this path.
OuiPath add(List<OuiPathSegment> segments) {
assert(
segments.every((s) => s != null), 'Cannot add null segments to path');
if (segments.isEmpty) {
return this;
}
Expand Down
5 changes: 1 addition & 4 deletions oui/lib/src/router/oui_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'oui_path_match.dart';
import '../scaffold/oui_scaffold.dart';
import '../screens/oui_screen_registry.dart';

class OuiRouter extends RouterDelegate<OuiPathMatch> with ChangeNotifier {
final OuiScreenRegistry _registry;

OuiPathMatch _activeMatch = OuiPathMatch.noMatch;
OuiPathMatch get match => _activeMatch;

OuiRouter(this._registry);
OuiRouter();

@override
OuiPathMatch? get currentConfiguration => _activeMatch;
Expand Down
36 changes: 20 additions & 16 deletions oui/lib/src/utils/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ class Background extends StatelessWidget {
final Gradient? gradient;
final Widget Function(BuildContext)? custom;

const Background({
super.key,
const Background._({
this.color,
this.gradient,
this.custom,
});

factory Background.color(Color color) => Background(color: color);
factory Background.color(Color color) => Background._(color: color);

factory Background.gradient(Gradient gradient) =>
Background(gradient: gradient);
Background._(gradient: gradient);

factory Background.custom(
Widget Function(BuildContext) custom, {
Color? color,
Gradient? gradient,
}) =>
Background(
Background._(
color: color,
gradient: gradient,
custom: custom,
Expand All @@ -37,20 +36,25 @@ class Background extends StatelessWidget {
decoration: BoxDecoration(gradient: gradient),
);
} else if (color != null) {
background = Container(color: color);
background = Container(
decoration: BoxDecoration(color: color),
);
}

if (custom != null && background != null) {
background = Stack(
children: [
background,
custom!(context),
],
);
} else {
background = custom!(context);
if (custom != null) {
if (background != null) {
background = Stack(
children: [
background,
custom!(context),
],
);
} else {
background = custom!(context);
}
}

return background;
// Fallback to an empty widget if all parameters are null
return background ?? const SizedBox.shrink();
}
}
97 changes: 97 additions & 0 deletions oui/test/background_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:oui/src/utils/background.dart';

void main() {
testWidgets('Background with color', (WidgetTester tester) async {
await tester.pumpWidget(
WidgetsApp(
color: const Color(0xFFFFFFFF),
builder: (context, _) => Background.color(const Color(0xFFFF0000)),
),
);

final container = tester.widget<Container>(find.byType(Container));
expect(
(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)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
);

await tester.pumpWidget(
WidgetsApp(
color: const Color(0xFFFFFFFF),
builder: (context, _) => Background.gradient(gradient),
),
);

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

testWidgets('Background with custom widget', (WidgetTester tester) async {
await tester.pumpWidget(
WidgetsApp(
color: const Color(0xFFFFFFFF),
builder: (context, _) => Background.custom(
(context) => const Text('Custom Widget'),
),
),
);

expect(find.text('Custom Widget'), findsOneWidget);
});

testWidgets('Background with color and custom widget',
(WidgetTester tester) async {
await tester.pumpWidget(
WidgetsApp(
color: const Color(0xFFFFFFFF),
builder: (context, _) => Background.custom(
(context) => const Text('Custom Widget'),
color: const Color(0xFFFF0000),
),
),
);

final container = tester.widget<Container>(find.byType(Container));
expect(
(container.decoration as BoxDecoration?)?.color,
const Color(0xFFFF0000),
);
expect(find.text('Custom Widget'), findsOneWidget);
});

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

await tester.pumpWidget(
WidgetsApp(
color: const Color(0xFFFFFFFF),
builder: (context, _) => Background.custom(
(context) => const Text('Custom Widget'),
gradient: gradient,
),
),
);

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